# Terrain analysis


```{r echo=FALSE}
knitr::opts_chunk$set(echo = FALSE)
fd<-paste(getwd(),"figs4",sep="/")
flnms<-dir(fd,pattern= "Screen")
fls<-paste(fd,flnms,sep="/")
aa<-as.numeric(gsub("[^0-9\\.]", "", flnms))
fls<-fls[order(aa)]
a<-dir("figs5",pattern="Scree*")
```

## What is terrain analysis

-Analysis of topographic features (geomorphology)> For example

  - Slope
  - Aspect
  - Hill shading
  - Flow
  - Insolation
  
Uses digital elevation models (DEM)

- Raster layer (i.e. pixels)
- Each pixel contains an elevation value
- DEMs vary in extent and resolution


There are many uses for terrain analysis. Hydrologists can use it to delimit watersheds and analyse flows. The layers derived from terrain analysis can be queried to produce information at points, along lines or within polygons. We can also carry out raster algebra operations on the layers.

How are DEMs produced?


- Traditionally made through interpolation between contours from a traditional map
- Now mainly through remote sensing involving some form of radar based sensor
   - SRTM (Shuttle Radar Topography mission): Global extent 90m resolution
   - Lidar (Light detection and ranging): Local extent, varying resolution



## Lidar data


Raw lidar data consists of a point cloud of returns with some information regarding intensity 

- First return: Top of vegetation or buildings
- Last return. Ground

Lidar point clouds can be processed using a range of different software. QGIS can be linked to LASTools. We will not process raw data in this class.

Source for Lidar data
http://environment.data.gov.uk/ds/survey/index.jsp#/survey?grid=SY98


To see some point clouds open http://plas.io/

There are some point clouds of Arne on the server. Download them locally then open with plas.io.

#### Action: Note that this is not an obligatory exerise. It is to illustrate the concept of a poiint cloud interactively


