Attribute expressions in Context

“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 the diagram, the height of a composer item, select some features, create a virtual field.
In this module, we explore how to construct expressions to manipulate data.

You try:

Goal: To learn how to use expressions to manipulate data

Data: appendix4-projects/attribute_table.qgs

  • Toggle edit the roads layer and select field calculator.
  • Select update existing attribute and choose column "type".
  • Enter the attr_expr and click ok.
  • Save your edits and inspect the changes.
  • Open field calculator and add a column road_lenght. Use the lenght_calc.
  • Save your edits. Compare the new length column with the length_km column. Are they different and if so give reasons?
  • Create a field length_recode (integer) and reclassify the roads into the following categories.
    Lenght_km <100 = 1, length_km >=100 and length < = 150 = 2 and length >150 = 3.
    Hint: Use the Case statement and length_km column.
  • Open the expression editor and select all 'Federal', 'Interstate' roads in 'North America'. Use usa_roads function. NB: Rewrite the above expression in your own ways to achieve the same results.

More about

Expressions are extensively used in QGIS. They can be used to alter existing data and producing new output and selecting a subset of the data. QGIS provides users with the ability to write custom expressions. Expressions are grouped into categories and the geometry functions are a special type which allows a user to manipulate the geometry of a feature and return new outputs.

Check your knowledge:

  1. What is the best way to describe geometry functions:
    1. A group of functions which help to find the area of point features
    2. Functions which only work when shapefile are used
    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. Finding the current date and time
    2. Evaluating the length of a column containing strings
    3. Do an intersection between two layers using the geometry functions
  3. Can a user update selected records:
    1. True
    2. False