-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculating_areas_stats.R
335 lines (279 loc) · 12.3 KB
/
calculating_areas_stats.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
library(terra)
library(rgdal)
library(tictoc)
library(reshape)
ntl_all = rast("~/data/ntl/ntl_results/aurora_correction/corrected_ntl_stack.tif")
#ntl = app(ntl_all, fun = sum)
#writeRaster(ntl, "~/data/ntl/ntl_results/aurora_correction/cum_ntls.tif")
ntl = rast("~/data/ntl/ntl_results/aurora_correction/cum_ntls.tif")
layer_shp_of_country = list("Russia" = "NAME_1",
"Sweden" = "NAME_1",
"Norway" = "NAME_1",
"Finland" = "NAME_2",
"Canada" = "NAME_1",
"USA" = "NAME_1",
"Faroe" = "NAME_0",
"Greenland" = "NAME_0",
"Iceland" = "COUNTRY")
shapefile_of_country = list("Russia" = "gadm36_RUS_1.shp",
"Sweden" = "gadm36_SWE_1.shp",
"Norway" = "gadm36_NOR_1.shp",
"Finland" = "gadm40_FIN_2.shp",
"Canada" = "gadm36_CAN_1.shp",
"USA" = "gadm36_USA_1.shp",
"Faroe" = "gadm36_FRO_0.shp",
"Greenland" = "gadm36_GRL_0.shp",
"Iceland" = "gadm40_ISL_0.shp")
regions_of_country = list(
"Russia" = c("Arkhangel'sk", "Chukot", "Kamchatka", "Karelia",
"Khanty-Mansiy","Komi", "Krasnoyarsk", "Maga Buryatdan",
"Murmansk", "Nenets","Yamal-Nenets", "Sakha"),
"Sweden" = c("Västerbotten", "Norrbotten"), # need to copy-paste the name
"Norway" = c("Troms", "Nordland", "Finnmark"),
"Finland" = c("Northern Ostrobothnia", "Lapland", "Kainuu"),
"Canada" = c("Yukon", "Northwest Territories", "Nunavut"),
"USA" = c("Alaska"),
"Faroe" = "Faroe Islands",
"Greenland" = "Greenland",
"Iceland" = "Iceland"
)
## Larger regions:
## Russia: all regs combined
country = "Russia"
country_shp = readOGR(paste0("~/data/ntl/regions_shapefiles/",
shapefile_of_country[country]))
reg_names = regions_of_country[[country]]
reg_shp_rus = country_shp[country_shp[[layer_shp_of_country[[country]]]] %in%
reg_names, 2]
# ----- North America
## North America - Canda: all regs + USA: Alasks
country = "Canada"
country_shp = readOGR(paste0("~/data/ntl/regions_shapefiles/",
shapefile_of_country[country]))
reg_names = regions_of_country[[country]]
reg_shp1 = country_shp[country_shp[[layer_shp_of_country[[country]]]] %in%
reg_names, 2]
country = "USA"
country_shp = readOGR(paste0("~/data/ntl/regions_shapefiles/",
shapefile_of_country[country]))
reg_names = regions_of_country[[country]]
reg_shp2 = country_shp[country_shp[[layer_shp_of_country[[country]]]] %in%
reg_names, 2]
reg_shp_na = rbind(reg_shp1, reg_shp2)
# ----- European Arctic
## Europe Faroe + Iceland + Finland: all regs + Norway: all regs + Sweden
country = "Faroe"
country_shp = readOGR(paste0("~/data/ntl/regions_shapefiles/",
shapefile_of_country[country]))
reg_shp1 = country_shp[country_shp[[layer_shp_of_country[[country]]]] %in%
regions_of_country[[country]], 2]
country = "Iceland"
country_shp = readOGR(paste0("~/data/ntl/regions_shapefiles/",
shapefile_of_country[country]))
reg_shp2 = country_shp[country_shp[[layer_shp_of_country[[country]]]] %in%
regions_of_country[[country]], 2]
country = "Finland"
country_shp = readOGR(paste0("~/data/ntl/regions_shapefiles/",
shapefile_of_country[country]))
reg_shp3 = country_shp[country_shp[[layer_shp_of_country[[country]]]] %in%
regions_of_country[[country]], 2]
country = "Norway"
country_shp = readOGR(paste0("~/data/ntl/regions_shapefiles/",
shapefile_of_country[country]))
reg_shp4 = country_shp[country_shp[[layer_shp_of_country[[country]]]] %in%
regions_of_country[[country]], 2]
country = "Sweden"
country_shp = readOGR(paste0("~/data/ntl/regions_shapefiles/",
shapefile_of_country[country]))
reg_shp5 = country_shp[country_shp[[layer_shp_of_country[[country]]]] %in%
regions_of_country[[country]], 2]
reg_shp_eu = rbind(vect(reg_shp1), vect(reg_shp2), vect(reg_shp3),
vect(reg_shp4), vect(reg_shp5))
country = "Greenland"
country_shp = readOGR(paste0("~/data/ntl/regions_shapefiles/",
shapefile_of_country[country]))
reg_shp_gr = country_shp[country_shp[[layer_shp_of_country[[country]]]] %in%
regions_of_country[[country]], 2]
reg_shp_un_pa = rbind(vect(reg_shp_rus), reg_shp_eu, vect(reg_shp_na), vect(reg_shp_gr))
clip_to_shape = function(orig_raster, reg_shp, vectr = F) {
cr = crop(orig_raster, ext(reg_shp))
if (vectr) cl = mask(cr, reg_shp) else cl = mask(cr, vect(reg_shp))
return(cl)
}
area_calc = function(rastr) {
ar = cellSize(rastr, unit = "km", mask = T, lyrs = T)
return(sum(values(ar), na.rm = T))
}
# Lit area
lit_area_calc = function(rastr) {
cll = rastr
cll[cll == 0] = NA
arl = cellSize(cll, unit = "km", mask = T, lyrs = T)
return(sum(values(arl), na.rm = T))
}
newly_lit_area_calc = function(rastr, rastr_base_year) {
cln = rastr
cln[cln == 0] = NA
cln[rastr_base_year > 0] = NA
arn = cellSize(cln, unit = "km", mask = T, lyrs = T)
return(sum(values(arn), na.rm = T))
}
lit_area_inc_dec = function(rastr, total_area_loc) {
cli = rastr
cli[cli <= 0] = NA
ari = cellSize(cli, unit = "km", mask = T, lyrs = T)
increase_area = sum(values(ari), na.rm = T)
cld = rastr
cld[cld >= 0] = NA
ard = cellSize(cld, unit = "km", mask = T, lyrs = T)
decrease_area = sum(values(ard), na.rm = T)
cat("increase_area", round(100*increase_area/total_area_loc,2), "%\n")
cat("decrease_area", round(100*decrease_area/total_area_loc,2), "%\n")
return(c(round(100*increase_area/total_area_loc,2),
round(100*decrease_area/total_area_loc,2)))
}
final_year = rast("~/data/ntl//ntl_results/aurora_correction/corrected_ntl_stack.tif")[[22]]
lm_slope = rast("~/data/ntl//ntl_results/aurora_correction/arima_slopes_significant.tif")
lm_slope[lm_slope == 100] = NA
base_year = rast("~/data/ntl//ntl_results/aurora_correction/corrected_ntl_stack.tif")[[1]]
cnt_uls = sapply(names(regions_of_country), function(country) {
tic(country)
if (country == "Norway") { # calculating newly lit areas based on 1993 for Norway
base_year = rast("~/data/ntl//ntl_results/aurora_correction/corrected_ntl_stack.tif")[[2]]
}
country_shp = readOGR(paste0("~/data/ntl/regions_shapefiles/",
shapefile_of_country[country]))
reg_names = regions_of_country[[country]]
uls = sapply(reg_names, function(reg_name) {
print(reg_name)
regn_shp = country_shp[country_shp[[layer_shp_of_country[[country]]]] ==
reg_name, 2]
cl = clip_to_shape(ntl, regn_shp)
clb = clip_to_shape(base_year, regn_shp)
crlm = clip_to_shape(lm_slope, regn_shp)
clf = clip_to_shape(final_year, regn_shp)
total_area = area_calc(cl)
lit_area = lit_area_calc(cl)
newly_lit_area = newly_lit_area_calc(cl, clb)
annual_growth_intensity =
(sum(values(clf), na.rm = T) -
sum(values(clb), na.rm = T))/(sum(values(clb), na.rm = T)*22)
inc_dec = lit_area_inc_dec(crlm, total_area)
all_numbers = c(round(total_area),
round(lit_area),
round(100*lit_area/total_area, 2),
round(newly_lit_area),
round(100*newly_lit_area/total_area, 2),
round(100*annual_growth_intensity, 2),
inc_dec)
return(c(country, reg_name, all_numbers))
})
toc()
return(uls)
})
du = data.frame(cnt_uls)
du = as.data.frame(t(du))
colnames(du) = c("country", "region","total area", "lit area",
"percent of lit to total", "newly lit area",
"percent of newly lit to total",
"annual growth intensity",
"percent increase", "percent decrease")
for (i in 3:10) {
du[,i] = as.numeric(du[,i])
}
write.csv(du, paste0("~/data/ntl//ntl_results/aurora_correction/table_of_areas_regions.csv"), row.names = F)
base_year = rast("~/data/ntl//ntl_results/aurora_correction/corrected_ntl_stack.tif")[[2]]
print("EU excl. Greenland")
regn_shp = reg_shp_eu
cl = clip_to_shape(ntl, regn_shp, vectr = T)
clb = clip_to_shape(base_year, regn_shp, vectr = T)
crlm = clip_to_shape(lm_slope, regn_shp, vectr = T)
clf = clip_to_shape(final_year, regn_shp, vectr = T)
total_area = area_calc(cl)
lit_area = lit_area_calc(cl)
newly_lit_area = newly_lit_area_calc(cl, clb)
annual_growth_intensity =
(sum(values(clf), na.rm = T) -
sum(values(clb), na.rm = T))/(sum(values(clb), na.rm = T)*21)
inc_dec = lit_area_inc_dec(crlm, total_area)
all_numbers = c(round(total_area),
round(lit_area),
round(100*lit_area/total_area, 2),
round(newly_lit_area),
round(100*newly_lit_area/total_area, 2),
round(100*annual_growth_intensity, 2),
inc_dec)
all_numbers_eu = all_numbers
base_year = rast("~/data/ntl//ntl_results/aurora_correction/corrected_ntl_stack.tif")[[1]]
print("North America")
regn_shp = reg_shp_na
cl = clip_to_shape(ntl, regn_shp)
clb = clip_to_shape(base_year, regn_shp)
crlm = clip_to_shape(lm_slope, regn_shp)
clf = clip_to_shape(final_year, regn_shp)
total_area = area_calc(cl)
lit_area = lit_area_calc(cl)
newly_lit_area = newly_lit_area_calc(cl, clb)
annual_growth_intensity =
(sum(values(clf), na.rm = T) -
sum(values(clb), na.rm = T))/(sum(values(clb), na.rm = T)*22)
inc_dec = lit_area_inc_dec(crlm, total_area)
all_numbers = c(round(total_area),
round(lit_area),
round(100*lit_area/total_area, 2),
round(newly_lit_area),
round(100*newly_lit_area/total_area, 2),
round(100*annual_growth_intensity, 2),
inc_dec)
all_numbers_na = all_numbers
print("Russia")
regn_shp = reg_shp_rus
cl = clip_to_shape(ntl, regn_shp)
clb = clip_to_shape(base_year, regn_shp)
crlm = clip_to_shape(lm_slope, regn_shp)
clf = clip_to_shape(final_year, regn_shp)
total_area = area_calc(cl)
lit_area = lit_area_calc(cl)
newly_lit_area = newly_lit_area_calc(cl, clb)
annual_growth_intensity =
(sum(values(clf), na.rm = T) -
sum(values(clb), na.rm = T))/(sum(values(clb), na.rm = T)*22)
inc_dec = lit_area_inc_dec(crlm, total_area)
all_numbers = c(round(total_area),
round(lit_area),
round(100*lit_area/total_area, 2),
round(newly_lit_area),
round(100*newly_lit_area/total_area, 2),
round(100*annual_growth_intensity, 2),
inc_dec)
all_numbers_rus = all_numbers
print("pan-Arctic")
regn_shp = reg_shp_un_pa
cl = clip_to_shape(ntl, regn_shp, vectr = T)
clb = clip_to_shape(base_year, regn_shp, vectr = T)
crlm = clip_to_shape(lm_slope, regn_shp, vectr = T)
clf = clip_to_shape(final_year, regn_shp, vectr = T)
total_area = area_calc(cl)
lit_area = lit_area_calc(cl)
newly_lit_area = newly_lit_area_calc(cl, clb)
annual_growth_intensity =
(sum(values(clf), na.rm = T) -
sum(values(clb), na.rm = T))/(sum(values(clb), na.rm = T)*22)
inc_dec = lit_area_inc_dec(crlm, total_area)
all_numbers = c(round(total_area),
round(lit_area),
round(100*lit_area/total_area, 2),
round(newly_lit_area),
round(100*newly_lit_area/total_area, 2),
round(100*annual_growth_intensity, 2),
inc_dec)
all_numbers_arctic = all_numbers
dat = data.frame(rbind(all_numbers_arctic, all_numbers_rus,
all_numbers_na, all_numbers_eu))
colnames(dat) = c("total area", "lit area",
"percent of lit to total", "newly lit area",
"percent of newly lit to total",
"annual growth intensity",
"percent increase", "percent decrease")
dat$region = c("pan-Arctic","Russia", "North America", "EU excl. Greenland")
write.csv(dat, "~/data/ntl//ntl_results/aurora_correction/table_of_areas.csv")