-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path01_z07_nunn_2012.rmd
142 lines (101 loc) · 3.4 KB
/
01_z07_nunn_2012.rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
### Project Overview
---
**Objective:**
@nunn2012ruggedness showed empirically that the ruggedness of the terrain has had a positive impacts on economic developement in African countries. In this demonstration, we calculate Terrain Ruggedness Index (TRI) for African countries from the world elevation data.
---
**Datasets**
* World elevation data
* World country borders
---
**GIS tasks**
* read a raster file
- use `terra::rast()`
* import world country border data
- use `rnaturalearth::ne_countries()`
* crop a raster data to a particular region
- use `terra::crop()`
* replace cell values
- use `terra::subst()`
* calculate TRI
- use `terra::focal()`
+ create maps
* use the `tmap` package
---
**Preparation for replication**
Run the following code to install or load (if already installed) the `pacman` package, and then install or load (if already installed) the listed package inside the `pacman::p_load()` function.
```{r demo_nunn_2012_packages}
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
sf, # vector data operations
tidyverse, # data wrangling
stars,
tmap,
raster,
rnaturalearth,
skimr
)
```
### Project Demonstration
We first read the world elevation data using `terra::rast()`.
```{r read-dem}
(
dem <- terra::rast("Data/nunn_2012/GDEM-10km-BW.tif")
)
```
```{r map-world-elev, cache = TRUE}
tm_shape(dem) +
tm_raster()
```
In this dataset, the elevation of the ocean floor is recorded as 0, so let's replace an elevation of 0 with `NA`s. This avoids a problem associated with calculating TRI later.
```{r replace-zero}
dem <- terra::subst(dem, 0, NA)
```
```{r map-world-elev-no-zero, cache = TRUE}
tm_shape(dem) +
tm_raster()
```
Now, since our interest is in Africa, let's just crop the raster data to its African portion. To do so, we first get the world map using `rnaturalearth::ne_countries()` and filter out the non-African countries.
```{r get-africa-sf}
africa_sf <-
#--- get an sf of all the countries in the world ---#
rnaturalearth::ne_countries(scale = "medium", returnclass = "sf") %>%
#--- filter our non-African countries ---#
filter(continent == "Africa")
```
We can now apply `terra::crop()` to `dem` based on the bounding box of `africa_sf`.
```{r crop-to-africa}
africa_dem <- terra::crop(dem, africa_sf)
```
Here is a map of the crop data.
```{r map-africa-elev, cache = TRUE}
tm_shape(africa_dem) +
tm_raster()
```
Now, we are ready to calculte TRI, which is defined as
$$
Ruggedness_{r,c} = \sqrt{\sum_{i=r-1}^{r+1} \sum_{j= r-1}^{r+1} (e_{i,j} - e_{r,c})^2}
$$
We are going to loop through the raster cells and calculate TRI. To do so, we make use of `terra::focal()` It allows you to apply a function to every cell of a raster and bring a matrix of the surrounding values as an input to the function. For example `terra::focal(raster, w = 3, fun = any_function)` will pass to your `any_function` a 3 by 3 matrix of the raster values centered at the point.
Let's define a function that calculates TRI for a given matrix.
```{r define-calc-tri}
calc_tri <- function(matr) {
# matr is a length 9 matrix
center <- matr[5]
sum_squares <- sum((matr - center)^2, na.rm = TRUE)
return(sqrt(sum_squares))
}
```
Now, let's calculate TRI.
```{r calc-tri}
tri_africa <-
terra::focal(
africa_dem,
w = 3,
fun = calc_tri
)
```
Here is the map of the calculated TRI.
```{r map-tri}
tm_shape(tri_africa) +
tm_raster()
```