The QGIS logo

Making digital maps at home
Using 'Select by Expression' - BGS Peat and Alluvium example

Introduction

Many maps are often made up by combining layers of data from a variety of sources, and then selecting those parts which we want to display in our map.

In this case we have a single layer from the British Geological Survey which contained over 10,000 different surface deposit polygons in its full country wide form. After clipping it to give us East Anglia on its own, it still contains over 800 polygons, divided into 14 classes of surface deposits. We now wish to produce one layer which contains only those polygons showing alluvium or peat.

We can examine the data by opening the attribute table. Do this by right clicking on the layer name, in this case, 'digmap625_eastanglia', and selecting 'Open attribute table'. We can see that the column headed 'LEX' contains the code 'ALV' where there is alluvium, and 'PEAT' where there is peat.

The map below shows the layer for East Anglia with all the styles turned on.


Using Selections

There are several ways in which features can be selected in QGIS. One way is to 'select features by single click,' where the cursor picks out the features on the map and the user clicks to select them. We will see an example of using that method in a later demonstration.

For this example we will use the 'Select by Expression' tool, and work with the layer's attribute table. Right click on the layer name and select 'Open attribute table'. From the row of icons at the top of the opened attribute table, hover over each one until you find, 'Select features using an expression.' (It is probably the 4th from the left sporting a large Epsilon symbol.) Click on it.

Constructing the expression

There are three panels visible in the resulting Select by expression dialogue. You can, if you are confident, enter the expression free form by yourself. However, I prefer to use the more long winded, but reliable, expression builder in the centre panel. Expand the 'Fields and Values' menu by clicking on the adjacent tiny '+' symbol. This displays all the fields in the attribute table. Highlight 'LEX' and double click on it. You should see the field "LEX" appear in the left panel within double quote marks.

Similarly now expand the 'Operators' menu, find the operator 'IS' and double click to select it. The left panel now reads, ' "LEX" IS '

Return to 'Fields and values' and highlight by a single click 'LEX' again. This will result in a new panel opening.

Now turn to the right hand panel. The lower of the two is marked 'Values', and below it is 'Load values', so click on 'all unique' to load the field values in the column 'LEX' that you highlighted. Locate 'ALV' and double click to load it.

Return to the 'Operators' menu and select 'OR' and double click to include it in our expression.

Now we repeat the first part of the expression, so select 'Fields and values' entry 'LEX' and double click again. The select 'Operators' and double click on 'IS' again. Back to 'Fields and values' and select 'LEX' again. Load 'All unique' field values yet again, and double click 'PEAT' as before.

Your expression should end up looking like this:- "LEX" IS 'ALV' OR "LEX" IS 'PEAT'

Once you get the idea it becomes much quicker to do it than to describe it.

Note that the boolean logic in use means that we cannot use common spoken expressions. We might well say in conversation, "Lex is alv or peat", but here we must use the construction given.

Finally click on the 'Select' button, and all alluvium and peat polygons are selected. Now 'Close' the Expression builder. Back in our Attribute Table we can see the correct rows are highlighted.

Create the new layer of peat and alluvium

Minimise the attribute table (do NOT close it), to get it out of the way. On the main QGIS map we will see the appropriate features also highlighted in yellow.

Right click on the layer name to open a dialogue, and select 'Save as...'. A new dialogue will open, headed 'Save vector layer as...'. We have several important entries to make. The first and most crucial entry is to ensure that an 'x' is placed in the box, 'Save only selected features.'

Next we want to ensure that the Co-ordinate Reference System is set to the Ordnance Survey grid. You want the entry to read:- Selected CRS (EPSG:27700, OSGB 1936 / British National Grid), so select it from the drop down menu.

(If it is not there, then click on the globe icon, and enter 27700 in the filter box, and then click to select OSGB 1936.... entry that should appear.)

Now we need to browse to the location to save our new layer, and when we are happy, give it a name such as 'eastangliapeatetc'. This will now appear in the 'Save as ' box and you can click OK. The result should load after a few seconds wait.

Turn off the original layer and the result will look like this, but with a different random colour.


Using styling to replace the QGIS random colours

Now we want to replace the random single fill colour with one of our own. We also wish to remove the boundary lines between the two types of deposit in order to make them appear as one continuous area.

Double click on the layer name to open the Layer Properties dialoge. Select the 'Style' tab and click on the any colour you desire to use. I have selected the pale blue, labelled as 'Water' in the row of available fills.

Now click on the small blue box labelled 'Simple fill' and a new dialogue panel opens with a number of further options.

Locate the 'Border style' options and open the drop down list attached to the box labelled 'Solid line'. Select 'No pen' as the border style and click on 'Apply'. The dividing boundary should pretty well disappear. We now have a peat and alluvium layer ready to be used in our topographic map in another section of these mapping examples.



Sources: All maps prepared using Ordnance Survey Opendata, except for Surface Geology -
Reproduced with the permission of the British Geological Survey ŠNERC. All rights Reserved


Go to DIY digital mapping Homepage This page created 9th October 2015
Last updated 9th October 2015.
Go to Main Home Page