Advanced editing of attribute data using the Field Calculator

“Interrogating the attribute table allows us to answer questions like : How, where, why? . ”

Field calculator is used for creating new columns and populating them using expressions in one go. A field calculator also allows the creation of virtual fields where values are computed on the fly as certain operations are happening ie calculating area when digitizing. Understanding how expressions work is fundamental in harnessing the full power of the field calculator in manipulating data.

You try:

Goal: To explore the advanced field calculator functions for manipulating data.

  • Load the ne_10m_rivers_lake_centerlines from the exercise data provided.
  • Open the attribute table for the river centerlines and toggle edit for the layer.
  • Observer the values in the name column.
  • Update the name column to replace all Ascii (eg? ) characters with underscores. Use the expression in the table below.
  • Create a new text column called river_code.
  • Use the conditional statement to update the river layer using the criteria listed below.
  • Symbolize the layer using the river_code column.

Name Expectation

Expression

regexp_replace( "name" ,'[^0-9a-zA-Z]+','_')

Condition 1

"featurecla" = 'Lake Centerline' and "name" is not null THEN 'AA'

Condition 2

"featurecla" = 'Lake Centerline' and "name" is null THEN 'AB'

Condition 3

"featurecla" = 'River' and "name" is null THEN 'CC'

Condition 4

"featurecla" = 'River' and "name" is null THEN 'CD'

Condition 5

Else 'ZA'

More about Field calculator in context

The field calculator allows manipulation of the attribute table by performing calculations using existing functions or user-defined python functions. The field calculator allows the creation of new columns based on existing columns ie creating a new column that has the name column in upper case. The results of the expressions can also be written to a virtual field. A virtual field is a field based on an expression calculated on the fly, meaning that its value is automatically updated as soon as the underlying parameter changes. The expression is set once; you no more need to calculate again the field if underlying values change. For example, you may want to use a virtual field if you need area values to be calculated during a digitizing process (creating, merging, splitting features) or to calculate a duration that needs to be updated from time to time. In some instances, users need to update columns using functions that are not listed in the field calculator and in this case they can create their own python functions using the QGIS API or basic python programming ie a function to determine in which UTM Zone particular points lie in.

Check your knowledge:

  1. :

  2. :

Further reading: