forked from camerynbrock/ci_impact_indicators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ae_2_preprocess.R
162 lines (139 loc) · 5.99 KB
/
ae_2_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
library(sf)
library(tidyverse)
library(raster)
library(terra)
library(fasterize)
library(gdalUtils)
library(foreach)
data_folder <- 'data/avoided_emissions'
template <- rast(paste0(data_folder, "/land_1km_eck4.tif"))
load_as_vrt <- function(folder, pattern, band=FALSE, raster=TRUE) {
vrt_file <- tempfile(fileext='.vrt')
files <- list.files(folder, pattern=pattern, full.names = TRUE)
if (length(files) == 0) {
stop('No files found')
}
if (band) {
gdalbuildvrt(paste0(folder, '/', files), vrt_file, b=band)
r <- raster(vrt_file)
} else {
gdalbuildvrt(paste0(folder, '/', files), vrt_file)
r <- raster::stack(vrt_file)
}
if (raster) {
return(r)
} else {
return(vrt_file)
}
}
# Function used to get IDs from a rasterized set of polygons (to determine
# which polygons were lost due to rasterization (very small polygons drop out)
get_unique <- function(x) {
bs <- raster::blockSize(x)
n_blocks <- bs$n
for (block_num in 1:n_blocks) {
these_vals <- unique(raster::getValues(x,
row=bs$row[block_num],
nrows=bs$nrows[block_num]))
if (block_num == 1) {
out <- these_vals
} else {
out <- unique(c(out, these_vals))
}
}
return (na.omit(out))
}
###############################################################################
# Load covariates
covariates_1 <- raster::stack(
list.files(file.path(data_folder), pattern = "covariate1", full.names = TRUE))
names(covariates_1) <- c('biome',
'elev',
'ecoregion',
'precip',
'slope',
'temp')
NAvalue(covariates_1) <- -32768
covariates_2 <- raster::stack(
list.files(file.path(data_folder), pattern = "covariate2",
full.names = TRUE))
names(covariates_2) <- c('dist_cities',
'crop_suitability',
'dist_roads',
'pa')
NAvalue(covariates_2) <- -32768
population <- raster::stack(
list.files("data/population", pattern = "_proj", full.names = TRUE))
names(population) <-
c('pop_2000', 'pop_2005', 'pop_2010', 'pop_2015', 'pop_2020')
NAvalue(population) <- -32768
biomass <- raster('data/avoided_emissions/covariate_biomass.tif')
names(biomass) <- c('total_biomass')
population_growth <- raster(
'data/avoided_emissions/covariate_population_growth.tif')
names(population_growth) <- c('pop_growth')
covariates <- stack(covariates_1, covariates_2, biomass, population,
population_growth)
writeRaster(
covariates,
filename='data/avoided_emissions/covariates_covariates.tif',
overwrite=TRUE, options="COMPRESS=LZW", datatype="INT2S")
write_csv(data.frame(names=names(covariates)), 'data/avoided_emissions/covariates_covariates.csv')
lc_2000 <- raster::stack("data/avoided_emissions/covariate_lc2000.tif")
names(lc_2000) <- c('lc_2000_forest',
'lc_2000_grassland',
'lc_2000_agriculture',
'lc_2000_wetlands',
'lc_2000_artificial',
'lc_2000_other',
'lc_2000_water')
writeRaster(lc_2000, filename='data/covariates_lc_2000.tif',
overwrite=TRUE, options="COMPRESS=LZW", datatype="INT2S")
lc_2015 <- raster::stack("data/avoided_emissions/covariate_lc2015.tif")
names(lc_2015) <- c('lc_2015_forest',
'lc_2015_grassland',
'lc_2015_agriculture',
'lc_2015_wetlands',
'lc_2015_artificial',
'lc_2015_other',
'lc_2015_water')
writeRaster(lc_2015, filename='data/avoided_emissions/covariates_lc_2015.tif',
overwrite=TRUE, options="COMPRESS=LZW", datatype="INT2S")
write_csv(data.frame(names=names(lc_2015)),
'data/avoided_emissions/covariates_lc_2015.csv')
# forest cover
fc <- raster::stack(list.files(data_folder, pattern = "forest_cover_20",
full.names = TRUE))
NAvalue(fc) <- -32768
writeRaster(fc, filename='data/avoided_emissions/covariates_fc.tif',
overwrite=TRUE, options="COMPRESS=LZW", datatype="INT2S")
write_csv(data.frame(names=names(fc)), 'data/avoided_emissions/covariates_fc.csv')
# forest cover change
fcc <- raster::stack(list.files(data_folder, pattern = "forest_cover_change_20",
full.names = TRUE))
NAvalue(fcc) <- -32768
writeRaster(fcc, filename='data/avoided_emissions/covariates_fc_change.tif',
overwrite=TRUE, options="COMPRESS=LZW", datatype="INT2S")
write_csv(data.frame(names=names(fcc)), 'data/avoided_emissions/covariates_fc_change.csv')
###############################################################################
### Load GADM boundaries
regions <- st_read("data/avoided_emissions/gadm_410-levels.gpkg"
, layer = "ADM_1") %>%
st_transform(crs(template))
regions$level0_ID <- as.numeric(factor(regions$GID_0))
regions$level1_ID <- as.numeric(factor(regions$GID_1))
regions_rast <- fasterize(regions, raster(template),
field = 'level1_ID')
region_IDs_after_rasterization <- get_unique(regions_rast)
regions <- regions[regions$level1_ID %in% region_IDs_after_rasterization, ]
regions$level0_ID <- as.numeric(factor(as.character(regions$GID_0)))
regions$level1_ID <- as.numeric(factor(as.character(regions$GID_1)))
# Now re-rasterize boundaries (with ID's that will disappear dropped) to ensure
# that all IDs are sequential and that they match between the data.frame and
# the raster.
regions_rast <- fasterize(regions, raster(template),
field='level1_ID')
names(regions_rast) <- 'region'
region_IDs_after_rasterization <- get_unique(regions_rast)
stopifnot(sort(region_IDs_after_rasterization) == sort(regions$level1_ID))
saveRDS(regions, file='data/avoided_emissions/regions.RDS')