Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

4. Queries in QGIS 

4.1. Introduction

QGIS provides a number of different ways of selecting, filtering and querying map layers. It's important to understand the terms that QGIS uses to describe these different techniques - they are summarised in this table.

Action

Result

How

Details

Filter

Display only a subset of the features in a layer (including both map and attribute table), hiding the rest.

Layer > Filter

Write a query to filter features.

Note that applying a filter to the attribute table filters the attribute table, but not the features on the map.

Select

Create a subset of tagged (selected) features from one or more layers, while continuing to display all features. Selected features are highlighted in the map and attribute table. Further actions (e.g. save, processing) can be performed on the selection only.

Click on one of the Select buttons, or use their dropdowns, to make a selection.

Identify

DShow data (attribute values) for a single feature or group of features, and optionally show values in a form for viewing/editing. Attribute data is shown for each feature, along with derived data - e.g. length/area.

Click the Identify features button, then click on a feature.

4.2. Filter

Filter a layer to see only the filtered features - the user will not see any that do not meet the filter conditions, either on the map or in the attribute table.

To apply a filter:

  1. Select the layer to be filtered

  2. Right-click > Filter

  3. In the Query Builder dialog, construct a query to show the features you need

For example, in the screenshot below, wind turbines have been filtered to those where the STATUS field has the value 'Application permitted - no conditions'.

The Query Builder dialog will help you construct a query interactively by selecting attributes, values and operators to build it up.  It is similar but not identical to the Expression Based Filter dialog described below.

To reset the query in order to see all data, click Clear in the Query Builder.

4.3. Select

Selecting features 'tags' them for future use - once selected, a feature is highlighted in the map and in the attribute table. Selecting creates a subset of the data that QGIS can use to work with in other processes - for example, once features have been selected, you can:

  • save the selected features as a new layer

  • run processing tasks on them

  • invert the selection to select all features currently not selected.

The Select tools are grouped together in the Attributes toolbar, and additional tools are in the Attribute Table toolbar, shown in the second row below.

Select features:

  • interactively in the map interface, for example by clicking or selecting by rectangle

  • interactively in the attribute table, by clicking one or more rows

  • by query, from the map or attribute table

To select interactively from the map, click the dropdown (the black triangle) next to the Select features by area or single click button in the toolbar . The menu which appears gives you a number of options:

Select Feature(s)

Select features individually

Select by rectangle

