Using expressions to drive symbology

“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.

You try:

Goal: Use an expression to scale the symbol size of cities according to their population.

  • Start a new project, set the CRS to 4326.
  • Load the 10m populated places layer.
  • Filter/subset the layer as per the table below.
  • Edit the data-defined property for the symbol size as per the size property in the table below.
  • Use labels and symbology to approximate the map in the image.

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)

More about expressions in symbology

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!

Check your knowledge:

  1. Which expression below will calculate the area of every feature? Use experimentation in the field calculator to verify your answer:

    1. @area
    2. area = $area
    3. $area
  2. 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:

    1. CASE WHEN pop_max > 5000000 THEN color_rgb(255,0,0) ELSE color_rgb(0,0,0) END
    2. IF(pop_max > 5000000, color_rgb(255,0,0), color_rgb(0,0,0))
    3. if pop_max is > 5000000 then red else black