Attribute expressions in Context

“Expressions offer a powerful way to manipulate attribute values, geometry and update the symbology of layers.”

An attribute defines the characteristics of an entity. In some cases, the attributes can contain fields that allow users to dynamically change the geometry style, the content or position of the label. Expressions enable users to manipulate the attribute data and compute values on the fly that can be utilized in styling layers, labelling and updating columns.
In this module, we explore how to construct expressions to manipulate data and use them to update values in columns in new columns or existing columns.

You try:

Goal: To learn how to use expressions to manipulate data

  • Add the layers from the exercise data.
  • Toggle edit the roads layer.
  • Use the field calculator to create a column road_length Using the options provided below.
  • Use the $length expression to populate the column you created above. Save the edits.
    *Compare this new column with the length_km column that already exists in the attribute table. Are there any differences?
  • What are the units of the length you calculated ? Update the length of the roads from the current units into km and miles.

Results
You should produce an attribute table that has an extra column road_length

More about expressions

Expressions are extensively used in QGIS and in order for a user to become a power user, they should master them. In QGIS there is an option to write custom expressions to expand on the predefined expressions available after installing QGIS. The type of data determines the kind of expression to use. For example, if the data is numeric users might need to use mathematical expressions whilst if the data is of string type then users might need to use string expression.

Check your knowledge:

  1. What is the best way to describe geometry functions:

    1. Functions that are used to expressions when the data type is numeric
    2. A type of layer available in QGIS
    3. Group of functions which are intended to exclusively operate on the geometry of layers
  2. Which of the following can not be achieved using an expression:

    1. Determine the length of a string
    2. Give statics about a column in the attribute table.
    3. Do an intersection between two layers using the geometry functions.
  3. Can a user update selected records:

    1. True
    2. False

Further reading:

Download the sample data for the lesson.