forked from camerynbrock/ci_impact_indicators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ae_1_preprocess.R
345 lines (272 loc) · 8 KB
/
ae_1_preprocess.R
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
library(tidyverse)
library(raster)
library(terra)
library(sf)
library(geodata)
template <- rast("~/Conservation International/Data/land_1km_eck4.tif")
# SRTM30+ Global 1-km Digital Elevation Model (DEM): Version 11: Land Surface
# from https://pae-paha.pacioos.hawaii.edu/erddap/griddap/srtm30plus_v11_land.html
# need to mosaic tiles
dem_files <- list.files("data/avoided_emissions", pattern = "srtm30",
full.names = TRUE)
for (i in seq_along(dem_files)){
rast <- rast(dem_files[i])
if(i == 1){dem <- rast} else {dem <- mosaic(dem, rast)}
}
writeRaster(
dem,
"data/avoided_emissions/SRTM_DEM.tif")
# project to be able to calculate slope
dem_proj <- dem %>%
project(template,
method = "bilinear",
threads = TRUE,
mask = TRUE)
writeRaster(
dem_proj,
"data/avoided_emissions/covariate_dem.tif")
# create slope
slope <- terrain(dem_proj, "slope")
writeRaster(slope,
"data/avoided_emissions/covariate_slope.tif")
# suitability probability for cropland
# for now, mosaic from Chen
crop_suit <- rast(
"data/avoided_emissions/croplandSuitabilityProbability_Chen2022.tif") %>%
project(template,
method = "bilinear",
threads = TRUE,
mask = TRUE)
writeRaster(
crop_suit,
"covariate_cropland_suitability.tif")
precip <- rast("data/avoided_emissions/chirps-v2.0.1981-2020.40yrs.tif") %>%
project(template,
method = "bilinear",
threads = TRUE) %>%
extend(template) %>%
crop(template) %>%
mask(slope)
writeRaster(
precip,
"covariate_precipitation.tif",
overwrite = TRUE)
# temperature
# have mean per month... take average of all
temp_files <- list.files("data/avoided_emissions/wc2.1_30s_tavg/",
pattern = ".tif", full.names = TRUE)
for (i in seq_along(temp_files)){
rast <- rast(temp_files[i])
if(i == 1) {temp_stack <- rast} else {temp_stack <- c(temp_stack, rast)}
}
temp <- mean(temp_stack) %>%
project(template,
method = "bilinear",
threads = TRUE,
mask = TRUE)
writeRaster(
temp,
"data/avoided_emissions/covariate_temp.tif")
# cropland
crop <- rast("data/avoided_emissions/Global_cropland_3km_2019.tif") %>%
project(template,
method = "bilinear",
threads = TRUE,
mask = TRUE)
writeRaster(
crop,
"data/avoided_emissions/covariate_cropland.tif")
# accessibility to cities from Weiss et al
cities <- rast("data/avoided_emissions/accessibility_to_cities_2015_v1.0.tif") %>%
project(template,
method = "bilinear",
threads = TRUE,
mask = TRUE)
writeRaster(
cities,
"data/avoided_emissions/covariate_cities.tif")
wdpa_poly <- read_sf("data/avoided_emissions/global-2023-04-10.gpkg") %>%
janitor::clean_names() %>%
st_transform(crs(template)) %>%
filter(!marine == "marine") %>%
vect()
wdpa <- terra::rasterize(
x = wdpa_poly,
y = template,
cover = FALSE)
writeRaster(
wdpa,
"data/avoided_emissions/covariate_wdpa.tif")
# distance to roads
# from https://www.globio.info/download-grip-dataset
# then calculated eucdistance with template as template in ArcGIS
roads <- rast("data/avoided_emissions/GRIP4roads_eucdistance.tif") %>%
project(template,
method = "bilinear",
threads = TRUE,
mask = TRUE)
writeRaster(
roads,
"data/avoided_emissions/covariate_distroads.tif")
# administrative unit
# per GADM 1st sub-national level
gadm <- read_sf("data/avoided_emissions/gadm_410.gpkg") %>%
dplyr::select(NAME_1) %>%
st_transform(crs = crs(template)) %>%
vect() %>%
rasterize(y = template,
field = "NAME_1")
writeRaster(
gadm,
"data/avoided_emissions/covariate_gadm.tif")
# ecoregion
eco <- read_sf(
dsn = "data/avoided_emissions",
layer = "Ecoregions2017") %>%
dplyr::select(ECO_NAME) %>%
st_transform(crs(template)) %>%
vect() %>%
rasterize(y = template,
field = "ECO_NAME")
writeRaster(
eco,
"data/avoided_emissions/covariate_ecoregions.tif")
biome <- read_sf(
dsn = "data/avoided_emissions",
layer = "Ecoregions2017") %>%
dplyr::select(BIOME_NAME) %>%
st_transform(crs(template)) %>%
vect() %>%
rasterize(y = template,
field = "BIOME_NAME")
writeRaster(
biome,
"data/avoided_emissions/covariate_biome.tif")
# Population for 2000, 2005, 2010, 2015 2020 from
# https://hub.worldpop.org/geodata/listing?id=64
mask_rast <- rast("data/avoided_emissions/covariate1_slope.tif")
for(i in 1:5){
rast <- rast(paste0("data/population/ppp_",
seq(from = 2000, to = 2020, by = 5)[i],
"_1km_Aggregated.tif")) %>%
project(template,
method = "bilinear",
threads = TRUE) %>%
mask(mask_rast)
writeRaster(rast,
paste0("data/population/ppp_",
seq(from = 2000, to = 2020, by = 5)[i],
"_1km_Aggregated_proj.tif"),
overwrite = TRUE)
if(i == 1) {population <- rast } else {population <- c(population, rast)}
}
population <- raster::stack(population)
names(population) <-
c('pop_2000', 'pop_2005', 'pop_2010', 'pop_2015', 'pop_2020')
NAvalue(population) <- -32768
population_growth <-
overlay(
population$pop_2000,
population$pop_2020,
fun = function(pop_2000, pop_2020) {
r = ((pop_2020 / pop_2000) ^ (1 / 15) - 1) * 100
# Set growth to zero when 2000 and 2020
# populations are equal
r[pop_2000 == pop_2020] <- 0
# Set growth rate to maximum of other
# pixels for areas that grew from zero
# population in 2000.
isinfs <- is.infinite(r)
r[isinfs] <- 0
r[isinfs] <- max(r, na.rm = TRUE)
r
}
)
writeRaster(
population_growth,
filename = 'data/avoided_emissions/covariate_population_growth.tif',
overwrite = TRUE,
options = "COMPRESS=LZW",
datatype = "INT2S"
)
# biomass
biomass <- rast('data/carbon_stored/biomass_prepped_2022.tif') %>%
project(template,
method = "bilinear",
threads = TRUE) %>%
mask(mask_rast)
writeRaster(
biomass,
"data/avoided_emissions/covariate_biomass.tif")
# land classes
# from https://www.ncbi.nlm.nih.gov/pmc/articles/PMC8349160/ 1km agg
# downloaded from https://figshare.com/articles/dataset/Open_Synergy_Land_Cover_Series/14329025/1?file=27322868
# information here https://data.apps.fao.org/map/catalog/srv/eng/catalog.search#/metadata/b68a0adb-06da-44bb-a5aa-7d0ca95b654e
# chose because closest to land cover classes needed, and already agg to 1km
lc <- rast("data/avoided_emissions/OS2015.tif")
names(lc) <- c(
"cropland",
"forest",
"grassland",
"shrubland",
"wetland",
"water",
"tundra",
"impervious surface",
"bareland")
list <- as.list(lc)
forest <- list[[2]]
grassland <- list[[3]]
agriculture <- list[[1]]
wetlands <- list[[5]]
artificial <- list[[8]]
other <- max(list[[4]], list[[7]], list[[9]], na.rm = TRUE)
water <- list[[6]]
lc_cov <- c(forest,
grassland,
agriculture,
wetlands,
artificial,
other,
water) %>%
project(template,
method = "bilinear",
threads = TRUE,
mask = TRUE)
writeRaster(
lc_cov,
"data/avoided_emissions/covariate_lc2015.tif")
lc <- rast("data/avoided_emissions/OS2000.tif")
names(lc) <- c(
"cropland",
"forest",
"grassland",
"shrubland",
"wetland",
"water",
"tundra",
"impervious surface",
"bareland")
list <- as.list(lc)
forest <- list[[2]]
grassland <- list[[3]]
agriculture <- list[[1]]
wetlands <- list[[5]]
artificial <- list[[8]]
other <- max(list[[4]], list[[7]], list[[9]], na.rm = TRUE)
water <- list[[6]]
lc_cov <- c(forest,
grassland,
agriculture,
wetlands,
artificial,
other,
water) %>%
project(template,
method = "bilinear",
threads = TRUE,
mask = TRUE)
writeRaster(
lc_cov,
"data/avoided_emissions/covariate_lc2000.tif")
# hansen forest cover prepped in hansen_prep