Ujaval Gandhi
Spatial Thoughts
ujaval@spatialthoughts.com
Learn QGIS expressions from the ground up.
QGIS Expressions Masterclass
QGIS Expressions Masterclass
This workshop will teach you the basics of QGIS expression syntax and how to use them to solve complex problems.
Class materials and data package available at https://courses.spatialthoughts.com/qgis-expressions.html
Spatial Thoughts
✅ QGIS.org certified training provider
✅ QGIS.org sustaining member
Introduction
Ujaval Gandhi
Introduction
Vigna Purohit
What are QGIS Expressions?
Combination of one or more values, operators and functions that is evaluated in the context of the QGIS project.
QGIS uses its own expression language that is derived from SQL syntax.
Expressions are used throughout QGIS.
Where can you use expressions?
You can use expressions throughout QGIS
The Expression Syntax
'name' → the string name
"name" → value of the name attribute of the current feature
@name → value of the variable called name
name() → output of the function called name
* There are several functions of the form $geometry, $id for obtaining attributes of the current feature. Since QGIS 3.28, they have been deprecated in favor of the variable @geometry, @id etc.
Anatomy of an expression
length(@geometry) > 100
false
function
variable
operator
value
result
evaluation
Operators and Conditionals
Operators
Operators are used to describe the action to be performed in the expression. QGIS expressions use operators derived from SQL.
Conditionals
Conditionals are used for making decisions based on certain conditions directly within the expression.
Functions
What are functions?
Function name
Input parameter
Input parameter
round(3.14159)
3
round(3.14159, 2)
3.14
For better readability, you can use named parameters.
round(
value:=3.14159,
places:=2
)
3.14
Functions can be nested
round(
value:=3.14159,
places:=2
)
3.14
3
to_int(
round(
value:=3.14159,
places:=2
)
)
Overlay Functions
QGIS Expression Engine comes with a suite of functions for spatial overlays. It allow you to perform spatial joins using expressions.
fid | state_name | code |
1 | Texas | TX |
2 | Illinois | IL |
.. | .. | .. |
states
fid | city_name | population |
1 | Houston | 2099451 |
2 | Dallas | 1304379 |
.. | .. | .. |
cities
fid | state_name | code |
1 | Texas | TX |
2 | Illinois | IL |
.. | .. | .. |
states
overlay_intersects(
layer:='cities',
expression:="city_name"
)
fid | state_name | code |
1 | Texas | TX |
2 | Illinois | IL |
.. | .. | .. |
states
fid | city_name | population |
1 | Houston | 2099451 |
2 | Dallas | 1304379 |
.. | .. | .. |
cities
fid | state_name | code |
1 | Texas | TX |
2 | Illinois | IL |
.. | .. | .. |
states
overlay_intersects(
layer:='cities',
expression:="city_name"
)
['Houston', 'Dallas', 'Austin', ... ]
fid | state_name | code | cities |
1 | Texas | TX | ['Houston', 'Dallas', 'Austin', ... ] |
2 | Illinois | IL | ['Chicago', 'Rockford', 'Champaign', ...] |
.. | .. | .. | |
states
overlay_intersects(
layer:='cities',
expression:="city_name"
)
Geometries
Creating and Manipulating Geometries
Geometry Generator Symbol Layers
Iteration
Iteration Functions
array_foreach(
array(1,2,3), @element + 1
)
[2,3,4]
Tip - Adding Comments
You can add single-line comments to your expressions with - -
-- This expression checks if length is more than 100m
length(@geometry) > 100
Tip - Adding Comments
You can add multi-line comments to your expressions with /* */
/* Calculate azimuth between 2 points
The results are in radians
*/
azimuth(
start_point(@geometry), end_point(@geometry)
)
Resources