Video - Adding Fields and Editing Feature Attributes with the Field Calculator

Catalogue number: Catalogue number: 89200005

Issue number: 2020008

Release date: February 19, 2020

QGIS Demo 8

Adding Fields and Editing Feature Attributes with the Field Calculator - Video transcript

(The Statistics Canada symbol and Canada wordmark appear on screen with the title: "Adding Fields and Editing Feature Attributes with the Field Calculator")

So in today's demo we'll use the Field Calculator to edit fields and feature attributes, including those storing geometric, numeric and text information. The Field Calculator is used to add and update fields or edit attributes of selected features. Like the Select by Expression tool, introduced in the previous demo, it uses expression syntax. Today we'll use the two tools in conjunction to select and update large selections of features.

So let's start by adding some spatial measures to our subset and created datasets.

So we can select them in the Layers Panel, and click the Field Calculator icon on the Attribute Toolbar. Since we are adding a new field, much like when we added the Fields to our AOI layer, we need to specify a field name, the field type as well as provide the parameters for the number of characters it stores. The same drop-downs from the Select by Expression tool are also found in the Field Calculator which we can use to create our expressions. In the Geometry drop-down we can see the general expression syntax, which tends to be the dollar sign followed by the measure of interest. So if we wanted to add coordinate information to a point dataset we could use $x or $y or for our polygons $perimeter and $area. The Preview shows the output, which is calculated in Map Units – making the Projected Coordinate Systems with units in metres applied to these layers, helpful in adding these measures.

To alter the units just apply an appropriate conversion factor, in this case divided by 10000 for area in hectares. We'll also copy the Expression. Now repeating with our AOI layer- we can also opt to Update an Existing Field – selecting the field to update from the drop-down – meaning we can finally populate the area in hectares field we added when we first created the AOI layer – pasting the expression and clicking OK.

It is important to note that these measures are not automatically updated if the geometry of a layer is edited. So if we split a polygon or clipped it we would have to update these fields using the Field Calculator and once again paste in the appropriate expression. We can then Save the Edits and turn the editor off.

Now lets take a look at updating large feature selections using the road_segment_1 layer. First we'll change the projection using the Reproject Layer tool. Much like the Save Vector Layer As… box it is used to transform a layer to a new projection, and can be applied as a batch process to multiple layers. So we'll change the coordinate reference system to UTM Zone 14N and use a temporary file for the output.

Then using our Interactive Selection tool, with our AOI layer highlighted, we'll select the merged Census Division feature, which we'll use to clip the reprojected road segments. So within the tool Reprojected is selected as the Input and the Overlay layer is the AOI. The Overlay layer is restricted to polygon vectors. We'll check Selected Features Only, so that only the road segments overlapping with the selected AOI feature are retained.  This can be used to standardize the extent of analysis for multiple layers, and in this case, reduce processing times with editing the attributes of our road features. I saved the file in our Intro Demo folder, and called it CPRoads for Clipped Projected Roads.

So now we'll use the Select by Expression and Field Calculator sequentially to isolate and update the attributes for large selections of features in the Road Segments layer. So the road classes were interpreted using the CanVEC catalogue and Road Class field shown in the downloading data from the Federal Geospatial Platform demo.  

We'll enter three expressions together and the remaining expressions are found in the video description. The first we'll enter is road classes greater than 309 AND less than 312 – for selecting within a value range - corresponding to Highway features. Clicking select - 10000 features are returned.

"road_class" > 309 AND "road_class" < 312

Since this is the first selection we have to create a New Field, which we'll call Class, specifying it as a Text field type with a length of 50. As seen, when there are selected features by default the Only Update Selected Features box is checked. To update all features we could simply uncheck the box.

Remembering our syntax rules from the Selecting by Attributes demo we need to apply single quotes around text-based entries.

Back in the Select by Expression tool let's populate another class, changing values to greater than 311 and less than 315. 25000 features were selected. Since we have already created our field we will use Update Existing scrolling down to the bottom of the list and selecting our Class field. Our current selection corresponds to local classes.

Now lets create one final selection - changing to road_class = 309, which corresponds with Collector roads. Once again in the Field Calculator, select the field to update and enter the corresponding attribute. The remaining expressions are provided in the video description – which can be used to populate the remainder of the field.

Now we'll use fields in our layer to update the Speed Limit field which is currently empty. The first one is simple entering "is_trans_c" = 11 – meaning is TransCanada Highway is TRUE. In the Calculator click Update and find the Speed_Rest Field. The limit for the TransCanada in Manitoba is 110 kilometres an hour. Recall numbers can be entered as is.

The next few expressions are slightly more complex, since we are using a variety of fields to approximate speed restrictions. So we can use the Official S field, which contains the full name and type of the road, and add LIKE wildcard way to isolate Highways, Freeways and Expressways. But we want to avoid selecting other roads containing WAY - like Stoneway or Wallford Way - so we will also specify of_street_6 which is the Road Type and use the IN operator so to avoid repeating the field for each attribute. We'll scroll through the unique entries and add Highway, comma Freeway. Had we left the Road Segments unclipped we would also need to add Expressway. And finally to avoid overwriting the Speed Limits we've already populated with previous expressions we'll use AND "speed_rest" IS NULL in all subsequent expressions. In the Field Calculator we'll add the corresponding speed limit, defaulting to 100 kilometres an hour.

Now we can remove the components except Speed Rest IS NULL from the previous expression, and use the Official 2 field which provides a general indication of the road settings. Once again we'll use the IN operator and add Unorganized, Reserve and Rural Municipality. Then we'll use the road_class field, specifying greater than 307 and less than 310, to isolate non-urban roads that are collector or arterial. Once the selection is created, we'll enter an average speed limit of 80 kilometres per hour in the Field Calculator.

As with our Class field, the remaining expressions and associated speed limits to update in the Field Calculator are found in the video description, along with an explanation of the expressions.

The last thing I'd like to show is adding two final fields. The first is the length of the road segments in kilometers. I recommend populating both these fields fully prior to applying these calculations to avoid repeating these procedures later, as we are only currently updating the selected features. So applying the skills from earlier in the video the syntax is $length, and divided by 1000 for units in kilometres.

Now we'll add a field called TimeMin, which we will use the Length and Speed Limit fields to calculate. Speed equals distance over time, so time equals distance over speed, dividing our Length KM field by the speed_rest field. Left as-is the output is in hours, so we can multiply by 60 to calculate the time to traverse each road segment in Minutes. Now ensure to Save Edits and Toggle the Editor off.

Congratulations! You should now feel confident using the Field Calculator to add and update fields and edit attributes for large feature selections in vector datasets. You should also feel comfortable applying expression syntax to perform these procedures – such as applying the appropriate operators and syntax for different attributes, and using multiple fields to isolate features by criteria of interest. We'll continue expanding these syntax skills throughout the tutorials. Additionally you should feel comfortable adding geometric, numeric and text attributes, and derive new information using the Calculator.  Apply these skills to datasets of interest to you.

In the next demo we will discuss procedures for visualizing vector data specifically focusing on the Symbology and Labels tabs in the Layer Properties box to visualize different fields.  We will use the CPRoads layer from this tutorial to demonstrate rule-based visualizations.

(Canada wordmark appears.)

Date modified: