-
Notifications
You must be signed in to change notification settings - Fork 76
/
api_source_mpc.R
577 lines (541 loc) · 18.8 KB
/
api_source_mpc.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#' @title Test access to collection in MPC
#' @keywords internal
#' @noRd
#' @description
#' These functions provide an API to handle/retrieve data from source's
#' collections.
#'
#' @param source Data source.
#' @param collection Image collection.
#' @param bands Band names
#' @param ... Other parameters to be passed for specific types.
#' @param start_date Start date.
#' @param end_date End date.
#' @param dry_run TRUE/FALSE
#' @return Called for side effects
#' @export
.source_collection_access_test.mpc_cube <- function(source,
collection,
bands, ...,
start_date = NULL,
end_date = NULL,
dry_run = TRUE) {
# require package
.check_require_packages("rstac")
items_query <- .stac_create_items_query(
source = source,
collection = collection,
start_date = start_date,
end_date = end_date,
limit = 1
)
# assert that service is online
tryCatch(
{
items <- rstac::post_request(items_query, ...)
},
error = function(e) {
stop(paste(
".source_collection_access_test.stac_cube: service is",
"unreachable\n", e$message
), call. = FALSE)
}
)
.check_stac_items(items)
# signing the url with the mpc token
access_key <- Sys.getenv("MPC_TOKEN")
if (!nzchar(access_key)) {
access_key <- NULL
}
items <- suppressWarnings(
rstac::items_sign(
items,
sign_fn = rstac::sign_planetary_computer(
httr::add_headers("Ocp-Apim-Subscription-Key" = access_key)
)
)
)
items <- .source_items_bands_select(
source = source,
items = items,
bands = bands[[1]],
collection = collection, ...
)
href <- .source_item_get_hrefs(
source = source,
item = items$feature[[1]],
collection = collection, ...
)
# assert that token and/or href is valid
if (dry_run) {
tryCatch(
{
.raster_open_rast(href)
},
error = function(e) {
stop(paste(
".source_collection_access_test.stac_cube: cannot",
"open url\n", href, "\n", e$message
), call. = FALSE)
}
)
}
return(invisible(source))
}
#' @title Create an items object in an MPC Sentinel-2 collection
#' @keywords internal
#' @noRd
#' @description \code{.source_items_new()} this function is called to create
#' an items object. In case of Web services, this function is responsible for
#' making the Web requests to the server.
#' @param source Name of the STAC provider.
#' @param collection Collection to be searched in the data source.
#' @param stac_query Query that follows the STAC protocol
#' @param bands Names of the bands to filter
#' @param ... Other parameters to be passed for specific types.
#' @param orbit Name of the orbit (e.g. "ascending" or "descending")
#' @param tiles Selected tiles (optional)
#' @param platform Satellite platform (optional).
#' @return An object referring the images of a sits cube.
#' @export
`.source_collection_access_test.mpc_cube_sentinel-1-grd` <- function(
source,
collection,
bands, ...,
orbit = "descending",
start_date = NULL,
end_date = NULL,
dry_run = TRUE) {
# require package
.check_require_packages("rstac")
orbits <- .conf("sources", source, "collections", collection, "orbits")
.check_chr_within(
x = orbit,
within = orbits,
msg = "Invalid `orbit` parameter"
)
stac_query <- .stac_create_items_query(
source = source,
collection = collection,
roi = list(
"xmin" = -50.479,
"ymin" = -10.1973,
"xmax" = -50.410,
"ymax" = -10.1510,
"crs" = "EPSG:4326"
),
start_date = start_date,
end_date = end_date,
limit = 1
)
stac_query <- rstac::ext_filter(
stac_query,
`sar:frequency_band` == "C" &&
`sar:instrument_mode` == "IW" &&
`sat:orbit_state` == {{orbit}}
)
# assert that service is online
tryCatch(
{
items <- rstac::post_request(stac_query, ...)
},
error = function(e) {
stop(paste(
".source_collection_access_test.stac_cube: service is",
"unreachable\n", e$message
), call. = FALSE)
}
)
.check_stac_items(items)
# signing the url with the mpc token
access_key <- Sys.getenv("MPC_TOKEN")
if (!nzchar(access_key)) {
access_key <- NULL
}
items <- suppressWarnings(
rstac::items_sign(
items,
sign_fn = rstac::sign_planetary_computer(
httr::add_headers("Ocp-Apim-Subscription-Key" = access_key)
)
)
)
items <- .source_items_bands_select(
source = source,
items = items,
bands = bands[[1]],
collection = collection, ...
)
href <- .source_item_get_hrefs(
source = source,
item = items$feature[[1]],
collection = collection, ...
)
# assert that token and/or href is valid
if (dry_run) {
tryCatch(
{
.raster_open_rast(href)
},
error = function(e) {
stop(paste(
".source_collection_access_test.stac_cube: cannot",
"open url\n", href, "\n", e$message
), call. = FALSE)
}
)
}
return(invisible(NULL))
}
`.source_collection_access_test.mpc_cube_sentinel-1-rtc` <- function(
source,
collection,
bands, ...,
orbit = "descending",
start_date = NULL,
end_date = NULL,
dry_run = TRUE) {
`.source_collection_access_test.mpc_cube_sentinel-1-grd`(
source = source,
collection = collection,
bands = bands, ...,
orbit = orbit,
start_date = start_date,
end_date = end_date,
dry_run = dry_run
)
}
#' @title Get bbox from file info for Sentinel-1 GRD
#' @keywords internal
#' @noRd
#' @param source Data source
#' @param file_info File info
#' @param ... Additional parameters.
#' @param collection Image collection
#' @return vector (xmin, ymin, xmax, ymax).
#' @export
`.source_tile_get_bbox.mpc_cube_sentinel-1-grd` <- function(source,
file_info, ...,
collection = NULL) {
.check_set_caller(".source_tile_get_bbox.mpc_cube_sentinel-1-grd")
# pre-condition
.check_num(nrow(file_info), min = 1, msg = "invalid 'file_info' value")
# get bbox based on file_info
xmin <- min(file_info[["xmin"]])
ymin <- min(file_info[["ymin"]])
xmax <- max(file_info[["xmax"]])
ymax <- max(file_info[["ymax"]])
# post-condition
.check_that(xmin < xmax,
local_msg = "xmin is greater than xmax",
msg = "invalid bbox value"
)
.check_that(ymin < ymax,
local_msg = "ymin is greater than ymax",
msg = "invalid bbox value"
)
# create a bbox
bbox <- c(xmin = xmin, ymin = ymin, xmax = xmax, ymax = ymax)
return(bbox)
}
#' @title Get bbox from file info for Sentinel-1 RTC
#' @keywords internal
#' @noRd
#' @param source Data source
#' @param file_info File info
#' @param ... Additional parameters.
#' @param collection Image collection
#' @return vector (xmin, ymin, xmax, ymax).
#' @export
`.source_tile_get_bbox.mpc_cube_sentinel-1-rtc` <- function(source,
file_info, ...,
collection = NULL) {
`.source_tile_get_bbox.mpc_cube_sentinel-1-grd`(
source = source,
file_info = file_info, ...,
collection = collection
)
}
#' @keywords internal
#' @noRd
#' @export
`.source_items_new.mpc_cube_sentinel-1-grd` <- function(source,
collection,
stac_query, ...,
tiles = NULL,
orbit = "descending") {
# set caller to show in errors
.check_set_caller(".source_items_new.mpc_cube_sentinel-1-grd")
orbits <- .conf("sources", source, "collections", collection, "orbits")
.check_chr_within(
x = orbit,
within = orbits,
msg = "Invalid `orbit` parameter"
)
stac_query <- rstac::ext_filter(
stac_query,
`sar:frequency_band` == "C" &&
`sar:instrument_mode` == "IW" &&
`sat:orbit_state` == {{orbit}}
)
# Sentinel-1 does not support tiles - convert to ROI
if (!is.null(tiles)) {
roi <- .s2_mgrs_to_roi(tiles)
stac_query$params$intersects <- NULL
stac_query$params$bbox <- c(roi[["lon_min"]],
roi[["lat_min"]],
roi[["lon_max"]],
roi[["lat_max"]]
)
}
items_info <- rstac::post_request(q = stac_query, ...)
.check_stac_items(items_info)
# fetching all the metadata
items_info <- suppressWarnings(
rstac::items_fetch(items = items_info, progress = FALSE)
)
# assign href
access_key <- Sys.getenv("MPC_TOKEN")
if (!nzchar(access_key)) {
access_key <- NULL
}
# Clean old tokens cached in rstac
.mpc_clean_token_cache()
items_info <- suppressWarnings(
rstac::items_sign(
items_info, sign_fn = rstac::sign_planetary_computer(
httr::add_headers("Ocp-Apim-Subscription-Key" = access_key)
)
)
)
return(items_info)
}
#' @keywords internal
#' @noRd
#' @export
`.source_items_new.mpc_cube_sentinel-1-rtc` <- function(source,
collection,
stac_query, ...,
tiles = NULL,
orbit = "descending") {
`.source_items_new.mpc_cube_sentinel-1-grd`(
source = source,
collection = collection,
stac_query = stac_query, ...,
tiles = tiles,
orbit = orbit
)
}
#' @keywords internal
#' @noRd
#' @export
`.source_items_new.mpc_cube_sentinel-2-l2a` <- function(source,
collection,
stac_query, ...,
tiles = NULL,
platform = NULL) {
# set caller to show in errors
.check_set_caller(".source_items_new.mpc_cube_sentinel-2-l2a")
if (!is.null(platform)) {
platform <- .stac_format_platform(
source = source,
collection = collection,
platform = platform
)
stac_query <- rstac::ext_query(
q = stac_query, "platform" == platform
)
}
# mpc does not support %in% operator, so we have to
if (!is.null(tiles)) {
items_list <- lapply(tiles, function(tile) {
stac_query <- rstac::ext_query(
q = stac_query, "s2:mgrs_tile" == tile
)
# making the request
items_info <- rstac::post_request(q = stac_query, ...)
.check_stac_items(items_info)
# fetching all the metadata
suppressWarnings(
rstac::items_fetch(items = items_info, progress = FALSE)
)
})
# getting the first item info
items_info <- items_list[[1]]
# joining the items
items_info$features <- do.call(
c,
args = lapply(items_list, `[[`, "features")
)
} else {
items_info <- rstac::post_request(q = stac_query, ...)
.check_stac_items(items_info)
# fetching all the metadata
items_info <- suppressWarnings(
rstac::items_fetch(items = items_info, progress = FALSE)
)
}
# assign href
access_key <- Sys.getenv("MPC_TOKEN")
if (!nzchar(access_key)) {
access_key <- NULL
}
# Clean old tokens cached in rstac
.mpc_clean_token_cache()
items_info <- suppressWarnings(
rstac::items_sign(
items_info,
sign_fn = rstac::sign_planetary_computer(
httr::add_headers("Ocp-Apim-Subscription-Key" = access_key)
)
)
)
return(items_info)
}
#' @title Create an items object in MPC Landsat collection
#' @keywords internal
#' @noRd
#' @description \code{.source_items_new()} this function is called to create
#' an items object. In case of Web services, this function is responsible for
#' making the Web requests to the server.
#' @param source Name of the STAC provider.
#' @param collection Collection to be searched in the data source.
#' @param stac_query Query that follows the STAC protocol
#' @param ... Other parameters to be passed for specific types.
#' @param tiles Selected tiles (optional)
#' @param platform Satellite platform (optional).
#' @return An object referring the images of a sits cube.
#' @export
`.source_items_new.mpc_cube_landsat-c2-l2` <- function(source,
collection,
stac_query, ...,
tiles = NULL,
platform = NULL) {
# set caller to show in errors
.check_set_caller(".source_items_new.mpc_cube_landsat-c2-l2")
if (!is.null(platform)) {
platform <- .stac_format_platform(
source = source,
collection = collection,
platform = platform
)
stac_query <- rstac::ext_query(
q = stac_query, "platform" == platform
)
}
.check_that(
is.null(tiles),
local_msg = "Error when retrieving Landsat MPC collection",
msg = "Searching by tiles not allowed, use roi"
)
# making the request based on ROI
items <- rstac::post_request(q = stac_query, ...)
.check_stac_items(items)
# fetching all the metadata and updating to upper case instruments
items <- suppressWarnings(
rstac::items_fetch(items = items, progress = FALSE)
)
# assign href
access_key <- Sys.getenv("MPC_TOKEN")
if (!nzchar(access_key)) {
access_key <- NULL
}
# Clean old tokens cached in rstac
.mpc_clean_token_cache()
items <- suppressWarnings(
rstac::items_sign(
items,
sign_fn = rstac::sign_planetary_computer(
httr::add_headers("Ocp-Apim-Subscription-Key" = access_key)
)
)
)
return(items)
}
#' @keywords internal
#' @noRd
#' @export
`.source_items_tile.mpc_cube_sentinel-1-grd` <- function(source,
items, ...,
collection = NULL) {
rep("NoTilingSystem", rstac::items_length(items))
}
#' @keywords internal
#' @noRd
#' @export
`.source_items_tile.mpc_cube_sentinel-1-rtc` <- function(source,
items, ...,
collection = NULL) {
`.source_items_tile.mpc_cube_sentinel-1-grd`(
source = source,
items = items, ...,
collection = collection
)
}
#' @title Organizes items for MPC Sentinel-2 collections
#' @param source Name of the STAC provider.
#' @param items \code{STACItemcollection} object from rstac package.
#' @param ... Other parameters to be passed for specific types.
#' @param collection Collection to be searched in the data source.
#' @return A list of items.
#' @keywords internal
#' @noRd
#' @export
`.source_items_tile.mpc_cube_sentinel-2-l2a` <- function(source,
items, ...,
collection = NULL) {
rstac::items_reap(items, field = c("properties", "s2:mgrs_tile"))
}
#' @title Organizes items for MPC Landsat collections
#' @param source Name of the STAC provider.
#' @param items \code{STACItemcollection} object from rstac package.
#' @param ... Other parameters to be passed for specific types.
#' @param collection Collection to be searched in the data source.
#' @return A list of items.
#' @keywords internal
#' @noRd
#' @export
`.source_items_tile.mpc_cube_landsat-c2-l2` <- function(source,
items, ...,
collection = NULL) {
# store tile info in items object
items$features <- purrr::map(items$features, function(feature) {
feature$properties$tile <- paste0(
feature$properties[["landsat:wrs_path"]],
feature$properties[["landsat:wrs_row"]]
)
feature
})
rstac::items_reap(items, field = c("properties", "tile"))
}
#' @title Filter S1 GRD tiles
#' @noRd
#' @param source Data source
#' @param cube Cube to be filtered
#' @param tiles Tiles to be selected
#' @return Filtered cube
#' @export
`.source_filter_tiles.mpc_cube_sentinel-1-grd` <- function(source,
collection,
cube,
tiles) {
return(cube)
}
`.source_filter_tiles.mpc_cube_sentinel-1-rtc` <- function(source,
collection,
cube,
tiles) {
`.source_filter_tiles.mpc_cube_sentinel-1-grd`(
source = source,
collection = collection,
cube = cube,
tiles = tiles)
}
.mpc_clean_token_cache <- function() {
mpc_token <- get("ms_token", envir = asNamespace("rstac"), inherits = TRUE)
cached_tokens <- names(mpc_token)
lapply(cached_tokens, function(cached_token) {
assign(cached_token, NULL, envir = mpc_token)
})
return(invisible(NULL))
}