“Expressions are a core component of QGIS. They let you define rules to govern just about every aspect of QGIS behavior.”
In this module, we will introduce the concept of expressions. If you ever created a spreadsheet formula, you should be on familiar ground here. Expressions are one of the most powerful features of QGIS. You can use them to control things like labelling behaviour, symbology behaviour, map composition behaviour and calculate values for attributes.
Goal: Use an expression to scale the symbol size of cities according to their population.
Check your results:
Do bigger cities have smaller markers than smaller ones?
How would you adjust the expression if you wanted to decrease the maximum icon size?
Name | Expectation |
---|---|
Places filter |
"featurecla"= 'Admin-0 capital' |
Style |
Single symbol |
Marker |
Capital |
Size |
scale_linear( pop_max, minimum("pop_max"),maximum("pop_max") , 8, 18) |
The same expression editor dialog is used everywhere that expressions are used, so learn that dialog once and re-use your knowledge everywhere!
The search box will let you quickly filter expressions based on your search term. Want to figure out how to generate a random number? Type rand
into the search box and then view the help text. Almost all expression elements provide simple usage help and examples. Generally, you can just cut and paste these examples into the editor area and then modify the expression according to your needs.
Expression elements starting with a $
are special - they return information about the current record, project etc. For example $area will tell you the area of the current feature.
Variables are also interesting - they are placeholders for specific information such as which version of QGIS you are running. In addition to the numerous pre-defined variables, you can also define your own ones. Variables can be defined in three places:
In the QGIS settings dialog, in the project properties dialog and in a map composition. You can refer to any variable by writing its name prefixed with @
. For example, @qgis_os_name
will be replaced by the name of the currently running Operating System.
Note: At the time of writing, there are also a few places where QGIS currently does not use the standard expression system: layer filter dialog and raster calculator, so watch out for these!
Which expression below will calculate the area of every feature? Use experimentation in the field calculator to verify your answer:
Figure out which of these expression(s) you would use to change the outline colour of a symbol from the places when max_pop is larger then 5 million people. Experiment in the symbol properties panel:
Download the sample data for the lesson.