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.
4.1.1. Apply a Filter
Open the essentials_03_styling project if it’s not open already
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
Double-click on total_crime in the Fields list to bring it into the expression
Click >
Type 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
4.2.1. Select features
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 2015
In the attribute table for the
Use the drop-down at the bottom left of the attribute table to Show Selected Features, and check that your selection is correct - the status bar at the top of the table shows how many are selected
Right-click on the layer in the Layers panel 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.
4.3.1. Identify features
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
Look 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. 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.
4.4.1. Create calculated fields
In the attribute table for crime_bristol_202210 — neighbourhood_crimes, open Field Calculator by clicking the button in the toolbar
Create a new field street_crime, which is a total of asb, public_order, and theft_from_the_person
Use the settings in the screenshot below, and build up the formula by double-clicking on fields in the Fields and Values list
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?
4.5.1. Create spatial queries
In the crime_bristol_202210 — neighbourhood_crimes 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 - there should be four stations
Use Join Attributes by Location to add the attributes from the containing high crime neighbourhood to the station feature
Look at the attribute table for the stations to see the result (check Selected features only)