Based on layer data and prebuilt or user defined functions, Expressions offer a powerful way to manipulate attribute value, geometry and variables in order to dynamically change the geometry style, the content or position of the label, the value for diagram, the height of a layout item, select some features, create virtual field, …
A list of the default functions and variables for writing expressions can be found at List of functions, with detailed information and examples.
13.1.1. The Expression string builder
Main dialog to build expressions, the Expression string builder is available from many parts in QGIS and, can particularly be accessed when:
selecting features with the Select By Expression… tool;
editing attributes with e.g. the Field calculator tool;
manipulating symbology, label or layout item parameters with the Data defined override tool (see Data defined override setup);
building a geometry generator symbol layer;
doing some geoprocessing.
The Expression builder dialog offers access to the:
Expression tab which, thanks to a list of predefined functions, helps to write and check the expression to use;
Function Editor tab which helps to extend the list of functions by creating custom ones.
126.96.36.199. The Interface
The Expression tab provides the main interface to write expressions using functions, layer fields and values. It contains the following widgets:
An expression editor area for typing or pasting expressions. Autocompletion is available to speed expression writing:
Corresponding variables, function names and field names to the input text are shown below: use the Up and Down arrows to browse the items and press Tab to insert in the expression or simply click on the wished item.
Function parameters are shown while filling them.
QGIS also checks the expression rightness and highlights all the errors using:
Underline: for unknown functions, wrong or invalid arguments;
Marker: for every other error (eg, missing parenthesis, unexpected character) at a single location.
Document your expression with comments
When using complex expression, it is good practice to add text either as a multiline comment or inline comments to help you remember.
/* Labels each region with its highest (in altitude) airport(s) and altitude, eg 'AMBLER : 264m' for the 'Northwest Artic' region */ with_variable( 'airport_alti', -- stores the highest altitude of the region aggregate( 'airports', 'max', "ELEV", -- the field containing the altitude -- and limit the airports to the region they are within filter := within( $geometry, geometry( @parent ) ) ), aggregate( -- finds airports at the same altitude in the region 'airports', 'concatenate', "NAME", filter := within( $geometry, geometry( @parent ) ) and "ELEV" = @airport_alti ) || ' : ' || @airport_alti || 'm' -- using || allows regions without airports to be skipped )
Above the expression editor, a set of tools helps you:
Create and manage user expressions
Under the expression editor, you find:
A set of basic operators to help you build the expression
An indication of the expected format of output when you are data-defining feature properties
A live Output preview of the expression (up to 60 characters), evaluated on the first feature of the Layer by default. To view output preview text exceeding 60 characters, you can hover your cursor over the text to display a tooltip pop-up containing the entire output preview. To copy the output preview text onto your clipboard, right-click on the output preview text and select Copy Expression Value.
You can browse and evaluate other features of the layer using the Feature combobox (the values are taken from the display name property of the layer).
In case of error, it indicates it and you can access the details with the provided hyperlink.
A function selector displays the list of functions, variables, fields… organized in groups. A search box is available to filter the list and quickly find a particular function or field. Double-clicking an item adds it to the expression editor.
A help panel displays help for each selected item in the function selector.
Press Ctrl+Click when hovering a function name in an expression to automatically display its help in the dialog.
A field’s values widget shown when a field is selected in the function selector helps to fetch features attributes:
Look for a particular field value
Display the list of All Unique or 10 Samples values. Also available from right-click.
When the field is mapped with another layer or a set of values, i.e. if the field widget is of RelationReference, ValueRelation or ValueMap type, it’s possible to list all the values of the mapped field (from the referenced layer, table or list). Moreover, you can filter this list to Only show values in use in the current field.
Double-clicking a field value in the widget adds it to the expression editor.
The right panel, showing functions help or field values, can be collapsed (invisible) in the dialog. Press the Show Values or Show Help button to get it back.
188.8.131.52. Writing an expression
QGIS expressions are used to select features or set values. Writing an expression in QGIS follows some rules:
The dialog defines the context: if you are used to SQL, you probably know queries of the type select features from layer where condition or update layer set field = new_value where condition. A QGIS expression also needs all these information but the tool you use to open the expression builder dialog provides parts of them. For example, giving a layer (
buildings) with a field (
pressing the Select by expression tool means that you want to “select features from buildings”. The condition is the only information you need to provide in the expression text widget, e.g. type
"height" > 20to select buildings that are higher than 20.
with this selection made, pressing the Field calculator button and choosing “height” as Update existing field, you already provide the command “update buildings set height = ??? where height > 20”. The only remaining bits you have to provide in this case is the new value, e.g. just enter
50in the expression editor textbox to set the height of the previously selected buildings.
Pay attention to quotes: single quotes return a literal, so a text placed between single quotes (
'145') is interpreted as a string. Double quotes will give you the value of that text so use them for fields (
"myfield"). Fields can also be used without quotes (
myfield). No quotes for numbers (
Functions normally take as argument a string for field name. Do:
attribute( @atlas_feature, 'height' ) -- returns the value stored in the "height" attribute of the current atlas feature
attribute( @atlas_feature, "height" ) -- fetches the value of the attribute named "height" (e.g. 100), and use that value as a field -- from which to return the atlas feature value. Probably wrong as a field named "100" may not exist.
Use named parameters to ease expression reading
Some functions require many parameters to be set. The expression engine supports the
use of named parameters. This means that instead of writing the cryptic expression
clamp( 1, 2, 9), you can use
clamp( min:=1, value:=2, max:=9). This also allows
arguments to be switched, e.g.
clamp( value:=2, max:=9, min:=1). Using named parameters
helps clarify what the arguments for an expression function refer to, which is helpful
when you are trying to interpret an expression later!
184.108.40.206. Some use cases of expressions
From the Field Calculator, calculate a “pop_density” field using the existing “total_pop” and “area_km2” fields:
"total_pop" / "area_km2"
Label or categorize features based on their area:
CASE WHEN $area > 10 000 THEN 'Larger' ELSE 'Smaller' END
Update the field “density_level” with categories according to the “pop_density” values:
CASE WHEN "pop_density" < 50 THEN 'Low population density' WHEN "pop_density" >= 50 and "pop_density" < 150 THEN 'Medium population density' WHEN "pop_density" >= 150 THEN 'High population density' END
Apply a categorized style to all the features according to whether their average house price is smaller or higher than 10000€ per square metre:
"price_m2" > 10000
Using the “Select By Expression…” tool, select all the features representing areas of “High population density” and whose average house price is higher than 10000€ per square metre:
"density_level" = 'High population density' and "price_m2" > 10000
The previous expression could also be used to define which features to label or show on the map.
Create a different symbol (type) for the layer, using the geometry generator:
point_on_surface( $geometry )
Given a point feature, generate a closed line (using
make_line) around its geometry:
make_line( -- using an array of points placed around the original array_foreach( -- list of angles for placing the projected points (every 90°) array:=generate_series( 0, 360, 90 ), -- translate the point 20 units in the given direction (angle) expression:=project( $geometry, distance:=20, azimuth:=radians( @element ) ) ) )
In a print layout label, display the name of the “airports” features that are within the layout “Map 1” item:
with_variable( 'extent', map_get( item_variables( 'Map 1' ), 'map_extent' ), aggregate( 'airports', 'concatenate', "NAME", intersects( $geometry, @extent ), ' ,' ) )
220.127.116.11. Saving Expressions
Using the Add current expression to user expressions button
above the expression editor frame, you can save important expressions you want
to have quick access to. These are available from the User expressions group
in the middle panel. They are saved under the user profile
<userprofile>/QGIS/QGIS3.ini file) and available in all expression
dialogs inside all projects of the current user profile.
A set of tools available above the expression editor frame helps you manage the user expressions:
Add the current expression to user expressions: store the expression in the user profile. A label and a help text can be added for easy identification.
Edit selected expression from user expressions, as well as their help and label
Import user expressions from a
.jsonfile into the active user profile folder
Export user expressions as a
.jsonfile; all the user expressions in the user profile
QGIS3.inifile are shared
13.1.2. Function Editor
With the Function Editor tab, you are able to write your own functions in Python language. This provides a handy and comfortable way to address particular needs that would not be covered by the predefined functions.
To create a new function:
Enter a name to use in the form that pops up and press OK.
A new item of the name you provide is added in the left panel of the Function Editor tab; this is a Python
.pyfile based on QGIS template file and stored in the
/python/expressionsfolder under the active user profile directory.
The right panel displays the content of the file: a python script template. Update the code and its help according to your needs.
Press the Save and Load Functions button. The function you wrote is added to the functions tree in the Expression tab, by default under the
Enjoy your new function.
If the function requires improvements, enable the Function Editor tab, do the changes and press again the Save and Load Functions button to make them available in the file, hence in any expression tab.
Custom Python functions are stored under the user profile directory, meaning that at
each QGIS startup, it will auto load all the functions defined with the current user
profile. Be aware that new functions are only saved in the
folder and not in the project file.
If you share a project that uses one of your custom functions you will need to also
.py file in the
To delete a custom function:
Enable the Function Editor tab
Select the function in the list
Press the Remove selected function. The function is removed from the list and the corresponding
.pyfile deleted from the user profile folder.
Here’s a short example on how to create your own
my_sum function that
will operate with two values.
from qgis.core import * from qgis.gui import * @qgsfunction(args='auto', group='Custom') def my_sum(value1, value2, feature, parent): """ Calculates the sum of the two parameters value1 and value2. <h2>Example usage:</h2> <ul> <li>my_sum(5, 8) -> 13</li> <li>my_sum("field1", "field2") -> 42</li> </ul> """ return value1 + value2
@qgsfunction decorator accepts the following arguments:
args: the number of arguments. When using the
args='auto'argument the number of function arguments required will be calculated by the number of arguments the function has been defined with in Python (minus 2 -
args = -1, any number of arguments are accepted.
groupargument indicates the group in which the function should be listed in the Expression dialog.
usesgeometry=Trueif the expression requires access to the features geometry. By default
handlesnull=Trueif the expression has custom handling for NULL values. If
False(default), the result will always be NULL as soon as any parameter is NULL.
referenced_columns=[list]: An array of attribute names that are required to the function. Defaults to
The function itself allows following arguments:
any number and type of parameters you want to pass to your function, set before the following arguments.
feature: the current feature
context: If there is an argument called
contextfound at the last position, this variable will contain a
QgsExpressionContextobject, that gives access to various additional information like expression variables. E.g.
context.variable( 'layer_id' )
The previous example function can then be used in expressions:
Further information about creating Python code can be found in the PyQGIS Developer Cookbook.