Video - Selecting Features by Attributes in QGIS

Catalogue number: Catalogue number: 89200005

Issue number: 2020007

Release date: February 18, 2020

QGIS Demo 7

Selecting Features by Attributes in QGIS - Video transcript

(The Statistics Canada symbol and Canada wordmark appear on screen with the title: "Selecting Features by Attributes in QGIS")

Hello everyone. Today we'll introduce the attribute-based selection tools, which are used to select features with common entries like classes, categories or value ranges within specified fields. They help select and sub-set data by specific criteria, whereas the interactive selection tools explored earlier help select by areas of interest. Specifically we'll cover: The Select by Value and Expression tools, covering general Expression syntax for the latter. Then we'll export our selection to a new layer, demonstrating how to subset datasets by attributes of interest.

So the attribute selection tools are found beside the interactive tools on the Attribute Toolbar and also default for the selected layer in the Layers panel.

Let's start with the Select by Value tool using the Grain Elevators layer. This tool is the easier of the two to use as it does not require expression syntax, but is best suited to creating simple selections.

So, fields are listed on the left, attributes of interest can be specified in the centre and the operators applied to create the selection are chosen from the drop-downs on the right.

We'll start with a simple selection isolating grain elevators in British Columbia – with 13 returned features.

We could add additional criteria as desired, such as specifying only elevators with railways operated by CN, that are Terminal elevators, with a capacity in metric tons greater than 50000. Now only 5 features are selected.

If we want to use a field more than once, such as selecting features within or outside a value range we can remove the other parameters, and re-select, then simply change the value and operator and expand the selection options by clicking the drop-down – and in this case clicking Add to Current Selection. So, these additional selection options facilitate isolating features that match your criteria of interest.

However, the Select by Expression tool provides much more flexibility in using multiple criteria or repeating a specific field. So, let's explore the tool now using the Census Subdivision layer. Expressions are written in an SQL-like syntax and are used in multiple tools. Don't worry if you have no previous experience – we'll cover the main rules as we work through some examples.

So expressions are written on the left-hand side of the tool, while the centre column can be used to help construct queries. If we click a specific function of interest, the format, components and a sample of the expression are provided on the right.

Expanding the Fields and Values drop-down we can select a field from our dataset, and click All Unique to return the distinct attributes from within that Field. To add them to the expression we can just double left-click. So fields are double-quoted and text-based attributes are single quoted within expression syntax. Scrolling down to the Operators drop-down we can select the appropriate operator – in this case LIKE for text-based entries. As you get more comfortable you can begin to write these expressions from the keyboard.

So, let's explore some additional expressions using the Census Division layer.

So we could use a Wildcard to isolate features with some overlap in their attributes. The % sign is a wildcard meaning any characters of any length - in this case returning all provinces and territories beginning with the letter N.

If we wanted to define the criteria that is not of interest, which is sometimes easier than specifying all the criteria that are - we could just add NOT in front of LIKE, which toggles our selected features. In this case we could have also used the Invert Feature Selection for the same results.

The equivalent of these operators for numeric attributes would be the equal sign (=) and exclamation mark equal sign (!=) for not equals.

So, now let's explore numeric-based expressions using the Unique Census Division identifier field, such as selecting features within or outside of specified ranges – in this case greater than 2000 and less than 4000.

So as you can see, numbers can be entered as-is and the Field Name is repeated for each expression component even when it is the same field. So here we selected all divisions within Ontario and Québec.

Now let's switch the operator to isolate features outside of the range, similar to the selection we created with the grain elevators at the beginning of the demo. No features are selected, and this is because the unique identifier cannot simultaneously be less than 2000 and greater than 4000. In this case we would need to use the OR operator, which is used to select outside of value ranges or additional criteria that are not inherently mutually inclusive. So, think critically about the applied operator and its influence on which features are returned.

We can also use brackets to compartmentalize different components of an expression, such as combining the AND and OR operators when creating more advanced expressions. So, we'll switch 4000 to 5000 and add another component specifying AND less than 6000. Here we've returned the divisions on the Atlantic and Pacific Coasts.

Finally we can also include different fields within the same expression. In this case adding Census Division Name like Division wildcard, which will return any Census Divisions whose names begin with the word Division.

So as you can see the Select by Expression tool offers a lot more flexibility and capabilities in creating advanced feature selections, enabling the repeated use of a specific field or multiple fields and attributes being incorporated when creating the selection.

Now, the final thing I'd like to discuss is exporting our selection in our Census Subdivision layer to a new layer - also known as sub-setting. To export our selected features, we can just right-click, go to Export and – hit Save Selected Features As. This checks the Save Only Selected box within the Save Vector Layer as box. Sub-setting helps remove peripheral features - reducing storage space and processing times. The Save Layer As box can also be used to permanently save temporary layers, change the coordinate reference system or the file format of a vector.

Here we'll change the coordinate reference system to UTM Zone 14, the same we used when creating our AOI polygon in the previous demo.

We'll also provide the output filename and directory. Using a distinctive file naming scheme can help organize and quickly find files. Use a scheme that's most intuitive for you. My preference is to apply prefixes to distinguish processes applied to datasets, as they are listed alphabetically in the Browser panel. Here, I'll call the subset layer .pmb-csd for projected Manitoba census subdivisions.

Click OK. And this will save our subset dataset for future use and also load the layer into the Layers Panel.

Congratulations! You've learned the skills to select features by criteria of interest and export them to a new layer. You should now feel confident using the drop-downs to help construct expressions and applying the syntax to different field types. We'll advance these skills in the following demo, using the expressions in the field calculator to add and update fields, and in conjunction with the Select by Expression tool update the attributes for large feature selections.

(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: