-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_MergeExtracts.R
66 lines (55 loc) · 2.01 KB
/
01_MergeExtracts.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
library(gdalUtils)
library(terra)
library(assertthat)
makeFiles <- function(input, ofname, r = "average", ot = "Int32"){
assertthat::assert_that(
all(file.exists(input)),
is.character(ofname)
)
ot <- match.arg(ot, c("Byte","Int16","UInt16","UInt32","Int32","Float32",
"Float64","CInt16","CInt32","CFloat32","CFloat64"),several.ok = FALSE)
gdalbuildvrt(gdalfile = input,
output.vrt = "out.vrt",
resolution = "highest",
separate = FALSE,
r = r)
# Now combine save
gdal_translate(src_dataset = "out.vrt",
dst_dataset = ofname,
ot = ot,
r = r,
co = c("COMPRESS=DEFLATE","PREDICTOR=2","ZLEVEL=9"),
verbose = TRUE
)
file.remove("out.vrt")
invisible()
}
# --- #
# Process each extracted file, merging them and then delete it
# Load files from extract folders and project
ll <- list.files("extracts/",full.names = TRUE)
ll <- ll[has_extension(ll, "tif")]
# First land area in m2
ll_land <- ll[grep("landarea",ll)]
makeFiles(ll_land, "extracts/landarea.tif")
sapply(ll_land, function(z) file.remove(z))
# Hansen Forest gain
ll_hansen <- ll[grep("Hansen_forestgain",ll)]
makeFiles(ll_hansen, "extracts/Hansen_forestgain.tif")
sapply(ll_hansen, function(z) file.remove(z))
# ESACCI Forest gain
ll_esacci <- ll[grep("ESACCI_forestgain-", ll)]
makeFiles(ll_esacci, "extracts/ESACCI_forestgain.tif")
sapply(ll_esacci, function(z) file.remove(z))
# ESACCI forest gain sum
ll_esacci <- ll[grep("ESACCI_forestgainsum", ll)]
makeFiles(ll_esacci, "extracts/ESACCI_forestgainsum.tif")
sapply(ll_esacci, function(z) file.remove(z))
# MODIS Forest gain
ll_modis <- ll[grep("MODIS_forestgain-", ll)]
makeFiles(ll_modis, "extracts/modis_forestgain.tif")
sapply(ll_modis, function(z) file.remove(z))
# ESACCI forest gain sum
ll_modis <- ll[grep("MODIS_forestgainsum", ll)]
makeFiles(ll_modis, "extracts/modis_forestgainsum.tif")
sapply(ll_modis, function(z) file.remove(z))