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
Right-click > Filter on crime_bristol 202210 - neighbourhood crimes
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
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
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
In the
wind_turbines_point layer
, use the Select Features by Value dialog to select only those features where the YEAR was later than 2015In the attribute table for the
station_passenger_entries
layer, Select the stations where the entries/exits in 2020/21 were greater than 5 millionUse 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
Right-click on the layer and Export > Save Selected Features as… to save these stations as a new GeoPackage
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: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
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 valuesLook at the options in the mini-toolbar at the top of the toolbar - note that you can copy the results to the clipboard
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
In the attribute table for
crime_bristol_202210 — neighbourhood_crimes,
open Field Calculator by clicking the button in the toolbarCreate a new field street_crime, which is a total of asb, public_order, and theft_from_the_person - your settings will need to match the screenshot below
Click OK to add the new field, then check the attribute table to look at the new values
4.5. Spatial Selections and Filters
Spatial queries answer questions such as:
Which points fall within this polygon?
Which lines cross this boundary?
What are the distances between features?
Spatial queries
In the layer, select those features where total crime is greater than 100
Open the Select by Location dialog by searching in the search box (bottom left)
Select the railway stations which fall within the high crime neighbourhoods, using the settings in the screenshot below - note you will need to have Selected features only checked
Click Run, and look at the results in the
railway_naptan
layer to check you have the correct result - there should be four stationsUse Join Attributes by Location to add the attributes from the containing high crime neighbourhood to the station feature - when you are done, look at the attribute table for the stations to see the result (again, check Selected features only)