Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Numberedheadings
number-formatdecimal
skip-headingsHNaN
start-numbering-with4
h1[h1.decimal].
h2[h1.decimal].[h2.upper-latin]
h3
h4
h5
enabledtrue
h6
start-numbering-atH1

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 used 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

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. Open the essentials_03_styling project if it’s not open already

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

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

    1. Double-click on total_crime in the Fields list to bring it into the expression

    2. Click >

    3. Type 200

  4. 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'.

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

Select features

  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 thestation_passenger_entries layer, use Select by Expression to 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 - the status bar at the top of the table shows how many are selected

  5. Right-click on the layer in the Layers panel 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

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 features

  1. Click on crime_bristol_202210 — neighbourhood_crimes, then right-click > Zoom to Layer

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

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

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

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.

Create calculated fields

  1. In the attribute table for crime_bristol_202210 — neighbourhood_crimes, open the Field Calculator by clicking the button in the toolbar

  2. Create a new field street_crime, which is a total of asb, public_order, and theft_from_the_person

  3. Use the settings in the screenshot below, and build up the formula by double-clicking on fields in the Fields and Values list

  4. Click OK to add the new field, then check the attribute table to look at the new values

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?

Create spatial queries

  1. In the crime_bristol_202210 — neighbourhood_crimes layer, select those features where total_crime is greater than 100

  2. Open the Select by Location dialog by searching in the search box (bottom left)

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

  4. Click Run, and look at the results in the railway_naptan layer - there should be four stations

  5. Use Join Attributes by Location to add the attributes from the containing high crime neighbourhood to the station feature

  6. Look at the attribute table for the stations to see the result (check Selected features only)