-
Notifications
You must be signed in to change notification settings - Fork 0
/
satres.R
323 lines (302 loc) · 7.78 KB
/
satres.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
.onLoad <- function(libname, pkgname) {
utils::data(
"sat_rest",
"sat_band",
"sat_rest_msk",
package = pkgname,
envir = parent.env(environment())
)
}
#' `satres` S3 class
#'
#' Creates a `satres` object from a set of raster files.
#'
#' Given a folder name or a vector of folder names, containing satellite band
#' raster files, creates an object containing all rasters grouped according to
#' their spatial resolution.
#'
#' If there are several rasters of the same area (tiles), it previously merges
#' them to form a single raster of the total area.
#'
#' A working folder where the virtual rasters are created can be indicated as a
#' parameter. Additionally, we indicate whether we wish to process only the
#' spectral band files (B1 to B12) or all available files.
#'
#' @param dir A string or string vector, folder names.
#' @param out_dir A string, output folder.
#' @param only_spectral_bands A boolean, include only spectral bands.
#'
#' @return A `satres` object.
#'
#' @family satellite definition
#' @seealso \code{\link{sat_untarzip}}
#'
#' @examples
#'
#' esa <- system.file("extdata", "esa", package = "satres")
#'
#' sr <- satres(dir = esa)
#'
#' sr <- satres(dir = esa,
#' out_dir = tempdir(),
#' only_spectral_bands = FALSE)
#'
#' @export
satres <- function(dir, out_dir = NULL, only_spectral_bands = TRUE) {
files <- NULL
for (d in dir) {
lf <-
list.files(
path = d,
pattern = "*.TIF|*.jp2",
recursive = TRUE,
full.names = TRUE,
ignore.case = TRUE
)
files <- c(files, lf)
}
if (is.null(out_dir)) {
dir <- tempdir()
sub_dir <- snakecase::to_snake_case(paste0(Sys.time()))
dir.create(file.path(dir, sub_dir))
out_dir <- paste0(dir, '/', sub_dir)
}
b_r <- select_band_files(files)
if (only_spectral_bands) {
files <- b_r[['band']]
} else {
files <- c(b_r[['band']], b_r[['rest']])
}
names <- sort(unique(names(files)))
b <- vector("list", length = length(names))
names(b) <- names
nexus <- get_nexus(out_dir)
vf <- NULL
names_1layer <- NULL
resolution <- NULL
for (n in names) {
vfn <- paste0(n, ".vrt")
f <- paste0(out_dir, nexus, vfn)
vf <- c(vf, vfn)
t <- terra::vrt(files[names(files) == n], f, overwrite = TRUE)
# only tiles of the same raster
if (terra::nlyr(t) == 1) {
b[[n]] <- t
names_1layer <- c(names_1layer, n)
resolution <- c(resolution, terra::res(t)[1])
}
}
b <- b[names_1layer]
names <- names(b)
names <- gsub("_10m", "", names)
names <- gsub("_20m", "", names)
names <- gsub("_60m", "", names)
names(b) <- names
r <- sort(unique(resolution))
b2 <- vector("list", length = length(r))
names(b2) <- paste0('r', r, 'm')
b_resolution <- paste0('r', resolution, 'm')
for (n in names(b2)) {
b2[[n]] <- transform_to_multiband(bands = b[which(b_resolution == n)])
}
structure(list(
bands = b2,
out_dir = out_dir,
virtual_files = vf
),
class = "satres")
}
#' Get spatial resolutions
#'
#' Returns the spatial resolutions of the multi-band raster that make up the object.
#'
#' @param sr A `satres` object.
#'
#' @return A vector of strings.
#'
#' @family satellite definition
#' @seealso \code{\link{sat_untarzip}}
#'
#' @examples
#'
#' esa <- system.file("extdata", "esa", package = "satres")
#' sr <- satres(dir = esa)
#'
#' r <- sr |>
#' get_spatial_resolution()
#'
#' @export
get_spatial_resolution <- function(sr)
UseMethod("get_spatial_resolution")
#' @rdname get_spatial_resolution
#' @export
get_spatial_resolution.satres <- function(sr) {
names(sr$bands)
}
#' Get band names
#'
#' Returns all names of the multi-band raster that make up the object.
#'
#' We can indicate the name of a certain spatial resolution to obtain only
#' its names.
#'
#' @param sr A `satres` object.
#' @param res A string, spatial resolution.
#'
#' @return A vector of strings.
#'
#' @family satellite definition
#' @seealso \code{\link{sat_untarzip}}
#'
#' @examples
#'
#' esa <- system.file("extdata", "esa", package = "satres")
#' sr <- satres(dir = esa, only_spectral_bands = FALSE)
#' r <- sr |>
#' get_band_names()
#'
#' @export
get_band_names <- function(sr, res)
UseMethod("get_band_names")
#' @rdname get_band_names
#' @export
get_band_names.satres <- function(sr, res = NULL) {
res <- check_spatial_resolution(sr, res)
b <- NULL
for (r in res) {
b <- c(b, names(sr$bands[[r]]))
}
sort(unique(b))
}
#' Get band names
#'
#' Returns the band names of the multi-band raster that make up the object.
#'
#' We can indicate the name of a certain spatial resolution to obtain only its
#' band names.
#'
#' @param sr A `satres` object.
#' @param res A string, spatial resolution.
#'
#' @return A vector of strings.
#'
#' @family satellite definition
#' @seealso \code{\link{sat_untarzip}}
#'
#' @examples
#'
#' esa <- system.file("extdata", "esa", package = "satres")
#' sr <- satres(dir = esa, only_spectral_bands = FALSE)
#' r <- sr |>
#' get_spectral_band_names()
#'
#' @export
get_spectral_band_names <- function(sr, res)
UseMethod("get_spectral_band_names")
#' @rdname get_spectral_band_names
#' @export
get_spectral_band_names.satres <- function(sr, res = NULL) {
b <- get_band_names(sr, res)
sbn <- sat_band_names()
sort(intersect(b, sbn))
}
################################################################
#' Transforms a list of raster bands in a raster multi-band
#'
#' To generate a multi-band raster, the definition of the vector must be
#' executed in a single instruction, which is why it must be done through
#' meta programming.
#'
#' @param bands A list of raster bands.
#'
#' @return A raster band.
#'
#' @keywords internal
transform_to_multiband <- function(bands) {
l <- length(bands)
str <- "c(bands[[1]]"
if (l > 1) {
for (i in 2:l) {
str <- paste0(str, sprintf(", bands[[%d]]", i))
}
}
str <- paste0(str, ")")
res <- eval(parse(text = str))
names(res) <- names(bands)
res
}
#' Find name to files
#'
#' Finds the name associated to a file name in a vector of named patterns.
#'
#' @param files A string vector.
#' @param patterns A string vector of values with names.
#'
#' @return A string vector.
#'
#' @keywords internal
find_name_to_files <- function(files, patterns) {
names <- names(patterns)
fn <- names(files)
for (i in 1:length(patterns)) {
r <- grepl(patterns[i], files, fixed = TRUE)
if (sum(r) > 0) {
fn[which(r)] <- names[i]
}
}
names(files) <- fn
files
}
#' Check spatial resolution
#'
#' Check if the indicated spatial resolution is one of those available.
#'
#' @param sr A `satres` object.
#' @param res A string, spatial resolution.
#' @param valid_null A boolean, resolution can be null.
#'
#' @return A string, spatial resolution.
#'
#' @keywords internal
check_spatial_resolution <-
function(sr, res = NULL, valid_null = TRUE) {
if (!valid_null) {
stopifnot("A spatial resolution must be indicated." = !is.null(res))
}
if (is.null(res)) {
res <- names(sr$bands)
} else {
res <- unique(res)
for (r in res) {
if (!(r %in% names(sr$bands))) {
stop(sprintf("The spatial resolution '%s' is not available.", r))
}
}
}
res
}
#' Check band
#'
#' Check if the indicated band is one of those available.
#'
#' @param sr A `satres` object.
#' @param res A string, spatial resolution.
#' @param bands A string, band names.
#'
#' @return boolean.
#'
#' @keywords internal
check_bands <- function(sr, res = NULL, bands = NULL) {
res_bands <- get_band_names(sr, res)
if (is.null(bands)) {
bands <- res_bands
} else {
bands <- unique(bands)
for (b in bands) {
if (!(b %in% res_bands)) {
stop(sprintf("The band '%s' is not available at the selected spatial resolutions.", b))
}
}
}
bands
}