-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis_plateaus.R
executable file
·305 lines (228 loc) · 9.75 KB
/
analysis_plateaus.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
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: TopoCliF
# Script purpose: Plateau detection
# Date: Mo Mar 19, 2019
# Author: Arne Thiemann
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# ---- source plateau detection function ----
source("analysis_plateaus_f_get-plateaus.R")
# ---- apply detection and derive metrics ----
# loop slope thresholds
for (i_slope in metric_slope_limit) {
# loop clump thresholds
for (i_clump in metric_clump_size_limit) {
# avoid metrics derivation from previous raster, if creation fails
intm_ras <- NULL
# create intermediate raster
intm_ras <- get_plateaus(
ras_elev_abs = temp_ras[["elev_absolute"]],
ras_slope = temp_ras[["slope"]],
plateau_min_size_sqm = i_clump,
slope_threshold_deg = i_slope,
ela_absolute = ela_calculated
)
# derive included metrics from plateau detection function
index[i, paste0("m_s", i_slope, "_c", i_clump, "_plateau_elevation_min")] <- intm_ras$metrics_result$plateau_min_elevation
index$plateau_elevation_max[i] <- intm_ras$metrics_result$plateau_max_elevation
index[i, paste0("m_s", i_slope, "_c", i_clump, "_plateau_elevation_range")] <- intm_ras$metrics_result$plateau_max_elevation -
intm_ras$metrics_result$plateau_min_elevation
# sourced out metrics
index[i, paste0("m_s", i_slope, "_c", i_clump, "_plateau_elevation_mean")] <- cellStats(
intm_ras$raster_plateau_dem, "mean"
)
index[i, paste0("m_s", i_slope, "_c", i_clump, "_plateau_elevation_sd")] <- cellStats(
intm_ras$raster_plateau_dem, "sd"
)
index[i, paste0("m_s", i_slope, "_c", i_clump, "_plateau_elevation_skewness")] <- e1071::skewness(
na.omit(as.data.frame(intm_ras$raster_plateau_dem))[,1],
type = 3
)
# absolute area
resulting_areas <- NULL
resulting_areas <- raster::freq(intm_ras$raster_result_classified) %>%
as.data.frame() %>%
mutate(count = count * prod(res(intm_ras$raster_result_classified)))
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_flat")] <- sum(
resulting_areas$count[resulting_areas$value %in% c(1, 101, 111)]
)
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_plateau_elevation_band")] <- sum(
resulting_areas$count[resulting_areas$value %in% c(100, 101, 111)]
)
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_flat_in_plateau_elevation_band")] <- sum(
resulting_areas$count[resulting_areas$value %in% c(101, 111)]
)
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_plateau")] <- resulting_areas$count[
resulting_areas$value == 111 & !is.na(resulting_areas$value)]
# area comparison
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_rel_flat_to_glacier")] <- (
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_flat")] /
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_glacier")]
) * 100
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_rel_plateau_to_glacier")] <- (
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_plateau")] /
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_glacier")]
) * 100
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_rel_plateau_elevation_band_to_glacier")] <- (
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_plateau_elevation_band")] /
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_glacier")]
) * 100
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_rel_flat_in_plateau_elevation_band_to_glacier")] <- (
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_flat_in_plateau_elevation_band")] /
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_glacier")]
) * 100
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_rel_plateau_to_flat")] <- (
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_plateau")] /
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_flat")]
) * 100
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_rel_flat_to_plateau_elevation_band")] <- (
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_flat")] /
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_plateau_elevation_band")]
) * 100
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_rel_flat_in_plateau_elevation_band_to_flat")] <- (
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_flat_in_plateau_elevation_band")] /
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_flat")]
) * 100
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_rel_plateau_to_plateau_elevation_band")] <- (
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_plateau")] /
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_plateau_elevation_band")]
) * 100
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_rel_plateau_to_flat_in_plateau_elevation_band")] <- (
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_plateau")] /
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_flat_in_plateau_elevation_band")]
) * 100
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_rel_flat_in_plateau_elevation_band_to_plateau_elevation_band")] <- (
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_flat_in_plateau_elevation_band")] /
index[i, paste0("m_s", i_slope, "_c", i_clump, "_area_absolute_plateau_elevation_band")]
) * 100
###### ToDo: add more, corresponding to matrix
# save raster, if set
if (save_plateau_rasters) {
dir.create(paste0(
plateau_rasters_output_directory,
"/plateau_detection/", i_slope, "_", i_clump, "/"
))
writeRaster(
intm_ras,
filename = paste0(
plateau_rasters_output_directory,
"/plateau_detection/", i_slope, "_", i_clump, "/",
input$RGI_alias[i], ".tif"
)
)
}
# plot raster if figure creation is set (syntax matching to other outputs)
if (plot_plateau_detection_figures) {
#### not implemented yet
}
}
}
# test application of function
if(F){
TESCHT <- get_plateaus(
ras_elev_abs = temp_ras[["elev_absolute"]],
ras_slope = temp_ras[["slope"]],
plateau_min_size_sqm = 80^2,
slope_threshold_deg = 5,
ela_absolute = ela_calculated
)
plot(TESCHT$raster_Result)
freq(TESCHT$raster_Result)
# value count
# 0 17208 # glacier
# 1 10 # flat spots on glacier
# 100 47088 # plateau elevation band
# 101 882 # flat spot on plateau elevation band, but too small
# 111 3408 # plateau
# NA 97868 # background
# ---- multiple apply plateau function ----
names(TESCHT$raster_Result) <- "plateaus"
test_RE_stack <- stack(temp_ras, TESCHT$raster_Result)
plot(test_RE_stack)
for (i_slope in metric_slope_limit) {
for (i_clump in metric_clump_size_limit) {
assign(
paste0("raster_", i_slope, "_", i_clump),
get_plateaus(
ras_elev_abs = temp_ras[["elev_absolute"]],
ras_slope = temp_ras[["slope"]],
slope_threshold_deg = i_slope,
ras_elev_rel = temp_ras[["elev_relative"]],
ela_relative = ela_assumed,
plateau_min_size_sqm = i_clump
)
)
plateau_geotable <- rbind(plateau_geotable, get_plateaus(
ras_slope = temp_ras[["slope"]],
slope_threshold_deg = i_slope,
ras_elev_rel = temp_ras[["elev_relative"]],
ela_relative = ela_assumed,
plateau_min_size_sqm = i_clump
)%>%
as(., "SpatialPixelsDataFrame") %>%
as.data.frame() %>%
mutate(
class = ifelse(
layer == 0,
"glacier",
ifelse(
layer == 1,
"below slope limit",
ifelse(
layer == 2,
"plateau detected",
"ERROR"
)
)
)
) %>%
mutate(
slope_limit = i_slope,
clump_size_limit = i_clump
)
)
}
}
# check option of df processing vs geoprocessing... matching via coordinates
# PLACEHOLDER: Detect equal-elevation zones and delineate them
plateau_geotable$class <- factor(
plateau_geotable$class,
levels = c("glacier", "below slope limit", "plateau detected")
)
png(
filename = paste0(
plot_output_path,
index$RGI_alias[i],
"_21_plateau_detection_thresholds.png"
),
width = 1920,
height = 1200,
units = "px",
res = 120
)
as.data.frame(temp_ras[["elev_absolute"]])
print(ggplot() +
geom_raster(
data = plateau_geotable,
mapping = aes(x = x, y = y, fill = class)
) +
facet_grid(slope_limit ~ clump_size_limit) +
coord_fixed() +
theme_minimal() +
theme(
legend.position = "bottom",
legend.key.width = unit(1.5, "cm"),
axis.text.y = element_text(angle = 45, hjust = 1),
axis.title.x = element_blank(),
axis.title.y = element_blank()
) +
#geom_contour() + # integrate ELA indicator line here, later
scale_fill_manual(
values = c(
rgb(.72, .92, .98), # glaciers
rgb(.37, .47, .88), # below slope limit
"#000000" # plateau detected
)
))
dev.off()
# PLACEHOLDER: print plateau area into metrics
}