Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Numberedheadings
h3, h4
number-formatdecimal
skip-headingsHNaN
start-numbering-with4
h1[h1.decimal].start-numbering-with4
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

Selection

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 -

in
  1. the status bar at the top of the table

, you should be showing 53
  1. shows how many are selected

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

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

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

, click
  1. 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

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

Create calculated fields

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

attribute table
  1. toolbar

. Choose whether to create Derive coordinates: in the Geometry options, the $x and $y functions will return the coordinates of the points in a point layer
  1. 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

    1. street_crime, which is a total of asb, public_order, and theft_from_the_person

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

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

    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 Spatial queries answer 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:

    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

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

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

  • 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

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

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

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

  • In the search box, search for Join attributes by location

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

    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

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

  • Identify Features

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

  • Click the layer of interest in the layer list

  • Click the Identify Features button - the cursor will changeClick on a map feature.
     
  • 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.

    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)