Updating attribute table using expressions in Context

“The attribute table does not expose the geometry column but expressions can interact with the geometry of features loaded in QGIS.”

An attribute defines the characteristics of an entity. The attribute table view is limited to showing tabular data and excludes showing the geometry column. The geometry is only exposed in expression and users can manipulate it to generate the area, length and do geoprocessing.

In this module, we explore how to update the attribute table based on the spatial relationship of another layer in QGIS.

You try:

Goal: To update attribute table based on the spatial relationship between two layers

  • Add the layers from the exercise data.
  • Toggle edit the province layer.
  • Create a new column based on the requirements below.
  • Update the new column to show the feature count of the number of towns within each province boundary. Use the aggregate expression
  • Save your edits and toggle edit off.
  • Navigate to the label settings and use the new column you populated as the label column.

Todo

  • Update the province layer with a new column called max_pop
  • Use expressions to populate this column indicating which town has the greatest population in each province.
  • Are you able to update the label you used so that it looks like the image above?

Name Expectation

aggregate

aggregate(layer:='cities', aggregate:='count', expression:="PROV", filter:=intersects( $geometry, geometry(@parent) ) )

column_name

count

data_type

integer

More about advanced expressions

Attribute data describes the characteristics of features. Expressions provide a means to manipulate these attributes to derive new information or to update existing data. Geometry expressions allow layer geometry to be manipulated to generate new geometry or to update the existing geometry. The most common expressions allow deriving new information like area, length and perimeter. In QGIS you can use the expression to generate new attributes that are of the different geometry types. From a polygon, you can derive the centroids and store them in the attribute table.

Aggregate expressions allow you to define the relationship between two or more layers in QGIS. Combining aggregate expressions with geometric expressions allow us to analyse the trends that occur across two or more spatial / non-spatial layers.