-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_glacier_raster.R
executable file
·116 lines (93 loc) · 2.85 KB
/
create_glacier_raster.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
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: TopoCliF
# Script purpose: create a comprehensive raster from a Randolph G.I. ID
# Date: Mo Nov 19, 2018
# Author: Arne Thiemann
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# ---- pre ------------
options(stringsAsFactors = F)
lapply(c("raster", "tidyr", "dplyr"), require, character.only = T)
# ---- RGI_ID to raster function ------------
rgi2ras <- function(
RGI_ID,
# by default, slope and aspect will be computed, but no TPI or TRI
compute_slope = T,
compute_aspect = T,
compute_TPI = F,
compute_TRI = F
){
# extract glacier from RGI shapefile
glacier_shp_sub <- glacier_shp[
glacier_shp@data$RGIId == RGI_ID,
]
# select matching DEM tiles
ext_glacier <- extent(glacier_shp_sub)
selected_tiles <- tile_catalogue$path[
tile_catalogue$Xmin <= ext_glacier@xmax &
tile_catalogue$Xmax >= ext_glacier@xmin &
tile_catalogue$Ymin <= ext_glacier@ymax &
tile_catalogue$Ymax >= ext_glacier@ymin
]
# mosaic tiles to glacier DEM
if (length(selected_tiles) > 1) {
selected_tiles <- lapply(selected_tiles, raster)
selected_tiles$fun <- mean
glacier_dem <- do.call(mosaic, selected_tiles)
} else {
glacier_dem <- raster(selected_tiles)
}
# reproject DEM and glacier shape
glacier_shp_sub <- spTransform(glacier_shp_sub, CRSobj = target_projection)
glacier_dem <- projectRaster(glacier_dem, crs = target_projection) # too slow
# crop it to the glacier
glacier_dem <- glacier_dem %>%
crop(., glacier_shp_sub) %>%
mask(., glacier_shp_sub)
# create relative elevation raster
glacier_dem_relative <- (glacier_dem - cellStats(glacier_dem, min)) /
(cellStats(glacier_dem, max) - cellStats(glacier_dem, min)) * 1000
glacier_ras <- brick(
glacier_dem,
glacier_dem_relative
)
rm(glacier_dem_relative, glacier_shp_sub)
# compute selected DEM sub products
if(compute_slope){
glacier_dem_slope <- terrain(
glacier_dem,
opt = "slope",
unit = "degrees",
neighbors = 8
)
glacier_ras <- addLayer(glacier_ras, glacier_dem_slope)
rm(glacier_dem_slope)
}
if(compute_aspect){
glacier_dem_aspect <- terrain(
glacier_dem,
opt = "aspect",
unit = "degrees",
neighbors = 8
)
glacier_ras <- addLayer(glacier_ras, glacier_dem_aspect)
rm(glacier_dem_aspect)
}
if(compute_TPI){
glacier_dem_tpi <- terrain(
glacier_dem,
opt = "TPI"
)
glacier_ras <- addLayer(glacier_ras, glacier_dem_tpi)
rm(glacier_dem_tpi)
}
if(compute_TRI){
glacier_dem_tri <- terrain(
glacier_dem,
opt = "TRI"
)
glacier_ras <- addLayer(glacier_ras, glacier_dem_tri)
rm(glacier_dem_tri)
}
# put out glacier raster
return(glacier_ras)
}