-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_get_elevation.R
69 lines (55 loc) · 1.48 KB
/
01_get_elevation.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
library(tidyverse)
library(plyr)
library(raster)
library(rgee)
library(sf)
# Register with the gmail count in EE -------------------------------------
ee_Initialize(user = '[email protected]')
# Create new grid ---------------------------------------------------------
peru <- st_read('_gpkg/peru.gpkg') %>%
st_make_grid(n = 6)
# Downloading raster data - elevation -------------------------------------
for (i in 1:length(peru)) {
region <- peru[i] %>% sf_as_ee()
def <- ee$Image("USGS/SRTMGL1_003")$
rename(sprintf("dem_%s",i))$
clip(region)
url <- def$getDownloadUrl(
list(
name = 'elev',
image = def,
scale = 100
)
)
if (!dir.exists('PeruDEM')){
dir.create('PeruDEM')
}
download.file(
url = url,
destfile = paste0('PeruDEM/tilen_',i,'.tar'),
method = 'curl'
)
}
lista <- paste0(
"PeruDEM/",
list.files(
path = 'PeruDEM/',
pattern = '.tar')
)
# Unzip elevation data and mosaic all rasters -----------------------------
l_ply(.data = lista, .fun = unzip, exdir = 'PeruDEM/')
list_tif <- list.files(
path = "PeruDEM",
pattern = ".tif$",
full.names = TRUE
)
read_tif <- lapply(list_tif, raster)
peru <- st_read('_gpkg/peru.gpkg')
dem_peru <- do.call(merge, c(read_tif, tolerance = 1)) %>%
crop(peru) %>%
mask(peru)
# plot(dem_peru)
file.remove(lista)
file.remove(list_tif)
# Write new raster of Peru -----------------------------------------------
writeRaster(dem_peru,'PeruDEM/PeruDEM.tif')