-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2a_HRA_habitat_2016_scenario_2020_results_processing.Rmd
237 lines (191 loc) · 12.1 KB
/
2a_HRA_habitat_2016_scenario_2020_results_processing.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
---
title: "Post-processing & summarizing the outputs from the Habitat Risk Assessment (HRA) model using the habitat maps from 2016 - Scenario 2020"
author: "Jade Delevaux"
created: September 2022
output:
html_document:
df_print: paged
---
To run this script, install the packages below
```{r Install packages needed for this script}
library(sf)
library(foreign)
library(raster)
library(rgdal)
library(dplyr)
library(exactextractr)
library(ggplot2)
library(gridExtra)
library(cowplot)
library(rgeos)
library(sp)
library(ggpubr)
library(tigris)
library(maptools)
library(reshape)
```
Note: The working directory (WD) needs to reflect your local machine. Use the function "Find and replace" in R studio to replace the root of the working directory by your local path (e.g., "E:/GreenFin") to apply this script.
# A/ Habitat 2016 - Stressors 2020: Create a map per habitat risk class
HRA outputs a risk raster map for each habitat (corals, seagrass, and mangroves), which classifies the level of risk as: no risk (0), low risk (1), medium risk (2), and high risk (3). To quantify the area (km2) of habitat under each risk level, we need to split the raster per risk level (low, medium, high) for each habitat type.
## 1/ Coral habitat
These codes will create three integer raster map for each each risk class of the coral habitat map.
Coral habitat at low, medium, high risk:
```{r Split the risk map for coral habitat into 3 seperate rasters}
reclass_risk_corals <- raster("E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/outputs/RECLASS_RISK_corals.tif")
risk_corals_none <- reclass_risk_corals==0
plot(risk_corals_none)
writeRaster(risk_corals_none, filename="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/risk_none_corals.tif", overwrite=TRUE)
risk_corals_low <- reclass_risk_corals==1
plot(risk_corals_low)
writeRaster(risk_corals_low, filename="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/risk_low_corals.tif", overwrite=TRUE)
risk_corals_med <- reclass_risk_corals==2
plot(risk_corals_med)
writeRaster(risk_corals_med, filename="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/risk_med_corals.tif", overwrite=TRUE)
risk_corals_hig <- reclass_risk_corals==3
plot(risk_corals_hig)
writeRaster(risk_corals_hig, filename="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/risk_hig_corals.tif", overwrite=TRUE)
```
## 2/ Seagrass habitat
These codes will create three integer raster map for each each risk class of the seagrass habitat map.
Seagrass habitat at low, medium, and high risk:
```{r Split the risk map for seagrass habitat into 3 seperate rasters}
reclass_risk_seagrass <- raster("E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/outputs/RECLASS_RISK_seagrass.tif")
risk_seagrass_none <- reclass_risk_seagrass==0
plot(risk_seagrass_none)
writeRaster(risk_seagrass_none, filename="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/risk_none_seagrass.tif", overwrite=TRUE)
risk_seagrass_low <- reclass_risk_seagrass==1
plot(risk_seagrass_low)
writeRaster(risk_seagrass_low, filename="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/risk_low_seagrass.tif", overwrite=TRUE)
risk_seagrass_med <- reclass_risk_seagrass==2
plot(risk_seagrass_med)
writeRaster(risk_seagrass_med, filename="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/risk_med_seagrass.tif", overwrite=TRUE)
risk_seagrass_hig <- reclass_risk_seagrass==3
plot(risk_seagrass_hig)
writeRaster(risk_seagrass_hig, filename="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/risk_hig_seagrass.tif", overwrite=TRUE)
```
## 3/ Mangroves habitat
These codes will create three integer raster map for each each risk class of the mangroves habitat map.
Mangroves habitat at low, medium and high risk:
```{r Split the risk map for mangroves habitat into 3 seperate rasters}
reclass_risk_mangroves <- raster("E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/outputs/RECLASS_RISK_mangroves.tif")
risk_mangroves_none <- reclass_risk_mangroves==0
plot(risk_mangroves_none)
writeRaster(risk_mangroves_none, filename="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/risk_none_mangroves.tif", overwrite=TRUE)
risk_mangroves_low <- reclass_risk_mangroves==1
plot(risk_mangroves_low)
writeRaster(risk_mangroves_low, filename="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/risk_low_mangroves.tif", overwrite=TRUE)
risk_mangroves_med <- reclass_risk_mangroves==2
plot(risk_mangroves_med)
writeRaster(risk_mangroves_med, filename="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/risk_med_mangroves.tif", overwrite=TRUE)
risk_mangroves_hig <- reclass_risk_mangroves==3
plot(risk_mangroves_hig)
writeRaster(risk_mangroves_hig, filename="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020/risk_hig_mangroves.tif", overwrite=TRUE)
```
# B/ Habitat 2016 - Stressors 2020: Calculate the habitat area under each risk class per planning unit
Note that to calculate the habitat area we need to multiply the raster values by 0.25 because: 1) we sum the grid cells from HRA at 500m x 500m = 250,000 m2 and 2) we want to convert those values to km2.
## 1/ Coral habitat
We import the coral habitat map which was subdivided by coastal planning units so we can summarize the values by planning units and at the country scale.
```{r Import the coral habitat shapefile subdivided by coastal planning units}
corals <- st_read("E:/GreenFin/01_hra_model/_base/cpu_corals_2016.shp")
```
We run a zonal statistic on the individual habitat risk rasters created in A.1 to calculate the total area in km2 of coral habitat under each risk low, medium, and high, per coastal planning unit.
```{r Calculate the area (km2) of habitat under different risk by plannig unit}
corals$non_km2 <- exact_extract(risk_corals_none, corals, 'sum')
corals$non_km2 <- round(corals$non_km2*0.25, digits = 2)
corals$low_km2 <- exact_extract(risk_corals_low, corals, 'sum')
corals$low_km2 <- round(corals$low_km2*0.25, digits = 2)
corals$med_km2 <- exact_extract(risk_corals_med, corals, 'sum')
corals$med_km2 <- round(corals$med_km2*0.25, digits = 2)
corals$hig_km2 <- exact_extract(risk_corals_hig, corals, 'sum')
corals$hig_km2 <- round(corals$hig_km2*0.25, digits = 2)
```
Then we convert the total km2 of habitat under different risk class into percent change from the total coastal planning unit area.
```{r Convert the km2 into percent change of the total plannig unit area}
corals$non_pct <- round(corals$non_km2/corals$cpu_km2*100, digits = 2)
corals$low_pct <- round(corals$low_km2/corals$cpu_km2*100, digits = 2)
corals$med_pct <- round(corals$med_km2/corals$cpu_km2*100, digits = 2)
corals$hig_pct <- round(corals$hig_km2/corals$cpu_km2*100, digits = 2)
```
We export the shapefile and convert it to a csv table.
```{r Export the shapefile and a csv file}
corals_sp <- as(corals, "Spatial")
class(corals_sp)
writeOGR(obj=corals_sp, dsn="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020", layer="risk_summary_corals", driver="ESRI Shapefile", overwrite_layer = T)
# convert to dataframe
corals_risk_summary <- as.data.frame(corals_sp)
names(corals_risk_summary)
# Write the csv file
write.csv(corals_risk_summary, file.path("E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020", paste("risk_summary_corals.csv")), row.names = F)
```
## 2/ Seagrass habitat
We import the seagrass habitat map which was subdivided by coastal planning units so we can summarize the values by planning units and at the country scale.
```{r Import the seagrass habitat shapefile subdivided by coastal planning units}
seagrass <- st_read("E:/GreenFin/01_hra_model/_base/cpu_seagrass_2016.shp")
```
We run a zonal statistic on the individual habitat risk rasters created in A.1 to calculate the total area in km2 of seagrass habitat under each risk low, medium, and high, per coastal planning unit.
```{r Calculate the area (km2) of habitat under different risk by plannig unit}
seagrass$non_km2 <- exact_extract(risk_seagrass_none, seagrass, 'sum')
seagrass$non_km2 <- round(seagrass$non_km2*0.25, digits = 2)
seagrass$low_km2 <- exact_extract(risk_seagrass_low, seagrass, 'sum')
seagrass$low_km2 <- round(seagrass$low_km2*0.25, digits = 2)
seagrass$med_km2 <- exact_extract(risk_seagrass_med, seagrass, 'sum')
seagrass$med_km2 <- round(seagrass$med_km2*0.25, digits = 2)
seagrass$hig_km2 <- exact_extract(risk_seagrass_hig, seagrass, 'sum')
seagrass$hig_km2 <- round(seagrass$hig_km2*0.25, digits = 2)
```
Then we convert the total km2 of habitat under different risk class into percent change from the total coastal planning unit area.
```{r Convert the km2 into percent change of the total plannig unit area}
seagrass$non_pct <- round(seagrass$non_km2/seagrass$cpu_km2*100, digits = 2)
seagrass$low_pct <- round(seagrass$low_km2/seagrass$cpu_km2*100, digits = 2)
seagrass$med_pct <- round(seagrass$med_km2/seagrass$cpu_km2*100, digits = 2)
seagrass$hig_pct <- round(seagrass$hig_km2/seagrass$cpu_km2*100, digits = 2)
```
We export the shapefile and convert it to a csv table.
```{r Export the shapefile and a csv file}
seagrass_sp <- as(seagrass, "Spatial")
class(seagrass_sp)
writeOGR(obj=seagrass_sp, dsn="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020", layer="risk_summary_seagrass", driver="ESRI Shapefile", overwrite_layer = T)
# convert to dataframe
seagrass_risk_summary <- as.data.frame(seagrass_sp)
names(seagrass_risk_summary)
# Write the csv file
write.csv(seagrass_risk_summary, file.path("E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020", paste("risk_summary_seagrass.csv")), row.names = F)
```
## 3/ Mangroves habitat
We import the mangroves habitat map which was subdivided by coastal planning units so we can summarize the values by planning units and at the country scale.
```{r Import the mangroves habitat shapefile subdivided by coastal planning units}
mangroves <- st_read("E:/GreenFin/01_hra_model/_base/cpu_mangroves_2016.shp")
```
We run a zonal statistic on the individual habitat risk rasters created in A.1 to calculate the total area in km2 of mangroves habitat under each risk low, medium, and high, per coastal planning unit.
```{r Calculate the area (km2) of habitat under different risk by plannig unit}
mangroves$non_km2 <- exact_extract(risk_mangroves_none, mangroves, 'sum')
mangroves$non_km2 <- round(mangroves$non_km2*0.25, digits = 2)
mangroves$low_km2 <- exact_extract(risk_mangroves_low, mangroves, 'sum')
mangroves$low_km2 <- round(mangroves$low_km2*0.25, digits = 2)
mangroves$med_km2 <- exact_extract(risk_mangroves_med, mangroves, 'sum')
mangroves$med_km2 <- round(mangroves$med_km2*0.25, digits = 2)
mangroves$hig_km2 <- exact_extract(risk_mangroves_hig, mangroves, 'sum')
mangroves$hig_km2 <- round(mangroves$hig_km2*0.25, digits = 2)
```
Then we convert the total km2 of habitat under different risk class into percent change from the total coastal planning unit area.
```{r Convert the km2 into percent change of the total plannig unit area}
mangroves$non_pct <- round(mangroves$non_km2/mangroves$cpu_km2*100, digits = 2)
mangroves$low_pct <- round(mangroves$low_km2/mangroves$cpu_km2*100, digits = 2)
mangroves$med_pct <- round(mangroves$med_km2/mangroves$cpu_km2*100, digits = 2)
mangroves$hig_pct <- round(mangroves$hig_km2/mangroves$cpu_km2*100, digits = 2)
```
We export the shapefile and convert it to a csv table.
```{r Export the shapefile and a csv file}
mangroves_sp <- as(mangroves, "Spatial")
class(mangroves_sp)
writeOGR(obj=mangroves_sp, dsn="E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020", layer="risk_summary_mangroves", driver="ESRI Shapefile", overwrite_layer = T)
# convert to dataframe
mangroves_risk_summary <- as.data.frame(mangroves_sp)
names(mangroves_risk_summary)
# Write the csv file
write.csv(mangroves_risk_summary, file.path("E:/GreenFin/01_hra_model/5_hra_outputs/2016_habitats/2020", paste("risk_summary_mangroves.csv")), row.names = F)
```
We completed this section so we can clear our working environment
```{r Clear the working environment}
rm(list = ls(all.names=TRUE))
```