Video - Visualizing Vector Data (Part 2): Rule-Based Visualizations and Labelling

Catalogue number: Catalogue number: 89200005

Issue number: 2020010

Release date: November 19, 2020

QGIS Demo 10

Visualizing Vector Data (Part 2) - Rule-Based Visualizations and Labelling - Video transcript

(The Statistics Canada symbol and Canada wordmark appear on screen with the title: "Visualizing Vector Data (Part 2) - Rule-Based Visualizations and Labelling")

Welcome back everyone. So following up from Part 1 of Visualizing Vector Data where we explored the application of symbology styles to different field types.

Today we'll apply rule-based visualizations, including both symbologies and labelling schemes. These styles can be used to assign different visualization parameters to features in a layer based on their attributes, such as using different fields, symbols or formatting for visualization. We'll use the rules to create a scale-dependent visualization of feature classes in the CPRoads layer.

Additionally, we'll discuss the use of available symbology file formats and how to load them to vector datasets. This will enable you to rapidly visualize datasets, including those in other locations or collection periods that share the same parameters, as well as share your visualization styles with others.

We'll conclude with additional labelling procedures, such as developing multi-field and conditional labelling expressions.

Double left click the CPRoads layer to open the Layer Properties box. In the Symbology tab, we'll begin by applying a Categorized style to the Class field created in the Field Calculator demo and click Classify to add them to the tab. The Categorized style is being used to establish the Symbol and Legend Values prior to switching to the Rule-Based style.

We can left-click and drag to reorder classes in the tab. We'll place them in ascending order to ensure that larger classes do not obscure the visualization of smaller classes. With them in Ascending order, we can now select Recreational through Arterial, holding shift between left-clicks, then right-click and select Change Colour – which we'll alter to Black. Repeating with the Ramps and Highways class, we'll alter the colour to Red. And just like the Graduated Symbology applied to grain elevators in the previous demo, we'll adjust the line widths for each class, double-left clicking to open the Symbol Selector. For Lane we'll apply a width of 0.2, Local a width of 0.3 and Collector a width of 0.8. Then we can select Arterial and Ramp together and edit the line widths to 1. Finally for our Highways class we can use a width of 1.5.

So with the visualization and legend values established now we can switch to the rule-based style. This will add a few additional parameters including Rule, Min and Max Scale columns. Select an entry and click on the Pencil icon or double left click it to open the Edit Rule box. Here we'll replace current rules with the original expressions used to create the Class field.And this is so we can apply the visualization not only to the CPRoads layer but to the original Road_Segments layer in Manitoba as well as those in other provinces that have the same visualization parameters. Clicking the Expression box, we'll replace the rule with "road_class" > 316 AND "road_class" < 321 for the Recreational class. And we'll copy the last component of the expression for parametrizing our other classes. Clicking OK. Check the Scale Range box. And for the Minimum scale, beyond which visualization is suspended, select 50,000 from the drop-down. We'll set the Maximum scale to 0, as we do not want to suspend rendering as we zoom in further.

Optionally, we can also single left click to edit rules in-situ within the symbology tab. So Lane classes corresponded to values of 307 OR 321. And we'll enter 100,000 for the minimum scale. I'll enter the remaining expressions and minimum scale values, specified in the video description.

So now we can click Apply and OK and take a look at our dynamic visualization of different road classes. With the current scale slightly greater than 1 in 1 million, all we can see is our highways. But zooming in slightly further, the Collector and Arterial classes appear, and even further the Lane and Local classes.

Now let's select the rules from the Symbology tab, copy using Ctrl C, and then Paste them into the Labels tab after selecting Rule-Based from the drop-down. For both the Symbology and Labels tab we can add new rules using the plus icon and parametrize them in the Edit Rule box. Individual rules can also be suspended by toggling them off or completely removed with the minus icon. We'll remove our Recreational and Ramp classes for labelling. We still need to open the edit rule box for each entry, first to check the Label box feature and specify the field used in labelling. Additionally, we will want to apply smaller minimum scale values for our labels.

So double-left clicking the Lane class, check the Labels box and select "official_s" field from the drop-down. This will label our features by the full road name. Copy the label field, which we'll paste for our other classes. So, we'll increase the text size by 1 in ascending order, so for Lane leave the default size of 10. We'll apply text buffers that match the line widths applied to each class back in the rule-based symbology. And for the Placement, there is a variety of options which vary between vector geometry types. For Local and Lane Classes we'll use Curved – the others using Parallel, and for all classes we'll switch Placement from Above to On Line. We can specify the Maximum Angle between characters at the bottom of the tab. Finally we'll alter the minimum scale to 1 in 50,000 for our Lane class. I'll parametrize the remaining classes and then we'll restart. As noted we can also use the rule-based style to apply different fields in visualization. So, for our Highway class, lets use the Route_Numb field. Change the Font Size to 14. Instead of a text buffer we'll apply a text-background, selecting Ellipse from the Shape drop-down and applying a buffer of 0.5 around the text. Change the fill colour to blue and the outline to red, with a width of 0.25. Finally, as specified earlier change the Placement from Above to On Line. Now let's examine the visualization.

So as we can see, there's many repeating labels, since it's a roads segments file – where each line between junctions is a separate entry in the attribute table. And this results in a messy appearance. Therefore, there are two options – in the Rendering tab, scrolling down to the bottom we can select Merge Connected Lines to Remove Duplicate labels. This will remove most repeating labels. Alternatively we can use the Dissolve Tool and select the "official_s" field, which will combine roads with the same name into one feature. So we'll use the Dissolve tool to ensure that all duplicates are removed. Multiple fields can be specified for Dissolving as needed, such as ensuring that different roads with the same name are not combined.

