1 of 32

Ujaval Gandhi

Spatial Thoughts

ujaval@spatialthoughts.com

Learn QGIS expressions from the ground up.

QGIS Expressions Masterclass

2 of 32

QGIS Expressions Masterclass

This workshop will teach you the basics of QGIS expression syntax and how to use them to solve complex problems.

  • Introduction to QGIS Expressions
  • Operators and Conditionals
  • Functions
  • Geometries
  • Iteration

Class materials and data package available at https://courses.spatialthoughts.com/qgis-expressions.html

3 of 32

Spatial Thoughts

  • A learning platform for modern geospatial technologies.
  • Free and open learning content for QGIS, Python, GDAL and Google Earth Engine.
    • Learning materials used by 1 million+ users every year.
    • All material is licensed under CC-BY
  • Online academy for cohort-based instructor-led classes.
    • Trained participants from over 150 countries.

✅ QGIS.org certified training provider

✅ QGIS.org sustaining member

4 of 32

Introduction

  • Background in GIS and Remote Sensing
    • Intern at Indian Institute of Remote Sensing (IIRS), Dehradun, India
    • MS in Geospatial Information Engineering from University of Wisconsin - Madison, USA
  • 15 years of professional experience
    • One of the early employees at Google Inc.
      • Moved to India in 2006 and established the maps team
      • Migrated internal teams to QGIS
      • Led the GIS and Aerial Imagery team in India from 2007-2019
    • Developed expertise in Google Earth Engine and trained 1000+ scientists and researchers across India
  • Presently
    • Left Google in 2020 to work on startup Spatial Thoughts.
    • Building open-source content and helping people master advanced geospatial skills.

Ujaval Gandhi

5 of 32

Introduction

  • Masters in Remote Sensing
  • Interest in Automating Workflows
  • Training Associate
    • Class logistics
    • Project support

Vigna Purohit

6 of 32

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.

7 of 32

Where can you use expressions?

You can use expressions throughout QGIS

  • Selecting features
  • Editing or creating attributes
  • Manipulating symbology, label or layout item parameters with data-defined overrides
  • Building a geometry generator symbol layer
  • Geoprocessing

8 of 32

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.

9 of 32

Anatomy of an expression

length(@geometry) > 100

false

function

variable

operator

value

result

evaluation

10 of 32

Operators and Conditionals

11 of 32

Operators

Operators are used to describe the action to be performed in the expression. QGIS expressions use operators derived from SQL.

  • Arithmetic Operators ( + - / * % )
  • Comparison Operators (= < > <> … )
  • Logical Operators (AND OR LIKE NOT IN IS … )

12 of 32

Conditionals

Conditionals are used for making decisions based on certain conditions directly within the expression.

  • IF: Tests a condition and returns a results depending whether the condition is true or false
  • CASE: Test multiple conditions and return the result for the first matching condition
  • COALESCE: Returns the first non-null value from the list

13 of 32

Functions

14 of 32

What are functions?

  • Functions take input parameters, process the data, and return a result.
  • QGIS comes with many built-in functions - from simple things like mathematical operations to advanced functions for fuzzy text matching.

15 of 32

Function name

Input parameter

Input parameter

16 of 32

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

17 of 32

Functions can be nested

round(

value:=3.14159,

places:=2

)

3.14

3

to_int(

round(

value:=3.14159,

places:=2

)

)

18 of 32

Overlay Functions

QGIS Expression Engine comes with a suite of functions for spatial overlays. It allow you to perform spatial joins using expressions.

  • overlay_intersects()
  • overlay_within()
  • overlay_touches()
  • overlay_nearest()

19 of 32

fid

state_name

code

1

Texas

TX

2

Illinois

IL

..

..

..

states

fid

city_name

population

1

Houston

2099451

2

Dallas

1304379

..

..

..

cities

20 of 32

fid

state_name

code

1

Texas

TX

2

Illinois

IL

..

..

..

states

overlay_intersects(

layer:='cities',

expression:="city_name"

)

21 of 32

fid

state_name

code

1

Texas

TX

2

Illinois

IL

..

..

..

states

fid

city_name

population

1

Houston

2099451

2

Dallas

1304379

..

..

..

cities

22 of 32

fid

state_name

code

1

Texas

TX

2

Illinois

IL

..

..

..

states

overlay_intersects(

layer:='cities',

expression:="city_name"

)

['Houston', 'Dallas', 'Austin', ... ]

23 of 32

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"

)

24 of 32

Geometries

25 of 32

Creating and Manipulating Geometries

  • The QGIS expression engine comes with a large number of functions that can be used to create, update, transform geometries of features.
  • The @geometry variable to get access to the geometry of the feature.

26 of 32

Geometry Generator Symbol Layers

  • Use expressions to create new points, lines or polygons on the fly without changing the data source or creating new layers.
  • The resulting geometry is rendered using QGIS symbology.
  • Ideal for creating different visualizations from one or more data layers.

27 of 32

Iteration

28 of 32

Iteration Functions

  • Array functions allow you to run expressions on each item in a list.
  • The @element is used to refer to the current item in the list.

array_foreach(

array(1,2,3), @element + 1

)

[2,3,4]

29 of 32

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

30 of 32

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)

)

31 of 32

32 of 32

Resources

  • QGIS Documentation [Expressions]
  • Advanced QGIS Expressions [Course Module]
  • Applications of QGIS Expressions [Articles]
  • Geometry Generator Examples by Michel Stuyts [GitHub]