Click Select Feature(s) (it's selected by default), then click on the map to select a single feature at that location. Use Ctrl-click to select multiple features.

You can also click and drag a box over features to select all features that intersect the box.

Select this tool and click on the map to select a single unsafe-one feature at that location

Select Features by Polygon/Freehand/Radius

Use these tools to select features by drawing a polygon, freehand line or radius:

Select by polygon

Select freehand

Select by radius

Select Features by Value or Expression

The tools on the Select Features by Value dropdown allow you to identify a selection by using a query, or by matching values against attributes.

  • Select Features by Value displays a dialog where you can enter values to search for in the data - note that the search is interactive, so as soon as you start typing, matching values appear

  • Select by Expression displays a dialog in which you can build a query by selecting fields, operators and functions. 

Navigate and Remove Selection

Navigate to a selection in two ways:

  • Pan Map to Selection will centre the map on the feature 

  • Zoom to Selection will set the map extents to match those of the feature 

To remove selections:

  • Deselect Features to deselect features from all layers 

4.4. The Attribute Table

The attribute table holds data about all the features in the layer - you can use it to view, edit and query data. Open it from the Attributes toolbar, or by right-click > Open Attribute table from a layer in the layer list. Each row in the attribute table corresponds to a feature on the map, and selection is synchronised between the table and the map - if a feature is selected in the attribute table, it's also selected on the map.

Attribute Table Views

The Show All Features button at the bottom left of the attribute table is a button which has a dropdown with a number of options to control which features appear in the table:

  • Show All Features: Show all features in the data

  • Show Selected Features: Only features selected in the map/table will be shown

  • Show Features Visible on Map: Only features which can be seen in the current map window are shown

  • Show Edited and New Features: During an editing session, this shows features which have been created or edited but not yet saved

  • Field Filter: filter the table by selecting a field and a search value

  • Advanced Filter (Expression): filter the table by opening the Expression based filter dialog

Toggle Attribute Views

There are two possible views of the data within the attribute table - the default tabular or grid view, and the form view. Switch to the form view and back by using the buttons at the bottom right-hand side of the window.

The form view is similar to the form used in Identify Results, but includes a navigation panel on the left for moving between features. This can be more convenient for editing attribute values.

Form view

Select features in the form view of the attribute table by clicking the checkbox to the left of the feature list - once selected, the box for the feature will show yellow. 

Attribute Table Field Filter

Run a simple search in the attribute table by selecting Field Filter from the menu in the bottom left of the attribute table dialog, then select the column name for the search. This is a simpler, single field version of the Select by Values tool described above, and it will show matching values interactively in the same way. To run the search, press Enter.

The attribute table will be filtered to show only the matching features.


Case sensitivity

Values are not case sensitive unless the Case sensitive box is checked, and the search is for a pattern within the string (for example ming will find Birmingham) so you don’t need the exact value.

Datatypes

Here, and in many other field-specific locations, there is an indicator of datatype immediately before the field name. For example, abc means a text field, while 123 means numeric.

Attribute Table Advanced Filter (Expression)

Select Advanced Filter (Expression) from the dropdown at bottom left of the attribute table to open the Expression Based Filter dialog - this is the same one as opened from Select Features by Expression earlier. However the result of applying this expression will only filter features in the attribute table, not on the map.

Queries can be built up in this dialog by selecting fields, operators, values and functions. For example, a simple query might be in the form:

[field] [operator] [value] - e.g. phase_of_education = 'Primary'

The example below builds a simple query to find all all parishes which are 'filler areas' - i.e. where there is no council.

  1. In the Expression Based Filter dialog, navigate to Fields and Values in the central panel and select the relevant field (in this case descript0)

  2. Double-click on the field to bring it into the Expression panel on the left

  3. Click on the = operator in the Expression panel

  4. In the Values panel, click all unique to show all the values of the field which appear in the data (or if there are a large number of values, click 10 samples)

  5. Double-click on the value you want to use for the filter (in this case 'FILLER AREA')

  6. You should now have an expression in place which looks something like the screenshot below - click OK to apply the filter

4.4.1. Operators

The operators below are the most commonly used, but more are available in the dialog.

Operator

Meaning

=

Equal to

<>

 Not equal to

<

Less than

>

Greater than

<=

Less than or equal to

>=

Greater than or equal to

LIKE

Case-sensitive string match

ILIKE

Case-insensitive string match

NOT

Combined with another operator to make it negative

%

Wildcard – include in the search string at the point of uncertainty

AND

Extends queries to apply more than one condition to a search

OR

Extends queries to search for different values and will be returned if either is true

||

Join two values into a string

The Field Calculator

The Field Calculator creates new fields, or amends the values of existing ones, by using expressions. For example, you could create a new field which totals the values of two existing fields.

Open Field Calculator by clicking the button in the attribute table toolbar. 

Choose whether to create a new field or update an existing one, and use the expression builder to specify the contents of the target field - in the example above, the total of all crime types.

As examples, you can use the Field Calculator to:

  • Rename a field: create a new field with the same type and length as the source field, with a new name, and populate the new field with data from the old field

  • Change data type: use a data type conversion (under Conversions) such as to_int to change a data type from string to numeric, for example

  • Derive coordinates: in the Geometry options, the $x and $y functions will return the coordinates of the points in a point layer

4.5. Spatial Selections and Filters

The queries and expressions we have been looking at until now work by using the attribute values of layers rather than their geography. There are a number of options for running spatially-based queries in QGIS, addressing questions such as:

  • Which points fall within this polygon?

  • Which lines cross this boundary?

  • What are the distances between features?

Two approaches are covered here:

  • Selecting features based on location

  • Join attributes by location

Spatial queries

There are many types of spatial queries - use the search box at the left of the information bar to look for others.

Select by Location

This tool creates a new selection of features in a layer, based on a spatial relationship between the layer and another layer (or even the same layer). For example you might want to use it to:

  • select points in a polygon

  • select lines which intersect a polygon

  • select a polygon which contains a specific point.

To select points in a polygon:

  1. Select the polygon you want to make the selection within - in this instance we have selected a parish within which we want to identify crimes

  2. In the search box, search for Select by location, then double-click on it to bring up the dialog

  3. In the Select by location dialog, complete the options in a similar way to the screenshot below

  4. Note that:

    1. The first layer should be the one which contains the things you want selected - e.g. crimes

    2. In the checkboxes for the geometric predicate  select the spatial relationship between the features in the two layers

    3. The second layer is the one you are using to drive the selection - in this case parishes

    4. The Selected features only checkbox specifies that only selected features in the second layer will be used to make the selection - in this case it's the single feature we selected in the first step

  5. Click OK to run the selection - the result will be a map which looks something like this:

  6. Note that only crimes within the selected parish have been selected, and are highlighted

Join Attributes by Location

This tool carries out a similar operation to the one above, but instead of making the result a selection, it adds the attribute values of the second layer to the first, target layer, so that each feature which satisfies the criteria in the spatial analysis takes on the attributes of the feature it is within (in this case). So for example, you could this to add a column to the crimes data, showing the parish that the crime took place in.

  1. As in the previous example, select a polygon you want to make a selection with

  2. In the search box, search for Join attributes by location

  3. Open the dialog, and complete in a similar way to the screenshot below:

  4. When the process is run a new Joined layer will be created and added to the map, comprising the original crime layer with, for each feature, the attributes from the parish it is within

  5. This layer can then be retained as a temporary layer in the project, or saved as a new layer

4.6. Identify Features

The Identify Features tool is used to display the attribute values,  and some derived properties, of a map feature. 

  1. Click the layer of interest in the layer list

  2. Click the Identify Features button - the cursor will change

  3. Click on a map feature.
     

  4. The feature will be highlighted, and the Identify Results panel box will appear, showing the values of all the attributes for the feature

Derived Information

The Derived section in the panel includes measurements derived from the geometry of the feature such as length, area and perimeter (depending on the geometry type).

Multiple Features

If you click close to feature boundaries, multiple features are identified, and these will be listed as separate items in the Identify Results hierarchy. Browse the tree to see the data for each feature.

Feature Attributes

To view the attributes for a feature in a form dialog, click the View feature form button at the top of the Identify Results panel . This makes it easier to view the data, and if the layer is in edit mode, to edit it.

To open the form automatically whenever Identify Results is used, check the Auto open form box at the bottom of the panel.

Identify Features from Multiple Layers

If you click on a location where there are features from multiple layers, you will be able to choose which layers to use in populating the Identify Results panel (including all of them).

To show results for a single selected layer, select Current layer from the Mode dropdown at the bottom of the Identify Results panel.

Copy to clipbboard

The Copy Selected Feature to clipboard button  copies the data from the clicked feature to the clipboard as tab delimited text - you can then paste it to a text editor or directly into a spreadsheet.








  • No labels