1. Click on this link http://r.bournemouth.ac.uk:82/Quantitative_and_Spatial_Analysis/Week_2/point_clouds/ in Firefox to open up the folder. 
2. Select one of the files. Download the file to your PC by write clicking and saving the target (this doesn't work in INternet Explorer). 
3. Open up the plas.io window by clicking on the link to it http://plas.io/  
4. Now load the file using the browse button at the top.
5. The result  will be dark at first and you won't see anything. Scroll down to find the intensity scaling and adjust this to a very narrow band on the left hand side until the cloud is visible. You will be shown this in the class.
6. Experiment with other options to get a feel for the nature of the point cloud
7. Zoom in and out and rotate the image with the mouse


http://r.bournemouth.ac.uk:82/Quantitative_and_Spatial_Analysis/Week_2/point_clouds/


#### Plos.io 

![](figs5/`r a[13]`)



## Working with Lidar data in QGIS

The Lidar point clouds for both Hengistbury head and Arne have been already processed to produce the digital terrain model (ground) and digital surface model (top of vegetation and buildings). You have loaded the layers for Hengistbury head already and you should have saved them locally. 

![](`r x<-1;fls[x]`)

### Analytical hill shading


Hill shading is a very commonly used technique, particularly in the field of Archaeology. It helps identify visible features as defined by their topography. As Lidar can penetrate forest canopies it can be used to identify Mayan ruins "lost in the jungle".

Running the hill shading algorithm is very easy.

#### Action: Make a hill shaded layer from the DTM

1. Click on the raster menu at the top  bar. 
2. Select analysis. 
3. Select hillshade 
3. Choose the input layer. Use the DTM you have loaded (You can try the DSM later to see the difference)
4. Keep the default options.
5. Click run to produce a temporary file output **IMPORTANT NOTE. All temporary files are just that. Temporary. This avoids building up lots of files that you may not eventually use. But you must save them before closing the program if you are going to keep them**


![](`r x<-x+1;fls[x]`)


![](`r x<-x+1;fls[x]`)

#### This is the result. 
You can now experiment with the technique by changing the parameters if you wish. Major changes away from the default settings will produce very ugly results, but small tweaks can help to show up features lit from different angles.


![](`r x<-x+1;fls[x]`)


## Colouring the DTM


The DTM and DSM layers are still showing in the project with a simple grey scale. We can colour them in with palettes in order to produce a clearer map.


#### Action: Colour the DTM using a single band "pseudocolour"

1. Right click the layer in the layers panel to open the menu.
2. Choose properties.
3. Choose Symbology
4. Select the render type to be "single band pseudocolor"
5. Choose a colour ramp
6. Apply the result to the map
7. Move (or ensure that it is already) the result up in the layers panel so it is visible at the top.
8. Move the hill shaded layer to be just below the shaded DTM
9. Adjust the transparency of the top layer (right click the layer, find transparency in the properties window, move the slider) so that the map is transparent over the hill shaded layer.

#### Steps 1 to 3

![](`r x<-x+1;fls[x]`)

![](`r x<-x+1;fls[x]`)

![](`r x<-x+1;fls[x]`)

#### Inverting the color ramp. (Click color ramp and choose invert)


![](`r x<-x+1;fls[x]`)

#### Colouring the layer

![](`r x<-x+1;fls[x]`)

![](`r x<-x+1;fls[x]`)

#### Adjusting the transparency

![](`r x<-x+1;fls[x]`)

#### This is the result. 

![](`r x<-x+1;fls[x]`)



### Adding contour lines

We can make a vector layer of contour lines directly from the DTM. If you use the DSM then contours may be drawn around the edges of trees and forests, which could be useful for analysis in some cases, but may look odd.

#### Action: Make contour lines at 2m intervals


1. Click on the raster menu on the top
2. Scroll to "extraction"
3. Choose contours
4. Select the contour spacing
5. Run the algorithm.
6. You may wish to adjust they styling to alter the colour and width of the lines by right clicking to see the Properties menu, selecting Symbology and altering the options there. 

#### Selecting contours from the menu

![](`r x<-x+1;fls[x]`)


#### The result

![](`r x<-x+1;fls[x]`)

#### Zooming in

![](`r x<-x+1;fls[x]`)

## Calculating slope and aspect

### Slope

Calculating the slope of hillsides is a basic operation in terrain analysis. More complex algorithms can use the slope layer in a range of applications, such as hydrological modelling (flows and watershed delineation) and insolation calculations.  It is easy to produce a slope layer.


#### Action: Produce a layer showing slope in degrees

1. Choose "analyses" from the raster menu at the top
2. Select the slope option from the menu
3. Select the DTM layer that will be used to calculate slope
4. Run the analysis with default options to produce a new temporary layer.


#### Selecting the algorithm

![](`r x<-x+1;fls[x]`)


#### Selecting the layer

![](`r x<-x+1;fls[x]`)

#### The result

![](`r x<-x+1;fls[x]`)


You may want to shade this layer using the styling options to select a pseudocolor ramp, as you did for the DTM.

### Aspect

Calculating the aspect of each hillside leads to a layer that can be used as input to other analyses. It may be particularly useful for calculating insolation. South facing slopes will tend to be warmer and this may influence the distribution of organisms such as insects and reptiles.


#### Action: Produce a layer showing aspect in degrees

1. Choose "analyses" from the raster menu at the top
2. Select the aspect option from the menu
3. Select the DTM layer that will be used to calculate slope
4. Run the analysis with default options to produce a new temporary layer.


#### Selecting the layer

![](`r x<-x+1;fls[x]`)



![](`r x<-x+1;fls[x]`)

Again you may want to change the colour scheme on the layer.

#### The result

![](`r x<-x+1;fls[x]`)

## Calculating canopy height.

The difference between the DTM and DSM layers (first Lidar return and last Lidar return) is a measure of the canopy height in forests and even lower vegetation such as shrubland and heathland. You can produce a simple canopy height model through subtraction. To do this we use the raster calculator option.

#### Action: Produce a canopy height model

1. Choose "raster calculator" from the raster menu at the top
2. Type a name for the output layer into the box You could call it CHM for canopy height model
3. Form the raster calculator expression by clicking on the first layer name i.e. DSM@1 ( the @ sign represents the first band, but there is only one band per layer) then the minus sign then the second layer. It should look like this "dsm@1" - "dtm@1"
4. Run the calculator
5. Shade the layer using a pseudocolor option


#### Running the calculation

![](`r x<-x+1;fls[x]`)

![](`r x<-x+1;fls[x]`)



#### The result


![](`r x<-x+1;fls[x]`)

## Investigating canopy height in 3D

You can use the qgis23js plug in to produce a three dimensional depiction of the canopy height by draping the coloured layer over the DSM.

![](`r x<-x+1;fls[x]`)

## Calculating raster stats for polygons.

It can be very useful to extract values from the terrain analyses and add them to vector layers. For example, you may want to find the slope, aspect, elevation etc. of some quadrats that you have placed in a study area. We will look at this again when carrying out field work at Arne. In the case of Hengistbury you may want to find the mean elevation, mean canopy height etc. of the area within each of the phase one polygons. You can do this using an option provided in the Saga tool kit.


#### Action: Add some derived statistics to the phase one polygons

1. You will need some digitised polygons or polygons derived from another source.
2. Activate the toolbox found under the processing menu at the top.
3. Scroll down to the SAGA options.
4. Find the option for "raster statistics for polygons" under the vector <-> raster menu heading.
5. Select the grid(s) (Saga term for raster) that you will use by clicking the button on the right. The processing can be slow for large grids so just choose the CHM
6. Choose the vector (polygon) layer you will use as an overlay.
7. Select the statistics you want to calculate. Just choose the ones you actually need, as the process can be slow. Say, for example, minimum, mean and maximum canopy height.
8. Press run and wait for the analysis to finish. This can take a while and could possibly hang if the layers are very large, as this is quite a complex operation.
9. Open the resulting layers attribute table to see the results.







![](`r x<-x+1;fls[x]`)

![](`r x<-x+1;fls[x]`)

![](`r x<-x+1;fls[x]`)

![](`r x<-x+1;fls[x]`)

![](`r x<-x+1;fls[x]`)

![](`r x<-x+1;fls[x]`)

![](`r x<-x+1;fls[x]`)


