-
Notifications
You must be signed in to change notification settings - Fork 76
/
api_conf.R
1256 lines (1222 loc) · 40.8 KB
/
api_conf.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' @title Set configuration parameters
#' @name .conf_set_options
#' @noRd
#' @author Rolf Simoes, \email{rolf.simoes@@inpe.br}
#'
#' @param processing_bloat Estimated growth size of R memory relative
#' to block size.
#' @param rstac_pagination_limit Limit of number of items returned by STAC.
#' @param gdal_creation_options GDAL creation options for GeoTiff.
#' @param gdalcubes_chunk_size Chunk size to be used by gdalcubes
#' @param sources Data sources
#' @param colors Color values
#' @param ... Other configuration params
.conf_set_options <- function(processing_bloat = NULL,
rstac_pagination_limit = NULL,
gdal_creation_options = NULL,
gdalcubes_chunk_size = NULL,
sources = NULL,
colors = NULL, ...) {
# set caller to show in errors
.check_set_caller(".conf_set_options")
# initialize config
if (!exists("config", envir = sits_env))
sits_env[["config"]] <- list()
# process processing_bloat
if (!is.null(processing_bloat)) {
.check_int_parameter(processing_bloat,
min = 1, len_min = 1, len_max = 1, max = 10
)
sits_env[["config"]][["processing_bloat"]] <- processing_bloat
}
# process rstac_pagination_limit
if (!is.null(rstac_pagination_limit)) {
.check_int_parameter(rstac_pagination_limit,
min = 1, len_min = 1, len_max = 1, max = 500
)
sits_env[["config"]][["rstac_pagination_limit"]] <- rstac_pagination_limit
}
# process gdal_creation_options
if (!is.null(gdal_creation_options)) {
.check_chr(gdal_creation_options,
allow_empty = FALSE,
regex = "^.+=.+$",
msg = .conf("messages", ".conf_set_options_gdal_creation")
)
sits_env$config[["gdal_creation_options"]] <- gdal_creation_options
}
# process gdalcubes_chunk_size
if (!is.null(gdalcubes_chunk_size)) {
.check_num_parameter(gdalcubes_chunk_size,
len_min = 3,
len_max = 3,
is_named = FALSE
)
sits_env[["config"]][["gdalcubes_chunk_size"]] <- gdalcubes_chunk_size
}
# process sources
if (!is.null(sources)) {
.check_lst_parameter(sources, len_min = 1)
# source names are uppercase
names(sources) <- toupper(names(sources))
# check each source
sources <- lapply(sources, function(source) {
# pre-condition
.check_lst_parameter(source, len_min = 2)
# check that source contains essential parameters
.check_chr_contains(names(source),
contains = c("s3_class", "collections")
)
names(source) <- tolower(names(source))
# check source
source <- .check_error(
{
do.call(.conf_new_source, args = source)
},
msg = .conf("messages", ".conf_set_options_source")
)
return(source)
})
# initialize sources
if (is.null(sits_env[["config"]][["sources"]])) {
sits_env[["config"]][["sources"]] <- sources
}
sits_env[["config"]][["sources"]] <- utils::modifyList(
sits_env[["config"]][["sources"]],
sources,
keep.null = FALSE
)
}
# check and initialize palettes
if (.has(colors)) {
# initialize colors
if (is.null(sits_env[["config"]][["colors"]])) {
sits_env[["config"]][["colors"]] <- colors
}
# add colors
sits_env[["config"]][["colors"]] <- utils::modifyList(
sits_env[["config"]][["colors"]],
colors,
keep.null = FALSE
)
}
# process extra parameters
dots <- list(...)
.check_lst(dots)
if (length(dots) > 0) {
sits_env[["config"]] <- utils::modifyList(
sits_env[["config"]],
dots,
keep.null = FALSE
)
}
return(invisible(sits_env[["config"]]))
}
#' @title Return the default configuration file
#' @name .conf_file
#' @keywords internal
#' @noRd
#' @return default configuration file
#'
.conf_file <- function() {
.check_set_caller(".conf_file")
# load the default configuration file
yml_file <- system.file("extdata", "config.yml", package = "sits")
# check that the file name is valid
.check_file(yml_file)
return(yml_file)
}
#' @title Return the internal configuration file (only for developers)
#' @name .conf_internals_file
#' @keywords internal
#' @noRd
#' @return default internal configuration file
.conf_internals_file <- function() {
.check_set_caller(".conf_internals_file")
# load the default configuration file
yml_file <- system.file("extdata", "config_internals.yml", package = "sits")
# check that the file name is valid
.check_that(file.exists(yml_file))
return(yml_file)
}
#' @title Return the user-relevant configuration file
#' @name .config_file
#' @keywords internal
#' @noRd
#' @return default user-relevant configuration file
.config_file <- function() {
.check_set_caller(".config_file")
# load the default configuration file
yml_file <- system.file("extdata", "config.yml", package = "sits")
# check that the file name is valid
.check_that(file.exists(yml_file))
return(yml_file)
}
#' @title Return the message configuration files (only for developers)
#' @name .conf_sources_files
#' @keywords internal
#' @noRd
#' @return internal sources configuration
.conf_sources_files <- function() {
.check_set_caller(".conf_sources_files")
# list the source files configurations
package_files <- system.file("extdata", "sources", package = "sits")
yml_files <- list.files(
path = package_files,
pattern = "config_source_*",
full.names = TRUE
)
# check that the file name is valid
purrr::map(yml_files, .check_file)
return(yml_files)
}
#' @name .conf_load_sources
#' @description Loads sources configurations
#' @keywords internal
#' @noRd
#' @return NULL, called for side effects
.conf_load_sources <- function() {
# get file paths
source_yml_files <- .conf_sources_files()
# load files
source_configs <- purrr::map(source_yml_files, function(file) {
yaml::yaml.load_file(
input = file,
merge.precedence = "override"
)
})
# prepare sources object
source_obj <- purrr::map(source_configs, "sources")
source_obj <- purrr::flatten(source_obj)
# prepare extras objects (e.g., token, url config)
extras_obj <- purrr::map(source_configs, function(source_config) {
source_config[["sources"]] <- NULL
source_config
})
extras_obj <- purrr::flatten(extras_obj)
# merge objects
config_obj <- utils::modifyList(extras_obj, list(
sources = source_obj
))
# set configurations
do.call(.conf_set_options, args = config_obj)
# done
return(invisible(NULL))
}
#' @title Return the message configuration file (only for developers)
#' @name .conf_messages_file
#' @keywords internal
#' @noRd
#' @return default internal configuration file
.conf_messages_file <- function() {
.check_set_caller(".conf_messages_file")
# load the default configuration file
yml_file <- system.file("extdata", "config_messages.yml", package = "sits")
# check that the file name is valid
.check_file(yml_file)
return(yml_file)
}
#' @name .conf_load_messages
#' @description Loads the error messages and warnings
#' @keywords internal
#' @noRd
#' @return NULL, called for side effects
.conf_load_messages <- function() {
# load the color configuration file
msgs_yml_file <- .conf_messages_file()
config_msgs <- yaml::yaml.load_file(
input = msgs_yml_file,
merge.precedence = "override"
)
# set the messages
sits_env[["config"]][["messages"]] <- config_msgs
return(invisible(NULL))
}
#' @title Return the default configuration file for colors
#' @name .conf_colors_file
#' @keywords internal
#' @noRd
#' @return default color configuration file
.conf_colors_file <- function() {
# load the default configuration file
yml_file <- system.file("extdata", "config_colors.yml", package = "sits")
# check that the file name is valid
.check_file(yml_file, msg = "invalid configuration file")
return(yml_file)
}
#' @title Loads default color table and legends
#' @name .conf_load_color_table
#' @description Loads the default color table
#' @keywords internal
#' @noRd
#' @return NULL, called for side effects
.conf_load_color_table <- function() {
# load the color configuration file
color_yml_file <- .conf_colors_file()
config_colors <- yaml::yaml.load_file(
input = color_yml_file,
merge.precedence = "override"
)
# set the legends
sits_env[["legends"]] <- config_colors$legends
# build the color table
colors <- config_colors[["colors"]]
color_table <- purrr::map2_dfr(colors, names(colors),
function(cl, nm) {
cc_tb <- tibble::tibble(
name = nm,
color = cl
)
return(cc_tb)
})
# set the color table
sits_env[["color_table"]] <- color_table
return(invisible(color_table))
}
#' @title Add user color table
#' @name .conf_add_color_table
#' @description Loads a user color table
#' and merges with default color table
#' @param color_tb user color table
#' @keywords internal
#' @noRd
#' @return new color table (invisible)
.conf_add_color_table <- function(color_tb) {
.check_set_caller(".conf_add_color_table")
# pre condition - table contains name and hex code
.check_chr_contains(
x = colnames(color_tb),
contains = .conf("color_table_cols"),
discriminator = "all_of"
)
# replace all duplicates
new_colors <- dplyr::pull(color_tb, .data[["name"]])
# remove duplicate colors
old_color_tb <- dplyr::filter(sits_env[["color_table"]],
!(.data[["name"]] %in% new_colors))
sits_env[["color_table"]] <- dplyr::bind_rows(old_color_tb, color_tb)
return(invisible(sits_env[["color_table"]]))
}
#' @title Merge user colors with default colors
#' @name .conf_merge_colors
#' @description Combines user colors with default color table
#' @param user_colors list of user colors
#' @keywords internal
#' @noRd
#' @return new color table
.conf_merge_colors <- function(user_colors) {
# get the current color table
color_table <- .conf_colors()
names_user_colors <- names(user_colors)
col_user_colors <- unname(user_colors)
for (i in seq_along(names_user_colors)) {
name <- names_user_colors[[i]]
col <- col_user_colors[[i]]
id <- which(color_table[["name"]] == name)
if (length(id) > 0) {
color_table[id, "color"] <- col
} else {
color_table <- tibble::add_row(color_table,
name = name,
color = col
)
}
}
sits_env[["color_table"]] <- color_table
return(color_table)
}
#' @title Merge user legends with default legends
#' @name .conf_merge_legends
#' @description Combines user legends with default
#' @param user_legends List of user legends
#' @keywords internal
#' @noRd
#' @return new color table
.conf_merge_legends <- function(user_legends){
.check_set_caller(".conf_merge_legends")
# check legends are valid names
.check_chr_parameter(names(user_legends), len_max = 100,
msg = .conf("messages", ".conf_merge_legends_user"))
# check legend names do not already exist
.check_that(!(any(names(user_legends) %in% names(sits_env[["legends"]]))))
# check colors names are valid
ok <- purrr::map_lgl(user_legends, function(leg){
.check_chr_parameter(leg, len_max = 100,
msg = .conf("messages", ".conf_merge_legends_colors"))
return(TRUE)
})
sits_env[["legends"]] <- c(sits_env[["legends"]], user_legends)
return(invisible(sits_env[["legends"]]))
}
#' @title Return the default color table
#' @name .conf_colors
#' @keywords internal
#' @noRd
#' @return default color table
#'
.conf_colors <- function() {
return(sits_env[["color_table"]])
}
#' @title Configure fonts to be used
#' @name .conf_set_fonts
#' @keywords internal
#' @noRd
#' @return NULL, called for side effects
#'
.conf_set_fonts <- function() {
# verifies if sysfonts package is installed
.check_require_packages("sysfonts")
.check_require_packages("showtext")
showtext::showtext_auto()
sysfonts::font_add_google("IBM Plex Sans", family = "plex_sans")
sysfonts::font_add_google("Roboto", family = "roboto")
sysfonts::font_add_google("Lato", family = "lato")
return(NULL)
}
#' @title Return the user configuration set in enviromental variable
#' @name .conf_user_env_var
#' @keywords internal
#' @noRd
#' @return YAML user configuration
.conf_user_env_var <- function() {
# load the default user configuration file
yml_file <- Sys.getenv("SITS_CONFIG_USER_FILE")
yaml_user_config <- NULL
# check if the file exists when env var is set
if (nchar(yml_file) > 0) {
.check_warn(
.check_file(yml_file,
msg = .conf("messages", ".conf_user_env_var")
)
)
# if the YAML file exists, try to load it
tryCatch({
yaml_user_config <- yaml::yaml.load_file(
input = yml_file,
merge.precedence = "override"
)},
error = function(e) {
warning(.conf("messages", ".conf_user_env_var"), call. = TRUE)
}
)
}
# returns the user configuration, otherwise null
return(yaml_user_config)
}
#' @title Load the user configuration file
#' @name .conf_set_user_file
#' @param config_user_file Configuration file provided by user
#' @keywords internal
#' @noRd
#' @return user configuration file
.conf_set_user_file <- function(config_user_file = NULL) {
.check_set_caller(".conf_set_user_file")
# try to find a valid user configuration file
# check config user file is valid
if (.has(config_user_file) && !is.na(config_user_file)) {
user_config <- tryCatch(
yaml::yaml.load_file(config_user_file, error.label = "",
readLines.warn = FALSE),
error = function(e) {
stop(.conf("messages", ".conf_set_user_file"), call. = TRUE)
}
)
} else {
user_config <- .conf_user_env_var()
}
if (.has(user_config)) {
if (.has(user_config[["colors"]])) {
user_colors <- user_config[["colors"]]
.conf_merge_colors(user_colors)
user_config[["colors"]] <- NULL
}
if (.has(user_config[["legends"]])) {
user_legends <- user_config[["legends"]]
.conf_merge_legends(user_legends)
user_config[["legends"]] <- NULL
}
if (length(user_config) > 0) {
user_config <- utils::modifyList(sits_env[["config"]],
user_config,
keep.null = FALSE
)
# set options defined by user (via YAML file)
# modifying existing configuration
.conf_set_options(
processing_bloat = user_config[["processing_bloat"]],
rstac_pagination_limit =
user_config[["rstac_pagination_limit"]],
gdal_creation_options =
user_config[["gdal_creation_options"]],
gdalcubes_chunk_size =
user_config[["gdalcubes_chunk_size"]],
sources = user_config[["sources"]],
colors = user_config[["colors"]],
view = user_config[["view"]],
plot = user_config[["plot"]]
)
}
}
}
#' @title Check band availability
#' @name .conf_check_bands
#' @description Checks if the requested bands are available in the collection
#'
#' @keywords internal
#' @noRd
#' @param source Data source
#' @param collection Collection to be searched in the data source.
#' @param bands Bands to be included.
#'
#' @return Called for side effects.
.conf_check_bands <- function(source, collection, bands) {
# set caller to show in errors
.check_set_caller(".conf_check_bands")
sits_bands <- .source_bands(
source = source,
collection = collection
)
source_bands <- .source_bands_band_name(
source = source,
collection = collection
)
.check_chr_within(
x = bands,
within = c(sits_bands, source_bands)
)
return(invisible(bands))
}
#' @title List configuration parameters
#' @name .conf_list_params
#' @description List the contents of a source
#'
#' @keywords internal
#' @noRd
#' @param params parameter list
#'
#' @return Called for side effects.
.conf_list_params <- function(params) {
params <- lapply(params, function(x) {
if (is.atomic(x)) {
return(x)
}
list(names(x))
})
params_txt <- yaml::as.yaml(
params,
indent = 4,
handlers = list(
character = function(x) {
res <- toString(x)
class(res) <- "verbatim"
res
},
integer = function(x) {
res <- toString(x)
class(res) <- "verbatim"
res
},
numeric = function(x) {
res <- toString(x)
class(res) <- "verbatim"
res
}
)
)
cat(params_txt, sep = "\n")
}
#' @title List contents of a source
#' @name .conf_list_source
#' @description List the contents of a source
#'
#' @keywords internal
#' @noRd
#' @param source Data source
#'
#' @return Called for side effects.
.conf_list_source <- function(source){
cat(paste0(source, ":\n"))
collections <- .source_collections(source)
purrr::map(collections, function(col) {
cat(paste0("- ", col))
cat(paste0(
" (", .source_collection_satellite(source, col),
"/", .source_collection_sensor(source, col), ")\n",
"- grid system: ", .source_collection_grid_system(source, col), "\n"
))
cat("- bands: ")
cat(.source_bands(source, col))
cat("\n")
if (.source_collection_open_data(source, col)) {
cat("- opendata collection ")
if (.source_collection_open_data(
source = source,
collection = col,
token = TRUE
)) {
cat("(requires access token)")
}
} else {
cat("- not opendata collection")
}
cat("\n")
cat("\n")
})
}
#' @title Get names associated to a configuration key
#' @name .conf_names
#' @param key key combination to access config information
#' @keywords internal
#' @noRd
#' @return names associated to the chosen access key
.conf_names <- function(...) {
key <- c(...)
res <- tryCatch(
{
names(sits_env[["config"]][[key]])
},
error = function(e) {
return(NULL)
}
)
# post-condition
.check_chr(res,
allow_empty = FALSE,
msg = paste(
"invalid names for",
paste0("'", paste0(key, collapse = "$"), "'"),
"key"
)
)
return(res)
}
#' @title Include a new source in the configuration
#' @name .conf_new_source
#' @description creates a new data source in the config file
#' @param s3_class s3 class associated to the source
#' @param collections collections associated to the source
#' @param ... other parameters associated to the new source
#' @param service protocol associated to the source (e.g., "STAC")
#' @param url url associated to the endpoint of the service
#' @keywords internal
#' @noRd
#' @return list with the configuration associated to the new source
.conf_new_source <- function(s3_class,
collections, ...,
service = NULL,
url = NULL) {
# set caller to show in errors
.check_set_caller(".conf_new_source")
# pre-condition
.check_chr_parameter(s3_class,
allow_empty = FALSE, len_min = 1,
msg = .conf("messages", ".conf_new_source_s3class")
)
if (!is.null(service)) {
.check_chr_parameter(service,
allow_empty = FALSE, len_min = 1, len_max = 1,
msg = .conf("messages", ".conf_new_source_service")
)
}
if (!is.null(url)) {
.check_chr_parameter(url,
allow_empty = FALSE, len_min = 1, len_max = 1,
regex = '^(http|https):https://[^ "]+$',
msg = .conf("messages", ".conf_new_source_url")
)
}
.check_lst(collections, len_min = 1)
names(collections) <- toupper(names(collections))
collections <- lapply(collections, function(collection) {
# pre-condition
.check_lst_parameter(collection,
len_min = 1,
msg = .conf("messages", ".conf_new_source_collections")
)
# collection members must be lower case
names(collection) <- tolower(names(collection))
collection <- .check_error(
{
do.call(.conf_new_collection, args = collection)
},
msg = .conf("messages", ".conf_new_source_collections")
)
return(collection)
})
# extra parameters
dots <- list(...)
.check_lst_parameter(dots, len_min = 0,
msg = .conf("messages", ".conf_new_source_collections_args"))
return(c(list(
s3_class = s3_class,
service = service,
url = url,
collections = collections
), dots))
}
#' @title Include a new collection in the configuration
#' @name .conf_new_collection
#' @description creates a new collection associated to a source
#' @param bands bands associated to the collection
#' @param ... other relevant parameters
#' @param satellite satellite associated to the collection
#' @param sensor sensor associated to the collection
#' @keywords internal
#' @noRd
#' @return list with the configuration associated to the new collection
.conf_new_collection <- function(bands, ...,
satellite = NULL,
sensor = NULL,
metadata_search = NULL) {
# set caller to show in errors
.check_set_caller(".conf_new_collection")
# check satellite
.check_chr_parameter(satellite,
allow_null = TRUE,
msg = .conf("messages", ".conf_new_collection_satellite")
)
# check sensor
.check_chr(sensor,
allow_null = TRUE,
msg = .conf("messages", ".conf_new_collection_sensor")
)
# check metadata_search
if (!missing(metadata_search)) {
.check_chr_within(metadata_search,
within = .conf("metadata_search_strategies"),
msg = .conf("messages", ".conf_new_collection_metadata")
)
}
# check extra parameters
dots <- list(...)
.check_lst(dots,
msg = .conf("messages", ".conf_new_collection_metadata_args")
)
# bands names is upper case
names(bands) <- toupper(names(bands))
# pre-condition
.check_lst(bands,
len_min = 1,
msg = .conf("messages", ".conf_new_collection_bands")
)
# define collection bands
collection_bands <- c()
# handle class bands
is_class_cube <- dots[["class_cube"]]
is_class_cube <- all(!is.null(is_class_cube))
if (is_class_cube) {
# configure class bands (assuming there is no cloud band in class cubes)
class_bands <- .conf_new_bands(bands, .conf_new_class_band)
# save band configuration object
collection_bands <- c(class_bands)
} else {
# handle cloud and non-cloud bands
cloud_band <- bands[names(bands) %in% .source_cloud()]
non_cloud_bands <- bands[!names(bands) %in% .source_cloud()]
# cloud bands
cloud_band <- .conf_new_bands(cloud_band, .conf_new_cloud_band)
# non-cloud bands
non_cloud_bands <- .conf_new_bands(non_cloud_bands, .conf_new_band)
# save bands configuration object
collection_bands <- c(non_cloud_bands, cloud_band)
}
# merge metadata properties
res <- c(list(bands = collection_bands),
"satellite" = satellite,
"sensor" = sensor,
"metadata_search" = metadata_search, dots
)
# post-condition
.check_lst(res,
len_min = 1,
msg = .conf("messages", ".conf_new_collection")
)
.check_lst(res$bands,
len_min = 1,
msg = .conf("messages", ".conf_new_collection_bands")
)
# return a new collection data
return(res)
}
#' @title Include a new band in the configuration
#' @name .conf_new_band
#' @description creates a description associated to a new band
#' @param missing_value missing value
#' @param minimum_value minimum value
#' @param maximum_value maximum_value
#' @param scale_factor scale_factor associated with the data
#' @param offset_value offset_value for the band
#' @param band_name name of the band
#' @param resolution spatial resolution (in meters)
#' @param ... other relevant parameters
#' @keywords internal
#' @noRd
#' @return list with the configuration associated to the new band
.conf_new_band <- function(missing_value,
minimum_value,
maximum_value,
scale_factor,
offset_value,
band_name,
resolution, ...) {
.check_set_caller(".conf_new_band")
# pre-condition
.check_num_parameter(
missing_value,
len_min = 1,
len_max = 1
)
.check_num_parameter(
minimum_value,
len_min = 1,
len_max = 1
)
.check_num_parameter(
x = maximum_value,
len_min = 1,
len_max = 1
)
.check_num_parameter(
scale_factor,
len_min = 1,
len_max = 1,
exclusive_min = 0
)
.check_num_parameter(
offset_value,
len_min = 1,
len_max = 1
)
.check_num_parameter(
resolution,
exclusive_min = 0,
len_min = 1,
len_max = 1,
allow_null = TRUE
)
.check_chr(
band_name,
allow_empty = FALSE,
len_min = 1,
len_max = 1
)
# extra parameters
dots <- list(...)
.check_lst(dots, msg = .conf("messages", ".check_new_band_dots"))
new_band_params <- c(list(
missing_value = missing_value,
minimum_value = minimum_value,
maximum_value = maximum_value,
scale_factor = scale_factor,
offset_value = offset_value,
band_name = band_name,
resolution = resolution
), dots)
# post-condition
.check_lst_parameter(new_band_params,
len_min = 7
)
# return a band object
return(new_band_params)
}
#' @title Include a new cloud band in the configuration
#' @name .conf_new_cloud_band
#' @description creates a description associated to a new cloud band
#' @param bit_mask bit mask to describe clouds (if applicable)
#' @param value values of the cloud band
#' @param interp_values pixel values that need to be replaced by interpolation
#' @param resolution spatial resolution (in meters)
#' @param band_name name of the band
#' @param ... other relevant parameters
#' @keywords internal
#' @noRd
#' @return list with the configuration associated to the new band
.conf_new_cloud_band <- function(bit_mask,
values,
interp_values,
resolution,
band_name, ...) {
# set caller to show in errors
.check_set_caller(".conf_new_cloud_band")
# pre-condition
.check_lgl_parameter(bit_mask)
.check_lst_parameter(values, fn_check = .check_chr)
.check_int_parameter(interp_values, len_min = 1)
.check_chr_parameter(band_name, len_min = 1, len_max = 1)
# extra parameters
dots <- list(...)
.check_lst(dots, msg = .conf("messages", ".check_new_cloud_band_dots"))
cloud_band_params <- c(list(
bit_mask = bit_mask,
values = values,
interp_values = interp_values,
resolution = resolution,
band_name = band_name
), dots)
# post-condition
.check_lst_parameter(cloud_band_params, len_min = 5)
# return a cloud band object
return(cloud_band_params)
}
#' @title Include a new class band in the configuration
#' @name .conf_new_class_band
#' @description creates a description associated to a new cloud band
#' @param bit_mask bit mask to describe clouds (if applicable)
#' @param values values of the class band
#' @param resolution spatial resolution (in meters)
#' @param band_name name of the band
#' @param ... other relevant parameters
#' @keywords internal
#' @noRd
#' @return list with the configuration associated to the new class band
.conf_new_class_band <- function(bit_mask, values, resolution, band_name, ...) {
# set caller to show in errors
.check_set_caller(".conf_new_class_band")
# pre-condition
.check_lgl_parameter(bit_mask)
.check_lst_parameter(values, fn_check = .check_chr)
.check_chr_parameter(band_name, len_min = 1, len_max = 1)
# check extra parameters
dots <- list(...)
.check_lst(dots, msg = .conf("messages",
".check_new_class_band_dots"))
# build band
class_band_params <- c(list(
bit_mask = bit_mask,
values = values,
resolution = resolution,
band_name = band_name
), dots)
# post-condition
.check_lst_parameter(class_band_params, len_min = 4)
# return a class band object
return(class_band_params)
}
#' @title Configure bands
#' @name .conf_new_bands
#' @description creates a description of multiple bands based on a user-defined
#' strategy.
#' @param bands bands to be configured
#' @param config_fnc band configuration strategy function
#' @keywords internal
#' @noRd
#' @return list of configurations associated with the given bands
.conf_new_bands <- function(bands, config_fnc) {
lapply(bands, function(band) {
# lower case bands
names(band) <- tolower(names(band))
# configure band
.check_error(
{
do.call(config_fnc, args = band)
},
msg = .conf("messages", ".conf_new_collection_bands")
)
})
}
#' @title Retrieve the rstac pagination limit
#' @name .conf_rstac_limit
#' @keywords internal
#' @noRd
#' @return pagination limit to rstac output
.conf_rstac_limit <- function() {
res <- .conf("rstac_pagination_limit")
return(res)
}
#' @title Retrieve the raster package to be used
#' @name .conf_raster_pkg
#' @keywords internal
#' @noRd
#' @return the raster package used to process raster data
#'
.conf_raster_pkg <- function() {
res <- .conf("raster_api_package")
return(res)
}
#' @title Retrieve the request package to be used
#' @name .conf_request_pkg
#' @keywords internal
#' @noRd
#' @return the package used to process http requisitions
#'
.conf_request_pkg <- function() {
res <- .conf("request_api_package")
return(res)
}
#' @title Basic access config functions
#' @noRd
#'
#' @author Rolf Simoes, \email{rolf.simoes@@inpe.br}
#'
#' @description
#' These are basic functions to access config options.
#'
#' @param ... Set of \code{character} values representing a key to access
#' some hierarchical config entry.
#'
#' @examples
#' if (sits_run_examples()) {
#' .conf_exists("run_tests") # TRUE
#' .conf("run_tests")
#' .conf_exists("not_existing_entry") # FALSE
#' }
NULL
#' @title Check if a key exists in config
#' @name .conf_exists
#' @noRd
#' @param throw_error Should an error be thrown if test fails?
#' @returns A logical value or an error if key not found and