Applied Spatial Analysis Using QGIS - Attribute Queries, Expressions & Selection Math
Applied Spatial Analysis Using QGIS — GIS for Surveyors (2026 Cohort) · 04 Oct 2026 · 40:56 · Watch on YouTube (offline-safe)
pipeline_network attribute table → sort it → read the field types → compute a new field with the Field Calculator → style by status → build "status" = 'active' in the Expression Builder, then type it by hand → finish with CASE WHEN.Data: last week's GeoPackage reopened and re-saved — the
pipeline_network layer (status, pipe_size, length) | Deliverable (graded): a filtered selection driven by your own written expression, plus the styling that shows it on the map — screenshot to WhatsApp Course_Helpers.
I write this after every live session — so students who lost power or signal can catch up in minutes, and so our community has a searchable archive for the future. Week 1 we brought data in. Week 2 we created data and made it measurable. Week 3 we made it correct. This week we finally ask it questions.
This week's one question: "My map shows 455,000 roads. How do I get only the ones that matter?"
Why Week 4 Exists
Weeks 1-3 got you data on a map that was correct: imported, in metres, and topologically clean. This week is the first time you can ask the data a question and get an answer.
That sounds small. It is the whole difference between a map and an analysis. A layer with 455,000 roads is not information — it is a picture of a problem. The moment you can write down "give me the ones that matter", you have analysis.
The plan, live on screen:
- Settle what the attribute table actually is — and why it is the real dataset behind the map
- Open last week's project, then Save As under this week's name
- Open the
pipeline_networkattribute table and read its fields - Sort it, and learn the field types and the functions that come with them
- Field Calculator — compute a new field,
length (m)for every pipe - Style by attribute so the categories become visible
- The Expression Builder, then
"status" = 'active'by hand - The lesson that costs everyone an hour once:
CASE WHEN, and why you must repeat the field name - Where to get bigger data to practise on
1) The Attribute Table Is the Dataset
We opened with the idea that the map is only the picture. Underneath it there is a table, and the table is the data. Everything you drew in Weeks 2 and 3 exists in that table as rows.
Attribute means a property of the thing. A pipe has a status, a size, a length. A parcel has an owner. A road has a class and a name. QGIS calls the columns fields and the rows features. Learn those two words now — every menu in this module uses them.
2) Open Last Week's Project — and Save It As Today
Same rule as Week 3: open the Week 3 project, then Save As to give this session its own file. A graded project you overwrote by accident is a week you repeat.
3) Reading the pipeline_network Table
We worked on the pipeline_network layer from last week, not a fresh dataset. That continuity is deliberate — the layers you built are now the things you query.
Opening the table, the first thing we read was status, and the values in it:
- connected
- disconnected
- damaged
Then pipe_size. This is the connection back to the Arabic explanation earlier in the session, where we talked about classifying features in the field and how that classification ends up here.
The classification you decide in the field is the thing you can filter by later. If the attribute is missing or spelled two different ways in the field, no expression you write will ever find it.
4) Sorting — Free Answers, No Expression Needed
Before any expression, the cheapest possible tool: sort the table.
Click a column header and sort ascending, click again for descending. That is the whole mechanism. We sorted by status so the categories grouped together.
Worth internalising: sorting changes the order rows are drawn and reported in. It does not change which features exist. A sort is a view, not a filter.
5) Field Types and Their Functions
We went through the data types you will meet in an attribute table, and each one brings its own functions:
And the functions that ride on them: round(), upper(), lower(), abs(), and length().
A trap worth naming: comparing text to text is case-sensitive. 'Active' and 'active' are different strings, and your filter will silently return nothing. That is what upper() and lower() are for — normalise first, then compare.
6) Field Calculator — Let QGIS Compute It
This is the step that turns the table from a record into a calculator. The Field Calculator creates a new field and fills it with a computed value, for every feature at once.
We computed the length of each pipe — new field length (m), run it, and every feature now carries its own length. We touched area (m²) for polygons and now() for a timestamp.
The instructor was emphatic about naming, because a field name is a promise to whoever reads the report:
"The project manager... whatever the title, let you confirm the report."
So: name the field for the person who will read it, not for the person who typed it. length_m beats fld_3. status beats Field2.
7) Style by Attribute — Make the Categories Visible
Attributes you never see on the map are attributes you never use. We styled pipeline_network by status, so connected, disconnected and damaged each got their own colour and you could read the network's health at a glance.
Then the practical tip of the session: save the style and load it again from file.
"You have to go through this, go and click Load Style... but I definitely want to get it from file."
Reusable styles mean you stop rebuilding a colour ramp by hand every week, and every map in a project ends up looking like it belongs to the same report. Save the .qml next to your data.
8) The Expression Builder — Then Type It Yourself
The Expression Builder is a dialog with three columns: fields, operators, and values. You build the condition by clicking rather than remembering syntax.
Then we typed it by hand, because you will not always have the builder:
"status" = 'active'
Three things to notice, and all three bit during the session:
- The field name in double quotes —
"status" - The value in single quotes —
'active'. Text values are always quoted. This is the single most common cause of an expression that refuses to run. - Double-click the field name in the table or the builder and it drops into the expression for you — no typing, no spelling mistakes.
Run it, and it returned exactly one feature.
"It returned one feature. Excellent."
Then we selected from the table, filtered from the table, counted what came back, and restyled the selection so it stood out from the rest of the network. Same three moves, three different entry points: map, table, and dialog.
9) CASE WHEN — and the Repeating-Field Mistake
This is the part worth bookmarking, because it is the thing everyone gets wrong once.
We wanted two categories at once: clean and connected. The obvious thing to type is:
"status" = 'clean' or 'connected' <-- WRONG
It fails, and the instructor showed exactly why:
"When you try to doclean or connected... you have to repeat the field. You cannot only saystatusequal clean or connected — it's going to scream."
Each condition has to be a complete statement of its own. So the field name is repeated:
"status" = 'clean' OR "status" = 'connected'
And the tidy way to write it is CASE WHEN:
CASE WHEN "status" = 'clean' THEN 'good' WHEN "status" = 'connected' THEN 'good' ELSE 'other' END
Tested, it returned about eight features. Then we styled those eight blue to prove the selection and the symbology agree.
10) Get Bigger Data and Practise
The last practical step was pointing at where to download complete Africa and Sudan datasets — roads, rivers, water bodies, and far more fields than our training pipeline has.
"This will allow you to download more data sets for practice and then create your own data sets, go and slice it, and play with the rules — and break it and fix it."
That is the right way to learn expressions: break them on purpose. A syntax error you caused deliberately teaches faster than a correct one you copied.
11) Recap — the Nine-Step Version
- Save As your project first, every single week
- Ask the question first. Before you filter, be clear: what question are you trying to answer for the client? Your field names and values should already reflect their workflow ("measure twice, cut once").
- Understand the data model early. The attribute table is the heart of GIS — what makes GIS different from CAD is that every feature carries its description in a database. Model your features ahead of time and understand the client's datasets and requirements.
- Build your forms to enforce consistency. The Value Map in the Attribute Form (from Week 3) is the foundation that keeps your expressions simple and your data clean later.
- Sort the table before writing anything — some answers are free
- Know your field types — text needs quotes, only numbers take
>and< - Field Calculator for computed values, and name fields for the report reader
- Style by attribute, then save the style to a
.qmlfile so you can reuse it - Expressions: field in
"quotes", value in'single quotes', and repeat the field for everyORcondition — or useCASE WHEN
And the instructor's own caveat, which is the honest summary of the session:
"You get some techniques, but you have to practise, practise, practise to master it. A lot of stuff to be covered — just we touch on the stuff. You have to go and read and practise."
What We Did Live — Scrub Guide
- Welcome & this week's mission — SQL-style expressions, filtering, selection, export
- The agenda — attribute table, selection, filtering, subsetting, export
- Why attribute tables matter — "measure twice, cut once"; GIS vs CAD (the database under the map), understand client workflow and data model first
- Chat check, open last week's project, save as today
- The pipeline attribute table —
statusvalues (connected / disconnected / damaged), pipe sizes - Filter, select & export — the core idea (GUI first)
- Sorting — ascending and descending; field types — text, numeric, integer, decimal, date
- Field Calculator —
length (m)per pipe, plusarea (m²)andnow() - Styling by status — load a style from file, reuse the
.qml, and use Value Maps / consistency from Week 3 - The Expression Builder — fields, operators, values
- Building the expression — field in double quotes, value in single quotes, double-click to insert
"status" = 'active'— returns 1 feature; select, filter, count, restyle- Downloading practice datasets — full Africa and Sudan layers
CASE WHEN— two conditions, the repeating-field rule, returns 8 features, styled blue- Recap — next week buffers and spatial operations
Common Hang-ups and Fixes
- The expression will not run and you cannot see why: almost always a quoting error. Field names take
"double quotes", text values take'single quotes'. Numbers take neither. ORreturns nothing or errors: you dropped the field name from the second condition. Write"status" = 'clean' OR "status" = 'connected', or useCASE WHEN.- A filter returns zero rows but you can see the features: case sensitivity.
'Active'will not match'active'. Wrap the field inlower()and compare against lowercase. - You compared a text field with
>or<: only numeric fields support ordering comparisons.pipe_sizeyes,statusno. - The Field Calculator result is all zeros or all null: the field already existed with a fixed width, so the values did not fit. Widen the field, or create a fresh one.
- You want only the selected features as a new layer: export the selection, don't export the layer — the whole layer is the default.
- Your styling disappeared in the next project: you never saved the style to a
.qmlfile.
What You Should Have Now
- Week 3 project reopened and re-saved under this week's name
- The
pipeline_networkattribute table open and understood: fields, types, and thestatuscategories - The table sorted ascending and descending on a field of your choice
- A new computed field from the Field Calculator —
length (m)per pipe is the one we did - The layer styled by
status, with the style saved to a.qmlfile you can reuse "status" = 'active'written, run and understood — 1 featureCASE WHEN "status" = 'clean' OR "status" = 'connected' ... ENDwritten and run — 8 features — and the selection styled to prove it- A note of where to download bigger Africa/Sudan datasets to practise on
One note on the demo layer, and why. This session deliberately taught the technique on the pipeline_network layer we built last week, rather than retyping your lab's roads query on screen. The point was the method — sorting, field types, Field Calculator, the repeating field name with OR, and CASE WHEN — on data you already know.
Your brief still asks for Filtered_Primary_Roads.gpkg from roads.gpkg (filter fclass = 'primary'), and build exactly what your brief asks for. You are not meant to copy my pipe example into your submission; you are meant to apply the technique to the dataset in your lab drive. The skills are identical — only the target layer and field name change. And the primary roads you filter are also the input that Week 5 buffers by 50 m for Project 4A.
This is Project 3. One quiz plus one practical lab — graded.
Chapters (from the recording — 40:56)
00:00 Welcome & this week's mission 06:10 The agenda — attribute table, selection, filtering & export 09:00 Chat check, open last week's project & save as today 09:30 The pipeline attribute table — fields, status & pipe size 15:26 Filter, select & export — the core idea 17:00 Sorting the table & the field types 19:00 Field Calculator — each pipe's length 22:08 Styling by status — loading a style from file 31:00 The Expression Builder 32:00 Building the expression — field, operator & quotes 34:30 "status" = 'active' — selection, filtering & counting 36:30 Downloading more practice datasets 38:00 CASE WHEN — two conditions & styling the result 40:00 Recap & next week — buffers in Week 5
What's Next
Week 5 — Spatial Proximity & Multi-Criteria Analysis (Project 4A): we stop asking questions of rows and start asking them of space. Buffer the primary roads at 50 m, clip the buildings inside the zone, and compute the affected area in m² and hectares. "How many buildings fall inside a 50 m no-build setback from primary roads?" Bring your layers from this week — the queries you wrote today decide what goes into the buffer. Every Sunday 9:45 a.m.
Resources: YouTube playlist Applied Spatial Analysis Using QGIS — Live Sessions & Labs · Video: Week 4 on YouTube · Course site saltco.org · Software QGIS (free & open source, offline) · Previous: Week 3 — Cadastral Boundary Mapping & Topology · Full publish kit in recordings/zoom_recordings/04_WEEK4/
Did I miss something? Tell me in the Q&A or open an issue — this archive is living and I will update it. — Sallah