In this lesson, you will be guided through a complete GIS analysis in QGIS.
Note
Lesson developed by Linfiniti Consulting (South Africa) and Siddique Motala (Cape Peninsula University of Technology)
You are tasked with finding areas in and around the Cape Peninsula that are suitable habitats for a rare fynbos plant species. The extent of your area of investigation covers Cape Town and the Cape Peninsula between Melkbosstrand in the north and Strand in the south. Botanists have provided you with the following preferences exhibited by the species in question:
- It grows on east facing slopes
- It grows on slopes with a gradient between 15% and 60%
- It grows in areas that have a total annual rainfall of > 1000 mm
- It will only be found at least 250 m away from any human settlement
- The area of vegetation in which it occurs should be at least 6000 ㎡ in area
As a student at the University, you have agreed to search for the plant in four different suitable areas of land. You want those four suitable areas to be the ones that are closest to the University of Cape Town where you live. Use your GIS skills to determine where you should go to look.
The data for this exercise can be found in the :file:`exercise_data/more_analysis` folder.
You are going to find the four suitable areas that are closest to the University of Cape Town.
The solution will involve:
- Analyzing a DEM raster layer to find the east facing slopes and the slopes with the correct gradients
- Analyzing a rainfall raster layer to find the areas with the correct amount of rainfall
- Analyzing a zoning vector layer to find areas that are away from human settlement and are of the correct size
Click on the
Current CRS button in the
lower right corner of the screen.
Under the :guilabel:`CRS` tab of the dialog that appears, use the
"Filter" tool to search for "33S".
Select the entry :guilabel:`WGS 84 / UTM zone 33S` (with EPSG code
32733).Click :guilabel:`OK`
Save the project file by clicking on the
Save Project toolbar button, or use the
:menuselection:`Project --> Save As...` menu item.Save it in a new directory called :file:`Rasterprac`, that you should create somewhere on your computer. You will save whatever layers you create in this directory as well. Save the project as :file:`your_name_fynbos.qgs`.
In order to process the data, you will need to load the necessary layers (street names, zones, rainfall, DEM, districts) into the map canvas.
Click on the
Open Data Source Manager
button in the Data Source Manager Toolbar, and enable the
:guilabel:`Vector` tab in the dialog that appears, or
use the :menuselection:`Layer --> Add Layer -->`
:menuselection:`Add Vector Layer...` menu itemEnsure that
:guilabel:`File` is selectedClick on the :guilabel:`...` button to browse for vector dataset(s)
In the dialog that appears, open the :file:`exercise_data/more_analysis/Streets` directory
Select the file :file:`Street_Names_UTM33S.shp`
Click :guilabel:`Open`.
The dialog closes and shows the original dialog, with the file path specified in the text field next to :guilabel:`Vector dataset(s)`. This allows you to ensure that the correct file is selected. It is also possible to enter the file path in this field manually, should you wish to do so.
Click :guilabel:`Add`. The vector layer will be loaded into your map. Its color is automatically assigned. You will change it later.
Rename the layer to
Streets- Right-click on it in the :guilabel:`Layers` panel (by default, the pane along the left-hand side of the screen)
- Click :guilabel:`Rename` in the dialog that appears and rename it, pressing the Enter key when done
Repeat the vector adding process, but this time select the :file:`Generalised_Zoning_Dissolve_UTM33S.shp` file in the :file:`Zoning` directory.
Rename it to
Zoning.Load also the vector layer :file:`admin_boundaries/Western_Cape_UTM33S.shp` into your map.
Rename it to
Districts.
- Click on the
Open Data Source Manager
button and enable the
:guilabel:`Raster` tab in
the dialog that appears, or use the
:menuselection:`Layer --> Add Layer -->`
:menuselection:`Add Raster Layer...` menu item - Ensure that
:guilabel:`File` is selected - Navigate to the appropriate file, select it, and click :guilabel:`Open`
- Do this for each of the following two raster files, :file:`DEM/SRTM.tif` and :file:`rainfall/reprojected/rainfall.tif`
- Rename the SRTM raster to
DEMand the rainfall raster toRainfall(with an initial capital)
Click and drag layers up and down in the :guilabel:`Layers` panel to change the order they appear in on the map so that you can see as many of the layers as possible.
Now that all the data is loaded and properly visible, the analysis can begin. It is best if the clipping operation is done first. This is so that no processing power is wasted on computing values in areas that are not going to be used anyway.
Due to the aforementioned area of investigation, we need to limit our districts to the following ones:
BellvilleCapeGoodwoodKuils RiverMitchells PlainSimon TownWynberg
Right-click on the
Districtslayer in the :guilabel:`Layers` panel.In the menu that appears, select the :guilabel:`Filter...` menu item. The :guilabel:`Query Builder` dialog appears.
You will now build a query to select only the candidate districts:
In the :guilabel:`Fields` list, double-click on the
NAME_2field to make it appear in the :guilabel:`SQL where clause` text field belowClick the :guilabel:`IN` button to append it to the SQL query
Open the brackets
Click the :guilabel:`All` button below the (currently empty) :guilabel:`Values` list.
After a short delay, this will populate the :guilabel:`Values` list with the values of the selected field (
NAME_2).Double-click the value
Bellvillein the :guilabel:`Values` list to append it to the SQL query.Add a comma and double-click to add
CapedistrictRepeat the previous step for the remaining districts
Close the brackets
Query builder
The final query should be (the order of the districts in the brackets does not matter):
"NAME_2" in ('Bellville', 'Cape', 'Goodwood', 'Kuils River', 'Mitchells Plain', 'Simon Town', 'Wynberg')Note
You can also use the
ORoperator; the query would look like this:"NAME_2" = 'Bellville' OR "NAME_2" = 'Cape' OR "NAME_2" = 'Goodwood' OR "NAME_2" = 'Kuils River' OR "NAME_2" = 'Mitchells Plain' OR "NAME_2" = 'Simon Town' OR "NAME_2" = 'Wynberg'
Click :guilabel:`OK` twice.
The districts shown in your map are now limited to those in the list above.
Now that you have an area of interest, you can clip the rasters to this area.
Open the clipping dialog by selecting the menu item :menuselection:`Raster --> Extraction --> Clip Raster by Mask Layer...`
In the :guilabel:`Input layer` dropdown list, select the
DEMlayerIn the :guilabel:`Mask layer` dropdown list, select the
DistrictslayerScroll down and specify an output location in the :guilabel:`Clipped (mask)` text field by clicking the :guilabel:`...` button and choosing :guilabel:`Save to File...`
- Navigate to the :file:`Rasterprac` directory
- Enter a file name -
DEM_clipped.tif - Save
Make sure that
:guilabel:`Open output file after running algorithm` is checkedClick :guilabel:`Run`
After the clipping operation has completed, leave the :guilabel:`Clip Raster by Mask Layer` dialog open, to be able to reuse the clipping area
Select the
Rainfallraster layer in the :guilabel:`Input layer` dropdown list and save your output as :file:`Rainfall_clipped.tif`Do not change any other options. Leave everything the same and click :guilabel:`Run`.
After the second clipping operation has completed, you may close the :guilabel:`Clip Raster by Mask Layer` dialog
Save the map
For our analysis we need the rasters to have the same CRS and they have to be aligned.
First we change the resolution of our rainfall data to 30 meters (pixel size):
In the :guilabel:`Layers` panel, ensure that
Rainfall_clippedis the active layer (i.e., it is highlighted by having been clicked on)Click on the :menuselection:`Raster --> Projections --> Warp (Reproject)...` menu item to open the :guilabel:`Warp (Reproject)` dialog
Under :guilabel:`Resampling method to use`, select :guilabel:`Bilinear (2x2 kernel)` from the drop down menu
Set :guilabel:`Output file resolution in target georeferenced units` to
30Scroll down to :guilabel:`Reprojected` and save the output in your :file:`rainfall/reprojected` directory as :file:`Rainfall30.tif`.
Make sure that
:guilabel:`Open output file after running algorithm` is checked
Then we align the DEM:
- In the :guilabel:`Layers` panel, ensure that
DEM_clippedis the active layer (i.e., it is highlighted by having been clicked on) - Click on the :menuselection:`Raster --> Projections --> Warp (Reproject)...` menu item to open the :guilabel:`Warp (Reproject)` dialog
- Under :guilabel:`Target CRS`, select :guilabel:`Project CRS: EPSG:32733 - WGS 84 / UTM zone 33S` from the drop down menu
- Under :guilabel:`Resampling method to use`, select :guilabel:`Bilinear (2x2 kernel)` from the drop down menu
- Set :guilabel:`Output file resolution in target georeferenced units` to
30 - Scroll down to :guilabel:`Georeferenced extents of output file to be created`. Use the button to the right of the text box to select :menuselection:`Calculate from Layer --> Rainfall30`.
- Scroll down to :guilabel:`Reprojected` and save the output in your :file:`DEM/reprojected` directory as :file:`DEM30.tif`.
- Make sure that
:guilabel:`Open output file after running algorithm` is checked
In order to properly see what's going on, the symbology for the layers needs to be changed.
- In the :guilabel:`Layers` panel, right-click on the
Streetslayer - Select :guilabel:`Properties` from the menu that appears
- Switch to the :guilabel:`Symbology` tab in the dialog that appears
- Click on the :guilabel:`Line` entry in the top widget
- Select a symbol in the list below or set a new one (color, transparency, ...)
- Click :guilabel:`OK` to close the :guilabel:`Layer Properties`
dialog.
This will change the rendering of the
Streetslayer. - Follow a similar process for the
Zoninglayer and choose an appropriate color for it
Raster layer symbology is somewhat different.
Open the :guilabel:`Properties` dialog for the
Rainfall30raster layerSwitch to the :guilabel:`Symbology` tab. You'll notice that this dialog is very different from the version used for vector layers.
Ensure that the button :guilabel:`Mean +/- standard deviation` is selected
Make sure that the value in the associated box is
2.00For :guilabel:`Contrast enhancement`, make sure it says :guilabel:`Stretch to MinMax`
For :guilabel:`Color gradient`, change it to :guilabel:`White to Black`
Click :guilabel:`OK`
The
Rainfall30raster, if visible, should change colors, allowing you to see different brightness values for each pixelRepeat this process for the
DEM30layer, but set the standard deviations used for stretching to4.00
Remove the original
RainfallandDEMlayers, as well asRainfall_clippedandDEM_clippedfrom the :guilabel:`Layers` panel:Right-click on these layers and select :guilabel:`Remove`.
Note
This will not remove the data from your storage device, it will merely take it out of your map.
Save the map
You can now hide the vector layers by unchecking the box next to them in the :guilabel:`Layers` panel. This will make the map render faster and will save you some time.
In order to create the hillshade, you will need to use an algorithm that was written for this purpose.
In the :guilabel:`Layers` panel, ensure that
DEM30is the active layer (i.e., it is highlighted by having been clicked on)Click on the :menuselection:`Raster --> Analysis --> Hillshade...` menu item to open the :guilabel:`Hillshade` dialog
Scroll down to :guilabel:`Hillshade` and save the output in your :file:`Rasterprac` directory as :file:`hillshade.tif`
Make sure that
:guilabel:`Open output file after running algorithm` is checkedClick :guilabel:`Run`
Wait for it to finish processing.
The new hillshade layer has appeared in the
:guilabel:`Layers` panel.
- Right-click on the
hillshadelayer in the :guilabel:`Layers` panel and bring up the :guilabel:`Properties` dialog - Click on the :guilabel:`Transparency` tab and set the
:guilabel:`Global Opacity` slider to
20% - Click :guilabel:`OK`
- Note the effect when the transparent hillshade is superimposed over
the clipped DEM.
You may have to change the order of your layers, or click off the
Rainfall30layer in order to see the effect.
Click on the :menuselection:`Raster --> Analysis --> Slope...` menu item to open the :guilabel:`Slope` algorithm dialog
Select
DEM30as :guilabel:`Input layer`Check
:guilabel:`Slope expressed as percent instead of degrees`.
Slope can be expressed in different units (percent or degrees).
Our criteria suggest that the plant of interest grows on slopes with
a gradient between 15% and 60%.
So we need to make sure our slope data is expressed as a percent.Specify an appropriate file name and location for your output.
Make sure that
:guilabel:`Open output file after running algorithm` is checkedClick :guilabel:`Run`
The slope image has been calculated and added to the map. As usual, it is rendered in grayscale. Change the symbology to a more colorful one:
- Open the layer :guilabel:`Properties` dialog (as usual, via the right-click menu of the layer)
- Click on the :guilabel:`Symbology` tab
- Where it says :guilabel:`Singleband gray` (in the :guilabel:`Render type` dropdown menu), change it to :guilabel:`Singleband pseudocolor`
- Choose :guilabel:`Mean +/- standard deviation x` for
:guilabel:`Min / Max Value Settings` with a value of
2.0 - Select a suitable :guilabel:`Color ramp`
- Click :guilabel:`Run`
Use the same approach as for calculating the slope, choosing :guilabel:`Aspect...` in the :menuselection:`Raster --> Analysis` menu.
Remember to save the project periodically.
Specify your :file:`Rasterprac` directory as the location for the :guilabel:`Output layer` (click on the :guilabel:`...` button), and save it as :file:`slope15_60.tif`
Ensure that the :guilabel:`Open output file after running algorithm` box is selected.
In the :guilabel:`Raster bands` list on the left, you will see all the raster layers in your :guilabel:`Layers` panel. If your Slope layer is called
slope, it will be listed asslope@1. Indicating band 1 of the slope raster.The slope needs to be between
15and60degrees.Using the list items and buttons in the interface, build the following expression:
(slope@1 > 15) AND (slope@1 < 60)
Set the :guilabel:`Output layer` field to an appropriate location and file name.
Click :guilabel:`Run`.
Now find the correct aspect (east-facing: between 45 and 135
degrees) using the same approach.
Build the following expression:
(aspect@1 > 45) AND (aspect@1 < 135)
You will know it worked when all of the east-facing slopes are white in the resulting raster (it's almost as if they are being lit by the morning sunlight).
Find the correct rainfall (greater than 1000 mm) the same way.
Use the following expression:
Rainfall30@1 > 1000
Now that you have all three criteria each in separate rasters, you
need to combine them to see which areas satisfy all the criteria.
To do so, the rasters will be multiplied with each other.
When this happens, all overlapping pixels with a value of 1 will
retain the value of 1 (i.e. the location meets the criteria), but
if a pixel in any of the three rasters has the value of 0 (i.e.
the location does not meet the criteria), then it will be 0 in the
result.
In this way, the result will contain only the overlapping areas that
meet all of the appropriate criteria.
Open the Raster Calculator (:menuselection:`Raster --> Raster Calculator...`)
Build the following expression (with the appropriate names for your layers):
[aspect45_135] * [slope15_60] * [rainfall_1000]
Set the output location to the :file:`Rasterprac` directory
Name the output raster :file:`aspect_slope_rainfall.tif`
Ensure that
:guilabel:`Open output file after running algorithm` is checkedClick :guilabel:`Run`
The new raster now properly displays the areas where all three criteria are satisfied.
Save the project.
The next criterion that needs to be satisfied is that the area must be
250 m away from urban areas.
We will satisfy this requirement by ensuring that the areas we compute
are inside rural areas, and are 250 m or more from the edge of the area.
Hence, we need to find all rural areas first.
Hide all layers in the :guilabel:`Layers` panel
Unhide the
Zoningvector layerRight-click on it and bring up the :guilabel:`Attribute Table` dialog. Note the many different ways that the land is zoned here. We want to isolate the rural areas. Close the Attribute table.
Right-click on the
Zoninglayer and select :guilabel:`Filter...` to bring up the :guilabel:`Query Builder` dialogBuild the following query:
"Gen_Zoning" = 'Rural'
See the earlier instructions if you get stuck.
Click :guilabel:`OK` to close the :guilabel:`Query Builder` dialog. The query should return one feature.
Query builder Zoning
You should see the rural polygons from the Zoning layer.
You will need to save these.
- In the right-click menu for
Zoning, select :menuselection:`Export --> Save Features As...`. - Save your layer under the :file:`Rasterprac` directory
- Name the output file :file:`rural.shp`
- Click :guilabel:`OK`
- Save the project
Now you need to exclude the areas that are within 250m from the
edge of the rural areas.
Do this by creating a negative buffer, as explained below.
Click the menu item :menuselection:`Vector --> Geoprocessing Tools --> Buffer...`
In the dialog that appears, select the
rurallayer as your input vector layer (:guilabel:`Selected features only` should not be checked)Set :guilabel:`Distance` to
-250. The negative value means that the buffer will be an internal buffer. Make sure that the units are meters in the dropdown menu.In :guilabel:`Buffered`, place the output file in the :file:`Rasterprac` directory, and name it :file:`rural_buffer.shp`
Click :guilabel:`Save`
Click :guilabel:`Run` and wait for the processing to complete
Close the :guilabel:`Buffer` dialog.
Make sure that your buffer worked correctly by noting how the
rural_bufferlayer is different from therurallayer. You may need to change the drawing order in order to observe the difference.Remove the
rurallayerSave the project
Now you need to combine your rural_buffer vector layer with the
aspect_slope_rainfall raster.
To combine them, we will need to change the data format of one of the
layers. In this case, you will vectorize the raster, since vector
layers are more convenient when we want to calculate areas.
Click on the menu item :menuselection:`Raster --> Conversion --> Polygonize (Raster to Vector)...`
Select the :file:`aspect_slope_rainfall` raster as :guilabel:`Input layer`
Set :guilabel:`Name of the field to create` to
suitable(the default field name isDN- Digital number data)Save the output. Under :guilabel:`Vectorized`, select :guilabel:`Save file as`. Set the location to :file:`Rasterprac` and name the file :file:`aspect_slope_rainfall_all.shp`.
Ensure that
:guilabel:`Open output file after running algorithm` is checkedClick :guilabel:`Run`
Close the dialog when processing is complete
All areas of the raster have been vectorized, so you need to select
only the areas that have a value of 1 in the suitable field.
(Digital Number.
Open the :guilabel:`Query Builder` dialog (right-click - :guilabel:`Filter...`) for the new vector layer
Build this query:
"suitable" = 1
Click :guilabel:`OK`
After you are sure the query is complete (and only the areas that meet all three criteria, i.e. with a value of
1are visible), create a new vector file from the results, using the :menuselection:`Export --> Save Features As...` in the layer's right-click menuSave the file in the :file:`Rasterprac` directory
Name the file :file:`aspect_slope_rainfall_1.shp`
Remove the
aspect_slope_rainfall_alllayer from your mapSave your project
When we use an algorithm to vectorize a raster, sometimes the algorithm yields what is called "Invalid geometries", i.e. there are empty polygons, or polygons with mistakes in them, that will be difficult to analyze in the future. So, we need to use the :guilabel:`Fix Geometry` tool.
- In the :guilabel:`Processing Toolbox`, search for
Fix geometries, and :guilabel:`Execute...` it - For the :guilabel:`Input layer`, select
aspect_slope_rainfall_1 - Under :guilabel:`Fixed geometries`, select :guilabel:`Save file as`, and save the output to :file:`Rasterprac` and name the file :file:`fixed_aspect_slope_rainfall.shp`.
- Ensure that
:guilabel:`Open output file after running algorithm` is checked - Click :guilabel:`Run`
- Close the dialog when processing is complete
Now that you have vectorized the raster, and fixed the resulting
geometry, you can combine the aspect, slope, and rainfall criteria
with the distance from human settlement criteria by finding the
intersection of the fixed_aspect_slope_rainfall layer and the
rural_buffer layer.
Click the menu item :menuselection:`Vector --> Geoprocessing Tools --> Intersection...`
In the dialog that appears, select the
rural_bufferlayer as :guilabel:`Input layer`For the :guilabel:`Overlay layer`, select the
fixed_aspect_slope_rainfalllayerIn :guilabel:`Intersection`, place the output file in the :file:`Rasterprac` directory
Name the output file :file:`rural_aspect_slope_rainfall.shp`
Click :guilabel:`Save`
Click :guilabel:`Run` and wait for the processing to complete
Close the :guilabel:`Intersection` dialog.
Make sure that your intersection worked correctly by noting that only the overlapping areas remain.
Save the project
The next criteria on the list is that the area must be greater than
6000 ㎡.
You will now calculate the polygon areas in order to identify the
areas that are the appropriate size for this project.
Open the new vector layer's right-click menu
Click the
Toggle editing button in the top
left corner of the table, or press Ctrl+eClick the
Open field calculator button in
the toolbar along the top of the table, or press Ctrl+iIn the dialog that appears, make sure that
:guilabel:`Create new field` is checked, and set the
:guilabel:`Output field name` to areaThe output field type should be a decimal number (real). Set :guilabel:`Precision` to1(one decimal).In the :guilabel:`Expression` area, type:
$area
This means that the field calculator will calculate the area of each polygon in the vector layer and will then populate a new integer column (called
area) with the computed value.Click :guilabel:`OK`
Do the same thing for another new field called
id. In :guilabel:`Field calculator expression`, type:$id
This ensures that each polygon has a unique ID for identification purposes.
Click
Toggle editing again, and save your
edits if prompted to do so
Now that the areas are known:
Build a query (as usual) to select only the polygons that are larger than
6000㎡. The query is:"area" > 6000
Save the selection in the :file:`Rasterprac` directory as a new vector layer called :file:`suitable_areas.shp`.
You now have the suitable areas that meet all of the habitat criteria for the rare fynbos plant, from which you will pick the four areas that are nearest to the University of Cape Town.
Create a new vector layer in the :file:`Rasterprac` directory as before, but this time, use :guilabel:`Point` as :guilabel:`Geometry type` and name it :file:`university.shp`
Ensure that it is in the correct CRS (
Project CRS:EPSG:32733 - WGS 84 / UTM zone 33S)Finish creating the new layer (click :guilabel:`OK`)
Hide all layers except the new
universitylayer and theStreetslayer.Add a background map (OSM):
- Go to the :guilabel:`Browser` panel and navigate to :menuselection:`XYZ Tiles --> OpenStreetMap`
- Drag and drop the
OpenStreetMapentry to the bottom of the :guilabel:`Layers` panel
Using your internet browser, look up the location of the University of Cape Town. Given Cape Town's unique topography, the university is in a very recognizable location. Before you return to QGIS, take note of where the university is located, and what is nearby.
Ensure that the
Streetslayer clicked on, and that theuniversitylayer is highlighted in the :guilabel:`Layers` panelNavigate to the :menuselection:`View --> Toolbars` menu item and ensure that :guilabel:`Digitizing` is selected. You should then see a toolbar icon with a pencil on it (
Toggle editing).
This is the Toggle Editing button.Click the :guilabel:`Toggle editing` button to enter edit mode. This allows you to edit a vector layer
Click the
Add Point Feature button, which
should be nearby the
Toggle editing buttonWith the :guilabel:`Add feature` tool activated, left-click on your best estimate of the location of the University of Cape Town
Supply an arbitrary integer when asked for the
idClick :guilabel:`OK`
Click the :guilabel:`Toggle editing` button to stop your editing session
Save the project
- Go to the Processing Toolbox, locate the Join Attributes by Nearest algorithm (:menuselection:`Vector general --> Join Attributes by Nearest`) and execute it
- :guilabel:`Input layer` should be
university, and :guilabel:`Input layer 2`suitable_areas - Set an appropriate output location and name
(
Joined layer) - Set the :guilabel:`Maximum nearest neighbors` to
4 - Ensure that
:guilabel:`Open output file after running algorithm` is checked - Leave the rest of the parameters with their default values
- Click :guilabel:`Run`
The resulting point layer will contain four features - they will
all have the location of the university and its attributes, and in
addition, the attributes of the nearby suitable areas (including the
id), and the distance to that location.
- Open the attribute table of the result of the join
- Note the
idof the four nearest suitable areas, and then close the attribute table - Open the attribute table of the
suitable_areaslayer - Build a query to select the four suitable areas closest to the
university (selecting them using the
idfield)
This is the final answer to the research question.
For your submission, create a fully labeled layout that includes the
semi-transparent hillshade layer over an appealing raster of your
choice (such as the DEM or the slope raster,
for example).
Also include the university and the suitable_areas layer, with
the four suitable areas that are closest to the university
highlighted.
Follow all the best practices for cartography in creating your output
map.













