-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_dem.R
480 lines (348 loc) · 15.5 KB
/
03_dem.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
library(tidyverse)
# library(osmdata)
library(sf) # sf_use_s2()
library(sp)
# library(rayshader) # To install rayshader, must install XQuartz first en mac : https://github.com/tylermorganwall/rayshader/issues/86
library(raster)
library(elevatr)
library(terrainr) # buffers
library(terra)
# library(plotly)
# library(reshape2)
# library(akima)
# library(htmlwidgets)
# library(rgl)
# library(leaflet)
# library(scales)
# BBOX DE LAS COORDILLERAS
list_files_bbox <- list.files(path = "data/inaigem/shape_tiles/",
recursive = TRUE,
full.name = TRUE, # directorio relativo raster
pattern = "\\.shp$")
list_files_bbox_sf <- lapply(list_files_bbox, FUN = sf::st_read)
# list.files.stack <- lapply(list.files, FUN = raster::stack)
# convertir a sp object cada bbox
# alay_sp <- as(alay$geometry, "Spatial")
# sf::as_Spatial(list_files_bbox_sf[[1]])
#comprobar CRS proyectar de ser necesario
# #CUENCA
#
# alay <- sf::st_read("./dem_covs/alay_grass_delim/alay_grass.shp")
#Bounding box: xmin: -70.94431 ymin: -13.89851 xmax: -70.85465 ymax: -13.77737
# alay_sp <- as(alay$geometry, "Spatial")
# mapview(alay_sp)
## DEM
# en caso ya se tenga el dem descargado
# dem_alos <- raster::raster( "./dem_alos.tif")
# aoi <- raster::stack( "./stacks_indices/imagen_1.tif")
# dem_alos_mask <- raster::crop(dem_alos, aoi)
# mapview(dem_alos_mask)
# raster::writeRaster(dem_alos_mask, "dem_alos_cuenca", format = "GTiff")
# raster::writeRaster(raster::projectRaster(from =dem_alos_mask, crs = crs(alay) ), "dem_alos_cuenca_utm19", format = "GTiff")
# raster::extent(alos)
# DESCARGAR DEM EN BASE AL BBOX DE CADA CORDILLERA
# SRTMGL3, SRTMGL1, AW3D30, and SRTM15Plus datasets
#https://dwtkns.com/srtm30m/
#API KEY OPEN TOPO d64d42bbd6e4c891134dfde0af54fcd8
set_opentopo_key("d64d42bbd6e4c891134dfde0af54fcd8")
# get_elev_raster(clip_box, expand = 0.1) # buffer de descarga en uniades del mapa
# dem_alos_shape <- get_elev_raster(alay_sp, src = "alos", clip = "bbox", expand = 0.1)# buffer de descarga en uniades del mapa
dem_bbox <- list()
for (i in 1:length(list_files_bbox)) {
dem_bbox[[i]] <- get_elev_raster(list_files_bbox_sf[[i]], #Either a data.frame of x (long) and y (lat), an sp, sf, or raster object as input
src = "alos",
clip = "bbox",
expand = 0.01)# buffer en unidades de mapa
}
#names
cordilleras_bbox_chr <- c("Blanca_tile","Central_tile","Huallanca_tile",
"Huayhuasha_tile","Huaytapallana_tile","LaRaya_tile",
"LaViuda_tile","Raura_tile","Urubamba_tile",
"Vilcabamba_tile","Vilcanota_tile")
names(dem_bbox) <- cordilleras_bbox_chr
#PLOT PRUEBA
#SIEMPRE PROBAR PRIMERO CON CORDILLERA RAURA, ES PEQUEÑA
dem_bbox[[8]] # raura?
#ALOS World 3D - 30m
# plot(dem_alos_shape)
# plot(alay_sp, add = T)
#WRITE DEMS
# raster::writeFormats() # "GTiff"
# raster::writeRaster(dem_alos, "./dem_alos", format = "GTiff" )
for (i in 1:length(dem_bbox)) {
raster::writeRaster(dem_bbox[[i]],
filename = paste0("./data/dem/dem_", cordilleras_bbox_chr[i], ".tif"),
format = "GTiff")
}
# COVRIBLES GENERDS O SAGA
# twi_saga <- raster::raster( "./dem_covs/twi.tif")
# mapview(twi_saga)
# Flow_Accumulation <- raster::raster( "./dem_covs/Flow_Accumulation.tif")
# mapview(Flow_Accumulation)
# # red de flujo
# reclass_df <- c(0, 50000, NA, # menos de 100000 m de acumulcion de flujo no es corriente de agua
# 50000, 100000, 1 , # mas de 100000 m de acumulcion de flujo si es corriente de agua
# 100000, 150000, 2,
# 150000, 300000, 3,
# 300000, Inf, 4)
#
# reclass_m <- matrix(reclass_df,
# ncol = 3,
# byrow = TRUE)
#
# water_lines <- reclassify(Flow_Accumulation,
# reclass_m)
# water_lines[water_lines == 0] <- NA
# mapview(water_lines)
# raster::writeRaster(water_lines, "./dem_covs/water_lines", format = "GTiff" )
#
# # COVRIBLES GENERDS EN GRASS
#
# flow_dir <- raster::raster( "./dem_covs/flow_dir_grass.tif")
# mapview(flow_dir)
# LOAD SAGA COVARIABLES
# crop to original dem ( sin buffer)
#stack saga covs + landsats covs
# LOAD DATA
# SAGA + DEM
#SAGA COVS
#saga_dem_Blanca_tile
saga_dem_Blanca_tile <- list.files(path = "./data/dem/saga_dem_Blanca_tile/",
recursive = TRUE,
full.name = TRUE, # directorio relativo raster
pattern = "\\.tif$")
saga_dem_Blanca_tile <- lapply(saga_dem_Blanca_tile, FUN = raster::raster)
saga_Blanca_stack <- raster::stack(saga_dem_Blanca_tile)
#saga_dem_Central_tile
saga_dem_Central_tile <- list.files(path = "./data/dem/saga_dem_Central_tile/",
recursive = TRUE,
full.name = TRUE, # directorio relativo raster
pattern = "\\.tif$")
saga_dem_Central_tile <- lapply(saga_dem_Central_tile, FUN = raster::raster)
saga_Central_stack <- raster::stack(saga_dem_Central_tile)
#saga_Huallanca_stack
saga_dem_Huallanca_tile <- list.files(path = "./data/dem/saga_dem_Huallanca_tile/",
recursive = TRUE,
full.name = TRUE, # directorio relativo raster
pattern = "\\.tif$")
saga_dem_Huallanca_tile <- lapply(saga_dem_Huallanca_tile, FUN = raster::raster)
saga_Huallanca_stack <- raster::stack(saga_dem_Huallanca_tile)
#saga_Huayhuasha_stack
saga_dem_Huayhuasha_tile <- list.files(path = "./data/dem/saga_dem_Huayhuasha_tile/",
recursive = TRUE,
full.name = TRUE, # directorio relativo raster
pattern = "\\.tif$")
saga_dem_Huayhuasha_tile <- lapply(saga_dem_Huayhuasha_tile, FUN = raster::raster)
saga_Huayhuasha_stack <- raster::stack(saga_dem_Huayhuasha_tile)
#saga_Huaytapallana_stack
saga_dem_Huaytapallana_tile <- list.files(path = "./data/dem/saga_dem_Huaytapallana_tile/",
recursive = TRUE,
full.name = TRUE, # directorio relativo raster
pattern = "\\.tif$")
saga_dem_Huaytapallana_tile <- lapply(saga_dem_Huaytapallana_tile, FUN = raster::raster)
saga_Huaytapallana_stack <- raster::stack(saga_dem_Huaytapallana_tile)
#saga_LaRaya_stack
saga_dem_LaRaya_tile <- list.files(path = "./data/dem/saga_dem_LaRaya_tile/",
recursive = TRUE,
full.name = TRUE, # directorio relativo raster
pattern = "\\.tif$")
saga_dem_LaRaya_tile <- lapply(saga_dem_LaRaya_tile, FUN = raster::raster)
saga_LaRaya_stack <- raster::stack(saga_dem_LaRaya_tile)
#saga_LaViuda_stack
saga_dem_LaViuda_tile <- list.files(path = "./data/dem/saga_dem_LaViuda_tile/",
recursive = TRUE,
full.name = TRUE, # directorio relativo raster
pattern = "\\.tif$")
saga_dem_LaViuda_tile <- lapply(saga_dem_LaViuda_tile, FUN = raster::raster)
saga_LaViuda_stack <- raster::stack(saga_dem_LaViuda_tile)
#saga_Raura_stack
saga_dem_Raura_tile <- list.files(path = "./data/dem/saga_dem_Raura_tile/",
recursive = TRUE,
full.name = TRUE, # directorio relativo raster
pattern = "\\.tif$")
saga_dem_Raura_tile <- lapply(saga_dem_Raura_tile, FUN = raster::raster)
saga_Raura_stack <- raster::stack(saga_dem_Raura_tile)
#saga_Urubamba_stack
saga_dem_Urubamba_tile <- list.files(path = "./data/dem/saga_dem_Urubamba_tile/",
recursive = TRUE,
full.name = TRUE, # directorio relativo raster
pattern = "\\.tif$")
saga_dem_Urubamba_tile <- lapply(saga_dem_Urubamba_tile, FUN = raster::raster)
saga_Urubamba_stack <- raster::stack(saga_dem_Urubamba_tile)
#saga_Vilcabamba_stack
saga_dem_Vilcabamba_tile <- list.files(path = "./data/dem/saga_dem_Vilcabamba_tile/",
recursive = TRUE,
full.name = TRUE, # directorio relativo raster
pattern = "\\.tif$")
saga_dem_Vilcabamba_tile <- lapply(saga_dem_Vilcabamba_tile, FUN = raster::raster)
saga_Vilcabamba_stack <- raster::stack(saga_dem_Vilcabamba_tile)
#saga_Vilcanota_stack
saga_dem_Vilcanota_tile <- list.files(path = "./data/dem/saga_dem_Vilcanota_tile/",
recursive = TRUE,
full.name = TRUE, # directorio relativo raster
pattern = "\\.tif$")
saga_dem_Vilcanota_tile <- lapply(saga_dem_Vilcanota_tile, FUN = raster::raster)
saga_Vilcanota_stack <- raster::stack(saga_dem_Vilcanota_tile)
# CROP
# MASK AL EXTENT DE CADA TILE ORIGINAL (SIN BUFFER)
#STACKEAR PARA TODAS LAS CORDILLERAS
saga_list_stacks <- list(saga_Blanca_stack,
saga_Central_stack,
saga_Huallanca_stack,
saga_Huayhuasha_stack,
saga_Huaytapallana_stack,
saga_LaRaya_stack,
saga_LaViuda_stack,
saga_Raura_stack,
saga_Urubamba_stack,
saga_Vilcabamba_stack,
saga_Vilcanota_stack)
# alay_latlon <- st_transform(alay, crs(dem_alos_mask)) # shapeen lan lot
# TILES
# tiles_list <- list.files(path = "./data/inaigem/shape_tiles/",
# recursive = TRUE,
# full.name = TRUE, # directorio relativo raster
# pattern = "\\.shp$")
#
# tiles_list_sf <- lapply(tiles_list, FUN = sf::read_sf)
#
#DEM
# list_files_dem <- list.files(path = "./data/dem/dems/",
# recursive = TRUE,
# full.name = TRUE, # directorio relativo raster
# pattern = "\\.tif$")
#
# dems_list <- lapply(list_files_dem, FUN = raster::raster)
# DEM ES UN POCO MAS GRANDE QUE LAS COV SAGA
# COVS SAGA SON MAS GRANDES QUE LANDSAT COVS
#
#
# # saga_Vilcanota_stack <- raster::stack(saga_dem_Vilcanota_tile)
#
#
# # MASK DEMS COVS TO TILES
#
#
# dem_mask <- function(dems_list,tiles_list_sf){
#
# # dems_list : lista de raster* objects
# # tiles_list_sf : lista de sf objects
#
# dem_msk_tile <- list() # list de dem masked (bbox de cada cordillera)
# dems_list <- lapply(dems_list, FUN = terra::rast) #convertir previamente de raster* to spatRas para funcion de terra::mask
# tiles_list_sf <- lapply(tiles_list_sf, FUN = terra::vect) ####convertir previamente de sf to spatVector para funcion de terra::mask
#
# for (i in 1:length(dems_list)) {
#
#
# dem_msk_tile[[i]] <- terra::mask(dems_list[[i]],
# tiles_list_sf[[i]])
#
# }
#
# dem_msk_tile <- lapply(dem_msk_tile, FUN = raster::raster) # volver a raster*object
# return(dem_msk_tile)
# }
#
#
# dem_mask_list <- dem_mask(dems_list,tiles_list_sf) # lista de dems masked
#
#
#
#
#
# # MASK SAGA COVS TO TILES
#
#
#
# saga_mask <- function(saga_list_stacks,tiles_list_sf){
#
# # saga_list_stacks : lista de raster*stacks objects para cada cordillera
# # tiles_list_sf : lista de sf objects
#
# saga_msk_tile <- list() # list de dem masked (bbox de cada cordillera)
# saga_list_stacks <- lapply(saga_list_stacks, FUN = terra::rast) #convertir previamente de raster* to spatRas para funcion de terra::mask
# tiles_list_sf <- lapply(tiles_list_sf, FUN = terra::vect) ####convertir previamente de sf to spatVector para funcion de terra::mask
#
# for (i in 1:length(saga_list_stacks)) {
#
#
# saga_msk_tile[[i]] <- terra::mask(saga_list_stacks[[i]],
# tiles_list_sf[[i]])
#
# }
#
# saga_msk_tile <- lapply(saga_msk_tile, FUN = raster::stack) # volver a raster*object
# return(saga_msk_tile)
# }
#
#
# saga_mask_list <- saga_mask(saga_list_stacks,tiles_list_sf) # lista de dems masked
#
# #write prueba
# raster::write (saga_mask_list[[8]], "./raura_saga_masked.tif", format = "GTif" ) # scribir rarura
#
#STACK DEM + SAGA COVS PARA CADA CORDILLERA EN UNA LISTA
# saga_mask_list[[8]]
#stacking
# dem_saga_tiled <- list()
# for (i in 1:length(dem_mask_list)) {
#
# dem_saga_tiled[[i]] <- raster::stack(dem_mask_list[[i]],
# saga_mask_list[[i]])
#
# }
#
# #cambiar nombres de covs.
#
# names_covs <- c ("DEM","ASPECT","CROSC","LONGC","MAXIC","MINIC","PLANC","PROFC","SLOPE")
#
#
# for (i in 1:length(saga_mask_list)) {
#
# names(dem_saga_tiled[[i]]) <- names_covs
#
# }
# # WRITE LISTA DE STACKS
#nombre para cade elemto de la lista ed stacks segun la cordillera de estudio
saga_cordilleras_chr <- c("Blanca_saga_dem_tiled",
"Central_saga_dem_tiled",
"Huallanca_saga_dem_tiled",
"Huayhuasha_saga_dem_tiled",
"Huaytapallana_saga_dem_tiled",
"LaRaya_saga_dem_tiled",
"LaViuda_saga_dem_tiled",
"Raura_saga_dem_tiled",
"Urubamba_saga_dem_tiled",
"Vilcabamba_saga_dem_tiled",
"Vilcanota_saga_dem_tiled"
)
names(dem_saga_tiled) <- saga_cordilleras_chr
saveRDS(dem_saga_tiled, "./data/dem/saga_dem_tiled/dem_saga_tiled.rds")
#EXPORT RAURA EJEMPLO
# raster::write (dem_saga_tiled[[8]][1], "./raura_dem_masked.tif", format = "GTif" ) # dem rarura
# raster::write (dem_saga_tiled[[8]][9], "./raura_dem_masked.tif", format = "GTif" ) # SLOPE rarura
#LANDSAT SF
#LANDSAT VI
#FINALMENTE SE TIENE UN STACK DE TODAS LAS COVARIABLES PARA CADA CORDILLERA ...
# REPROYECTAR
# latlon <- "+proj=longlat +datum=WGS84 +no_defs"
#
#
# list_files_covs_dem_ltlon <- list()
#
# for (i in 1:length(list_files_covs_dem)) {
# list_files_covs_dem_ltlon[[i]] <- raster::projectRaster(from = list_files_covs_dem[[i]],
# crs = latlon )
#
# }
# MASK AL EXTENT DE CADA DEM ORIGINAL (SIN BUFFER)
alay_latlon <- st_transform(alay, crs(dem_alos_mask)) # shapeen lan lot
list_files_covs_dem_msk <- list()
for (i in 1:length(list_files_covs_dem)) {
list_files_covs_dem_msk[[i]] <- terra::mask(list_files_covs_dem_ltlon[[i]], alay_latlon)
}
mapview(list_files_covs_dem_msk[[1]])
#STACK COVARIABLES
# dem <- terra::mask(dem_alos, alay_latlon)
covs_dem_stck <- raster::stack(list_files_covs_dem_msk)