Rather than re-entering the rules for our Dissolved layers, simply copy and paste the style from the CPRoads layer. Use All Categories to ensure that both the symbology and labels are retained. So this enables rapid visualization between layers so long as they are of the same geometry type and share the same visualization parameters. If we decided we only want to apply labelling or symbology scheme, the visualization parameters can also be specified when pasting.

So toggling the Dissolved layer on and CPRoads off , we can see that all duplicates have been removed – significantly improving the visualization. Zooming in further we can begin to see our curved labels for the Lane and Local classes.

So now let's discuss the symbology styles for saving our visualizations. There are a few file formats available in QGIS with different uses. The two main types for vectors are under the Export options.

The first is the Layer Definition File or .qlr. This format is explicitly tied to the source dataset. So save the file in the same directory as the source layer. And the definition file can then be loaded directly into QGIS. Simply double-left click to load it in to the Layers Panel with the pre-defined visualization parameters, including both the symbology and labels. The same should apply when loading the source layer. For this layer I used the Merge Connected Lines in the Labels Rendering sub-tab. Zooming out we can see that most, but not all duplicates have been removed.

The second format, the QGIS Layer Style File or .qml, is similar. It saves the visualization parameters but is not tied to the source dataset. Thus, it is helpful for sharing visualizations with others, or applying created visualizations to the same dataset in a different location or time period. To load this file go in to the Layer Properties box, click the Style tab and Load Style. The specific visualization parameters can be specified in the Categories box. Then locate and load it from directory. We can copy and paste the style to the Ontario Road Segments file. And now the visualization is applied across Manitoba and Ontario, with the dynamic rendering as we zoom in and out. So this underscores the utility of these visualization files.

Within the Symbology tab we can also save edited or created colour ramps. This format is not tied to any layer. But, saved colour ramps can be accessed from the Color Ramp drop-down. For example, I inverted the Spectral colour ramp and saved it as a Temperature colour ramp with Add to Favorites checked, meaning it's immediately accessible in the first Colour Ramp drop-down. I also saved the Heat Map style from our Grain Elevators layer in the previous demo, with Favourites unchecked, so it's in the expanded All Colour Ramps side-bar.

Now for the final component let's use expression syntax to generate multi-field labels and conditional labelling schemes. For the first we'll select the Grain Elevators layer and open the Field Calculator.

We'll provide the name MFLabel for the field name, change the type to text with a length of 100. Expand the Fields and Values drop-downs to see the available fields. Now we can enter generic text components with single quotes, staring with Oper colon space closed quote for the rail operator. Then use the two vertical dash lines (||) to separate different components of the expression, here adding the Railway field. Adding the separators again, single quote and hit Enter twice, and then type Co – colon - space and close quote, followed by the separators and the Company_Consolidated field. Finally, we'll add a comma and a space enclosed by single quotes, then the separators and the PR field. We can now see an example of the label in the Preview output.

So now open the Layer Properties box, and in the Labels tab select Single Labels and then the MFLabel field at the bottom of the drop-down. Once again we can specify a text-size and a buffer. In the Placement tab, we'll set where the labels should be placed, which as noted varies between geometry types. Cartographic could be used for mapping but may increase rendering times. Here we'll use the Offset from Point and specify the Centre. Based on the spacing in our label, this should place our label above and below the points. For the Rendering tab add a minimum scale of 1 in 500,000, then click OK.

Zooming in beyond the Minimum Scale value, our multi-field labels appear for each elevator – indicating the railway operator, company and abbreviated province. This can be reapplied to any layers, fields and vector geometry types to provide key information in labelling.

Now let's explore conditional labelling, used to label features with certain criteria or specified attributes. Toggle on and open the CPRoads' Layer Properties Box and switch to Single Labels. Click the Expression button beside the Label. And expanding the Conditionals drop-down double-left click CASE, and examine the syntax on the right. So once it's added to the Expression box, we can now specify the criteria or conditions. In general, its CASE WHEN, then you need to specify the conditions, what to do when the conditions are satisified, and what to do otherwise, adding ELSE. Here we'll use the Class field and the IN operator, adding Arterial, Collector and Highway classes to label our major road features. Now we need to specify the labelling field after THEN, selecting route_num from the Fields and Values drop-down. Finally we need to specify what to do with the other features, after ELSE we can add NULL. Now we can click Apply and OK.

Though all of our Highway features are now labelled by their route numbers. However, the Collector and Arterial roads are not labelled since they lack route numbers. So lets add another conditional expression, an IF statement, to the label after THEN. So the format for IF statements is specifying the criteria, then entering what to do when it's true, followed by what to do if it's FALSE separated by commas. So here we'll specify if the Route Number is NOT NULL, label by the Route Number, otherwise use the Speed_Rest field – which we populated in the Field Calculator demo.

So now all of our specified classes are being labelled. This is not a particularly aesthetically pleasing visualization, but does demonstrate that we can use expressions to specify the attributes or conditions for which labels should be generated. Further, we can nest expressions within one another to ensure the specific attributes and label parameters of interest are used. Apply these expression skills to other datasets in specifying the conditions and attributes to use.

In this demo, we covered rule-based symbologies and labelling schemes. We also learned how to copy and paste styles, as well as save and load symbology files to rapidly visualize vector datasets. Finally, we explored labels in greater detail including using expressions to create multi-field and conditional labelling schemes. Apply these skills for parametrizing, saving and sharing vector visualizations. These skills are fundamental to creating logical, easily interpretable visualizations and maps of spatial data. In the next tutorial we'll cover procedures for joining datasets by attributes, including one-to-one and one-to-many joins.

(The words: "For comments or questions about this video, GIS tools or other Statistics Canada products or services, please contact us:" appear on screen.)

(Canada wordmark appears.)

Date modified: