Filtering attribute data using Forms

“QGIS allows you to filter, sort and manipulate attribute data.”

In the table view, each row in the attribute table displays the attributes of a unique feature in the layer. Selecting a row selects the feature and likewise, selecting a feature in the map canvas (in case of geometry enabled layer) selects the row in the attribute table. If the set of features selected in the map canvas (or attribute table) is changed, then the selection is also updated in the attribute table (or map canvas) accordingly.

With QGIS you can manipulate the table display by filtering and sorting these attributes. You can also use queries to search for features with specific characteristics. In this exercise, we will try out some of these capabilities.

You try:

Goal: To query and filter attributes

  • Load the Countries layer from the exercise data.
  • Open the attribute table for the layer.
  • Enable the form-based filter tool.
  • Filter the table based on the specification in the table below.
  • Scroll down if needed and use the 'Filter Features' button to filter the table.

Check your results

How many records were listed once the filter was applied?

Name Expectation

First Filter field

type

Filter text

Disputed

Filter Case

Case sensitive

Filter Expression

Contain

Second Filter field

pop_est

Filter Value

150000

Filter Expression

Greater than

More about filtering attribute data

When you filter features in the attribute table, the filter only applies to the table – the map canvas will still show all the features. If you use the select features button rather than the filter features button, the matching rows will be selected – you can zoom to the selected features on the map or raise the selected features to the top of the table.

Once there is a table filter in place, you can easily update all the filtered features by placing the table in edit mode and using the quick formula bar. Note that, as of QGIS 2.18, you need to force a refresh of the table to see your updates applied to the table.

There are also ways to filter your layer for both geometries and attribute data in QGIS. For example, you can:

  • Do a subset filter on a layer (via the layer properties dialogue) which will hide any features (including from the attribute table) that do not match the query criteria
  • Use virtual layers to create a view of a subset of layers
  • Use DB manager to query data from a database that meets your specific criteria

You can also use soft approaches to filter features shown in the map canvas - for example using a rule-based renderer to exclude certain features from the map render.

There is another way to filter the records shown in the QGIS table view - you can limit the list of features shown to only those that are also visible in the map canvas by using the filter button on the bottom left corner of the attribute table and setting it to Show features visible on the map.

Check your knowledge:

  1. True or false: Applying a filter in the attribute table will result in only those features being shown on the canvas:

    1. True
    2. False
  2. What is the purpose of filtering the attribute table - choose all that apply:

    1. to quickly view a list of records that match your criteria
    2. to create a selection of features
    3. to be able to quickly update the attributes of a subset of features
  3. Why would you choose filter using form rather than the normal filtering:

    1. Form based filter allows you to combine multiple filters on a combination of columns
    2. Normal filtering only works on a single attribute column even in advanced mode
    3. Normal filtering is not case sensitive.