Continuing on from the previous tutorial:-
Return to QGIS. Add the westminster_const_region.shp file if necessary
- Press the Add Delimitated Text file button, and select the .csv export of the cleansed electoral data
- The two options I changed from the default settings are:-
- First record contains field names
- No geometry (attribute only table)
Step 3 – Joining the data
Joining the polygons in westminster_const_region.shp to the data imported from the Results_Cleansed spreadsheet will allow the data to be presented in a spatial and visual format which will be much easier to interpret, allow for spatial analysis and also give the viewer an idea of the geographic spread. Using QGIS’ Join function will hopefully save a lot of copying and pasting!
Right click on westminster_const_region.shp and select Properties to open the Properties dialog
- Select the Joins button from the left panel
- Join Layer – the layer that you want to join to
- Join Field – the field that you want to join to
- Target Field – the field in this layer that contains the matching data
The join will now appear in the layer’s Joins list:-
The attribute table will now show the combined data for both layers:-
This data can now be used to create a thematic map that colours each constituency according to party that won the seat in 2010.
I won’t go through all the steps of creating a thematic map as an earlier tutorial does this.
I’ve used the same colours that the different parties in the UK use:-
The thematic map shows the results across the entire UK. It is easy to identify patterns in the result, for example
- The Liberal Democrats mostly won seats in Scotland, the North East, Wales and South West.
- There is strong Labour support in South West Scotland, North West England, West Midlands, South Wales, London, Liverpool and Manchester.
- The Conservative support covers much of the rest of England, especially South East England, excluding London.
I would like to show you how to use QGIS to combine different skills to
- Import data from Excel or other spreadsheets
- Analyse the data
- Present the results as a thematic map
- Use a feature subset to hide superfluous data
There is a year to go to the next general election. Under the British electoral system, the country is divided into 660 constituencies. The MP for each constituency is elected using the First Past the Post system, where the candidate with the most votes is chosen as MP for that constituency. The party that has the most MPs elected wins the election, and the right to form the nest government.
Parties concentrate their resources on the constituencies that they are most likely to win or lose. These are usually the ones where majority in the previous election was closest.
This project will use QGIS to join fields between OS Boundary line data and the 2010 election results to identify and map these constituencies.
I downloaded the following data sets:-
OS Boundaryline: https://www.ordnancesurvey.co.uk/opendatadownload/products.html
Step 1 – Examine and prepare the data
I need both data sets to use exactly the same name and formatting in order to In order to add the 2010 election results to the OS Boundary line polygons.
- Start QGIS and set the map projection to OSGB.
- Use the Add Vector Layer button to add the file westminster_const_region.shp from the OS Boundary line data
- Open the attribute table to check the data structure and contents:-
Now to check the GE2010-constituency-results-website.xls
Unfortunately the two datasets don’t use exactly the same constituency names! It is fairly easy, but time consuming to match the record from the Election Results dataset to the OS Boundary Line record using Excel or Libre Office.
To export the OS Boundary line polygon names to Excel:-
- Right click on the westminster_const_region.shp in the Layers Panel
- Select Save As
I prefer to use the .dbf format when exchanging data between GIS and Excel as it is quicker to import than using .csv format.
Both name fields are needed. The first, constituency _name will be used to link to the constituency polygons once the table is imported into QGIS. The second, Results_Table_Name is used by the VLOOKUP query that adds the five columns from the results data.
Save the data as a .csv file when this stage is complete.
Topology rules define the permissible relationships of features within a given GIS layer or between features in two different GIS layers. An example is that features in a road dataset must be connected to other roads at both ends, unless the road is specified as a dead end street.
Advantage of topology over queries
A lot of the checks that topology rules carry out could be achieved using spatial queries. You may have to use queries if the GIS software you’re using doesn’t have a topology feature.
Topology rules have the advantage that they only need be created once and then they can check your work as you go.
Queries would need to be re-created each time they are run. They can be saved, depending on the GIS being used, but this is still more time consuming and it is a task that must be carried out separately at the end of a work session.
QGIS 2.2 topology tool has the following rules pre-defined:-
- End points must be covered by (e.g. a railway line usually begins and ends at a station)
- Must contain (e.g. a building polygon must contain at least one address point seed)
- Must not have dangles (a line must begin and end at another line)
- Must not have duplicates (each feature should be unique, e.g. postcode areas)
- Must not have gaps (e.g. administrative area polygons cannot have gaps)
- Must not have invalid geometries
- Must not have multi-part geometries (each feature should be a separate entry)
- Must not overlap (e.g. administrative area polygons cannot overlap each other)
- Must not overlap with (a feature from layer must not overlap with another layer)
Example 1 – Roads must not have dangles
The following example uses the “Must not have dangles” rule to identify polylines from a roads dataset that are not snapped to other lines. Roads usually begin and end at a junction with another road, so this is a useful rule to identify where lines were not correctly snapped together.
To create and validate a Topology Rule
- Open the Topology Panel, by selecting Vector menu, Topology Checker, Topology Checker
- The Topology Panel appears in the lower right corner of the QGIS desktop window
- Press the Configure button to open the Topology Rule Settings dialog
- The top of the box will have 2 or 3 pull down boxes depending on the layer and rule that is chosen. Use these to build the rule and then press the Add Rule button.
- Press OK when done, the dialog box closes and the window returns to the QGIS Desktop.
- Press either the Validate All or Validate extent, depending on whether you wish to validate the entire dataset or just the current view extent.
- The errors will be listed. Double click on a row will make the map window zoom and pan to the error.
QGIS can re-project a layer using both on-the-fly re-projection for the current session; and by saving a copy of the layer with a new Co-ordinate Reference System (CRS) defined.
On the fly
This is useful when a layer only needs to be re-projected for the current session.
Add the layer. If the CRS is known, QGIS will re-project it if necessary.
To check which CRS has been specified for the layer, right click on the layer in the Layers Panel, select Properties, and then select the General tab.
To save a new copy
It is a good practice to save a copy of a layer once it has been re-projected. This is to ensure the new CRS and transformations are permanently assigned to it. This avoids transformation errors when it is added to later map documents.
To save a copy:-
- Right click on the layer in the Layers Panel , select Save As.
- In the Save Vector layer as dialog, specify the filename, plus the new Co-ordinate Reference System. It is possible to add a symbology reference scale and new attributes. It is a good idea to add the new layer to the map to check it is correctly projected.
I want to find a location that is close to existing industrial areas (red polygons) and away from Sites of Special Scientific Interest (green polygons)
I will do this by using the Proximity (Raster Distance) analysis tool to create distance thematics, then using the Raster calculator to average the distance from each criterion.
Convert to Raster
The process only works with input files that are in raster format. As our source is in vector format (it’s a polygon .shp file), we need to convert it to a raster file.
It is good practice to add an attribute column, set its value to 1 so the resulting raster has a value of 1 for all the polygons. This can be done using the Field Calculator in the Attribute table.
Change display properties
The raster initially appears as a grey box. Don’t worry, this is normal. I am going to adjust its display properties so I can see the information.
- Right click on the layer
- Select Properties
- Select Stretch to MinMax under Contrast Enhancement
- Select the style tab
- Tick Invert Colour map. This makes the areas with a value of 1 black and the areas with a value of 0 white
The colours look quite stark, so I’m going to apply a transparency:-
- Select the Transparency tab
- In the Transparent Pixel List box, enter 1 under Gray and 50 under % Transparent
Its appearance is now less over-powering and I can see other layers too:-
Now to create a thematic based on the proximity (or distance) between each pixel and the nearest point of a SSSI site:-
- Open the Proximity calculator by selecting Raster menu, Analysis, Proximity (Raster Distance)
- Select the input raster and output raster (I found it works best if the output file is in .tif format)
- I want to measure the distance to SSSI pixels with a value of 1 (that is what I set to be the value used for areas with SSSI designation)
- Distance units are Geo (geographical) rather than pixels
The output is initially a grey square. I have made the following adjustments:-
- Colour map – Puseudo colour
- Global Transparency – 50%
It is worth exploring the options to invert the colour map and Contrast Enhancement
The SSSI sites are visible as the green polygons, the thematic is red for areas closest to sites and blue for areas furthest away:-
Repeat the process for all the necessary layers:
The raster calculator is a powerful tool that performs mathematical operations on each cell in a raster. Examples of this can be to calculate elevation, distance or density.
In this case, I am going to use it to identify the areas that are greater than 1km from a SSSI site by applying queries to identify matching pixel values.
- Open the raster calculator
- Enter the following expression SSSI_Distance@1 > 1000
The map units are in metres, so 1,000m = 1km
The resulting raster appears as a grey box. As usual, adjust its display properties (e.g. Contrast Enhancement, Invert Map and Transparency). The areas that are further than 1km from a SSSI site are now highlighted in grey, with the sites visible in green:-
I want to identify which areas of Chetney Marshes would be flooded by a 2m rise in sea level. I used LIDAR data in contour format as the elevation data:-
I am using the TIN method to create an elevation model as the area is relatively small and the data is supplied as contours. TIN’s are more accurate, especially if the source data isn’t in grid format. However they are slower to process, especially over very large areas.
Select Plugins, Interpolation to open the Interpolation dialogue:-
I made the following changes to the display properties:-
- Colour map – Pusedocolour
- Transparency – I set global transparency to 50%
I now have an elevation model of the area. I now use the Raster Calculator to identify each pixel with an elevation of less than 2m. The elevation is stored in the pixel value. The raster calculator will identify each pixel with band value of less than 2.
The expression is elevation < 2
Select Raster, Raster Calculator to open the Raster Calculator:-
To change the Display Properties for a layer, Right click on the Layer in the layers panel (Table of Contents) and select Properties. I made the following changes to the display properties so the areas that are less than 2m, and susceptible to flooding appear highlighted in blue:-
- Style tab: Color map – Colormap
- Transparency: Global transparency 50%
- Colormap tab: I added 2 entries:-
- 0.00 white
- 1.00 blue
Raster files consist of a grid of cells, each cell contains a numeric value, which is used to determine how to colour each cell. This value may be based on the elevation of the cell, flood water depth, or soil quality. It is possible to extract this information by point sampling or using a terrain profile. Point sampling copies the cell’s value to the overlying point. A terrain profile tool plots a graph with the cell’s value (elevation) on the Y axis and the distance along the section on the X axis.
Point Sampling Tool
DEM’s are often used to then update the elevation values of overlying points, for example I have used data from DEM’s to update the elevation values of address points and utilities. This isn’t as accurate as surveying each point, but it is a lot quicker! This process is also referred to image extraction, raster/vector conversion.
For this tutorial, you will need:-
- The Point Sampling tool in QGIS is an optional plugin. You can download it by using the menus to select Plugins, Fetch Python Plugins.
- Nasa’s srtm data, which you can download from here: http://srtm.csi.cgiar.org/
- Some point data. If you can’t think of any, then they’re easy to create, for example use the Open Layers plugin to load Open Streetmap or Google Maps of your area, and then create points over a few cities.
I’m going to add the elevation value from the srtm rasters to a selection of UK towns and cities:-
- Use the menus to select Plugins, Analyses, Point Sampling Tool
- The Point Sampling Tool dialogue box opens. Select:-
- The layer that contains the points to be sampled
- The layer(s) with the field(s)/band(s) to get values from
- The output (results) file
- Press OK
The results file just contains the elevations:-
It is possible to add these to the original layer:-
- Create a buffer around the new points
- Use the menus to select Vector, Data Management Tools, Join Attributes By location
- Select the original points as the target and the buffer as the join layer
Another option is to update the x and y co-ordinates for both points using the Field calculator and then to match the rows in Excel on the co-ordinate column.