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

« Previous Version 2 Next »

4. Queries in QGIS 

Queries on layers can be used to filter the data to a smaller subset, select a subset of data for further processing, or simply to interrogate the data. QGIS uses specific terms for different actions:

Action

Result

How

Details

Filter

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

Layer > Filter

Only the filtered data is uses in QGIS

Select

Create a subset of selected features while continuing to display all features - selected features are highlighted in the map and attribute table, and further actions (e.g. save, processing) can then be performed on the selection only

Click on one of the Select buttons, or use their dropdowns

Identify

Show data (attribute values) for a single feature or group of features, along with with derived data - e.g. length/area

Click the Identify features button, then click on a feature

4.1. Filter

Filter a layer to see only the filtered features - any features which don’t meet the filter conditions will not be visible in QGIS.

Apply a Filter

  1. Right-click > Filter on crime_bristol 202210 - neighbourhood crimes

  2. In the Query Builder dialog, use the Fields and Operators panels to construct a query which will show only those neighbourhoods where total crime is over 200

  3. Review the result on the map and in the attribute table

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

4.2. Select

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.

You can 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

Selection

  1. Use the selection tools to create some manual selections on the map, and compare the results in the attribute table for the layer - try the Select Features by Polygon and Select Features by Freehand options

  2. In the wind_turbines_point layer, use the Select Features by Value dialog to select only those features where the YEAR was later than 2015

  3. In the attribute table for the station_passenger_entries layer, Select the stations where the entries/exits in 2020/21 were greater than 5 million

  4. Use the drop-down at the bottom left of the attribute table to Show Selected Features, and check that your selection is correct - in the status bar at the top of the table, you should be showing 53 selected

  5. Right-click on the layer and Export > Save Selected Features as… to save these stations as a new GeoPackage

  6. In the original station_passenger_entries layer, clear the selection, and use the Select by Value option to select only those stations whose name begins with ‘London’, as per the screenshot below:

  7. In the same dialog, use Zoom to Features to navigate to the selected stations

4.3. Identify

Use Identify to click on individual features on the map and get their attribute values, and derived data such as area and length.

Identify

  1. Click on crime_bristol_202210 — neighbourhood_crimes, then right-click > Zoom to Layer, click on the Identify Features button to activate Identify, then click on one of the neighbourhoods - a panel will appear on the right with a list of values

  2. Look at the options in the mini-toolbar at the top of the toolbar - note that you can copy the results to the clipboard

  3. Open up the (Derived) section of the results to see the area and perimeter of the feature

4.4. 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.

Field Calculator

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