forked from e-sensing/sits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_conf.R
1117 lines (1040 loc) · 34.6 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_num(processing_bloat,
min = 1, len_min = 1, len_max = 1, max = 10,
is_integer = TRUE,
msg = "invalid 'processing_bloat' parameter"
)
sits_env$config[["processing_bloat"]] <- processing_bloat
}
# process rstac_pagination_limit
if (!is.null(rstac_pagination_limit)) {
.check_num(rstac_pagination_limit,
min = 1, len_min = 1, len_max = 1, max = 500,
is_integer = TRUE,
msg = "invalid 'rstac_pagination_limit' parameter"
)
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 = "invalid 'gdal_creation_options' parameter"
)
sits_env$config[["gdal_creation_options"]] <- gdal_creation_options
}
# process gdalcubes_chunk_size
if (!is.null(gdalcubes_chunk_size)) {
.check_num(gdalcubes_chunk_size,
min_len = 3,
max_len = 3,
is_named = FALSE,
msg = "invalid gdalcubes chunk size"
)
sits_env$config[["gdalcubes_chunk_size"]] <- gdalcubes_chunk_size
}
# process sources
if (!is.null(sources)) {
.check_lst(sources, min_len = 1)
# source names are uppercase
names(sources) <- toupper(names(sources))
sources <- lapply(sources, function(source) {
# pre-condition
.check_lst(source,
min_len = 2,
msg = "invalid 'source' parameter"
)
# check that source contains essential parameters
.check_chr_contains(names(source),
contains = c("s3_class", "collections"),
msg = "invalid 'source' parameter"
)
names(source) <- tolower(names(source))
# check source
source <- .check_error(
{
do.call(.conf_new_source, args = source)
},
msg = "invalid 'source' parameter"
)
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 (!is.null(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() {
# 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, msg = "invalid configuration 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() {
# load the default configuration file
yml_file <- system.file("extdata", "config_internals.yml", package = "sits")
# check that the file name is valid
.check_file(yml_file, msg = "invalid configuration file")
return(yml_file)
}
#' @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
# sits_env[["config"]] <- utils::modifyList(sits_env[["config"]],
# class_schemes,
# keep.null = FALSE
# )
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
#' @keywords internal
#' @noRd
#' @return new color table (invisible)
.conf_add_color_table <- function(color_tb) {
# pre condition - table contains name and hex code
.check_chr_contains(
x = colnames(color_tb),
contains = .conf("sits_color_table_cols"),
discriminator = "all_of",
msg = "invalid colour table - missing either name or hex columns"
)
# 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
#' @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)
}
.conf_merge_legends <- function(user_legends){
# check legends are valid names
.check_chr_parameter(names(user_legends), len_max = 100,
msg = "invalid user legends")
# check legend names do not already exist
.check_that(!(all(names(user_legends) %in% names (sits_env$legends))),
msg = "user defined legends already exist in sits")
# check colors names are valid
ok <- purrr::map_lgl(user_legends, function(leg){
.check_chr_parameter(leg, len_max = 100,
msg = "invalid color names in user legend")
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 = paste(
"invalid configuration file informed in",
"SITS_CONFIG_USER_FILE"
)
)
)
# 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(msg = paste(
"invalid configuration file informed in",
"SITS_CONFIG_USER_FILE"), 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) {
# try to find a valid user configuration file
# check config user file is valid
if (!purrr::is_null(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("invalid user configuration file", call. = TRUE)
}
)
} else {
user_config <- .conf_user_env_var()
}
if (!purrr::is_null(user_config)) {
if (!purrr::is_null(user_config$colors)) {
user_colors <- user_config$colors
.conf_merge_colors(user_colors)
user_config$colors <- NULL
}
if (!purrr::is_null(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"]],
tmap = user_config[["tmap"]]
)
}
}
}
#' @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),
msg = paste(
"invalid bands.\nPlease verify",
"the provided bands."
)
)
return(invisible(bands))
}
#' @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(s3_class,
allow_empty = FALSE, len_min = 1,
msg = "invalid 's3_class' parameter"
)
if (!is.null(service)) {
.check_chr(service,
allow_empty = FALSE, len_min = 1, len_max = 1,
msg = "invalid 'service' parameter"
)
}
if (!is.null(url)) {
.check_chr(url,
allow_empty = FALSE, len_min = 1, len_max = 1,
regex = '^(http|https)://[^ "]+$',
msg = "invalid 'url' parameter"
)
}
.check_lst(collections, min_len = 1)
names(collections) <- toupper(names(collections))
collections <- lapply(collections, function(collection) {
# pre-condition
.check_lst(collection,
min_len = 1,
msg = "invalid 'collections' parameter"
)
# collection members must be lower case
names(collection) <- tolower(names(collection))
collection <- .check_error(
{
do.call(.conf_new_collection, args = collection)
},
msg = "invalid 'collections' parameter"
)
return(collection)
})
# extra parameters
dots <- list(...)
.check_lst(dots, msg = "invalid extra arguments in collection")
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(satellite,
allow_null = TRUE,
msg = "invalid 'satellite' value"
)
# check sensor
.check_chr(sensor,
allow_null = TRUE,
msg = "invalid 'sensor' value"
)
# check metadata_search
if (!missing(metadata_search)) {
.check_chr_within(metadata_search,
within = .conf("metadata_search_strategies"),
msg = "invalid 'metadata_search' value"
)
}
# bands names is upper case
names(bands) <- toupper(names(bands))
# separate cloud and non-cloud bands
non_cloud_bands <- bands[!names(bands) %in% .source_cloud()]
cloud_band <- bands[names(bands) %in% .source_cloud()]
non_cloud_bands <- lapply(non_cloud_bands, function(band) {
# pre-condition
.check_lst(bands,
min_len = 1,
msg = "invalid 'bands' parameter"
)
# bands' members are lower case
names(band) <- tolower(names(band))
band <- .check_error(
{
do.call(.conf_new_band, args = band)
},
msg = "invalid 'bands' parameter"
)
return(band)
})
cloud_band <- lapply(cloud_band, function(cloud_band) {
# pre-condition
.check_lst(bands,
min_len = 1,
msg = "invalid 'bands' parameter"
)
# bands' members are lower case
names(cloud_band) <- tolower(names(cloud_band))
cloud_band <- .check_error(
{
do.call(.conf_new_cloud_band, args = cloud_band)
},
msg = "invalid 'bands' parameter"
)
return(cloud_band)
})
# extra parameters
dots <- list(...)
.check_lst(dots, msg = "invalid extra arguments in collection")
res <- c(list(bands = c(non_cloud_bands, cloud_band)),
"satellite" = satellite,
"sensor" = sensor,
"metadata_search" = metadata_search, dots
)
# post-condition
.check_lst(res,
min_len = 1,
msg = "invalid 'collection' value"
)
.check_lst(res$bands,
min_len = 1,
msg = "invalid collection 'bands' value"
)
# 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, ...) {
# pre-condition
.check_num(
x = missing_value,
len_min = 1,
len_max = 1,
msg = "invalid 'missing_value' parameter"
)
.check_num(
x = minimum_value,
len_min = 1,
len_max = 1,
msg = "invalid 'minimum_value' parameter"
)
.check_num(
x = maximum_value,
len_min = 1,
len_max = 1,
msg = "invalid 'maximum_value' parameter"
)
.check_num(
x = scale_factor,
len_min = 1,
len_max = 1,
exclusive_min = 0,
msg = "invalid 'scale_factor' parameter"
)
.check_num(
x = offset_value,
len_min = 1,
len_max = 1,
msg = "invalid 'offset_value' parameter"
)
.check_num(
x = resolution,
exclusive_min = 0,
len_min = 1,
len_max = 1,
allow_null = TRUE,
msg = "invalid 'resolution' parameter"
)
.check_chr(
x = band_name,
allow_empty = FALSE,
len_min = 1,
len_max = 1,
msg = "invalid 'band_name' value"
)
# extra parameters
dots <- list(...)
.check_lst(dots, "invalid extra arguments in band")
res <- 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(res,
min_len = 7,
msg = "invalid 'band' value"
)
# return a band object
return(res)
}
#' @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(bit_mask,
len_min = 1, len_max = 1,
msg = "invalid 'bit_mask' parameter"
)
.check_lst(values,
fn_check = .check_chr,
len_min = 1, len_max = 1,
msg = "invalid cloud 'values' parameter"
)
.check_num(interp_values,
len_min = 1, is_integer = TRUE,
msg = "invalid 'interp_values' parameter"
)
.check_chr(band_name,
allow_empty = FALSE, len_min = 1, len_max = 1,
msg = "invalid 'band_name' value"
)
# extra parameters
dots <- list(...)
.check_lst(dots, "invalid extra arguments in cloud band")
res <- c(list(
bit_mask = bit_mask,
values = values,
interp_values = interp_values,
resolution = resolution,
band_name = band_name
), dots)
# post-condition
.check_lst(res,
min_len = 5,
msg = "invalid 'band' value"
)
# return a cloud band object
return(res)
}
#' @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(key = c("rstac_pagination_limit"))
# post-condition
.check_num(res,
min = 1, len_min = 1, len_max = 1,
msg = "invalid 'rstac_pagination_limit' in config file"
)
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(key = c("raster_api_package"))
# post-condition
.check_chr(res,
len_min = 1, len_max = 1,
msg = "invalid 'raster_api_package' in config file"
)
.check_chr_within(res,
within = .raster_supported_packages(),
discriminator = "one_of",
msg = "invalid 'raster_api_package' in config file"
)
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
#' @noRd
#' @param throw_error Should an error be thrown if test fails?
#' @returns A logical value or an error if key not found and
#' `throw_error` is `TRUE`.
.conf_exists <- function(..., throw_error = FALSE) {
key <- c(...)
exists <- !is.null(.try(sits_env[["config"]][[key]], .default = NULL))
if (!exists && throw_error) {
stop("key '", paste(key, collapse = "->"), "' not found in config")
}
# Return test
exists
}
#' @title Get a config value based on a key
#' @noRd
#' @returns A value in config or an error if key does not exists.
.conf <- function(...) {
key <- c(...)
# Check for key existence and throws an error if it not exists
.conf_exists(key, throw_error = TRUE)
sits_env[["config"]][[c(key)]]
}
#' @title Config functions eo_cube
#' @noRd
#'
#' @author Rolf Simoes, \email{rolf.simoes@@inpe.br}
#'
#' @description
#' These are syntactic sugar functions to easily access config options for
#' bands of `eo_cube` cubes which is a S3 class representation
#' for an Earth Observation cube. It is the primary data used to obtain a
#' classification map.
#
#' The config entries of a `eo_cube` are located in
#' `sources -> <SOURCE> -> collections -> <COLLECTION>` key.
#' Values for source, collection, and band are uppercase.
#'
#' @examples
#' if (sits_run_examples()) {
#' # tests if 'BDC -> MOD13Q1-6 -> NDVI' key exists in config
#' .conf_eo_band_exists(
#' source = "BDC",
#' collection = "MOD13Q1-6",
#' band = "NDVI"
#' )
#' # get configuration for band NDVI of 'BDC -> MOD13Q1-6' collection
#' x <- .conf_eo_band(
#' source = "BDC",
#' collection = "MOD13Q1-6",
#' band = "NDVI"
#' )
#' }
NULL
#' @title Check if a band entry exists in config
#' @noRd
#' @param source Data source.
#' @param collection Collection in the data source.
#' @param band Band name.
#' @returns A logical value or an error if `source` or `collections`
#' does not exists.
.conf_eo_band_exists <- function(source, collection, band) {
# source, collection, and band are uppercase
source <- toupper(source)
collection <- toupper(collection)
band <- .band_eo(band)
# Check for source and collection and throws an error if it not exists
.conf_exists(
"sources", source, "collections", collection,
throw_error = TRUE
)
# Test for band and return
.conf_exists("sources", source, "collections", collection, "bands", band)
}
#' @title Get a config value for a band
#' @noRd
#' @param source Data source.
#' @param collection Collection in the data source.
#' @param band Band name
#' @details
#' If the band is not found, a default value will be returned from config.
#' If neither source nor collection entries are found in configuration file,
#' an error is thrown.
#' @returns A value in config.
.conf_eo_band <- function(source, collection, band) {
# Format band name
band <- .band_eo(band)
# Return a default value if band does not exists in config
if (!.conf_eo_band_exists(source, collection, band)) {
if (band %in% .conf_names("default_values", "eo_cube")) {
return(.conf("default_values", "eo_cube", band))
}
return(.conf("default_values", "eo_cube", "default"))
}
# Get band config value and return it
.conf("sources", source, "collections", collection, "bands", band)
}
#' @title Config functions for derived_cube
#' @noRd
#'
#' @author Rolf Simoes, \email{rolf.simoes@@inpe.br}
#'
#' @description
#' These are syntactic sugar functions to easily access config options for
#' bands of `derived_cube` cubes. `derived_cube`s are a S3 class
#' representation of a cube generated by the classification workflow starting
#' from an Earth Observation data cube.
#'
#' There are several classes of `derived_cube`:
#' * probs_cube: multilayer probability cube produced by a
#' classification with the probabilities attributed to each class by a
#' model. The possible band names are 'probs', 'bayes', and
#' 'bilat', acronyms for 'probability', 'Bayesian smoothing', and
#' 'Bilateral smoothing'.
#' * class_cube: labeled cube (classified map) produced by choosing
#' a label for each pixel. Its unique band name is 'class'.
#' * uncertainty_cube: a cube produced to measure the uncertainty of
#' a classification for each pixel. The possible band names are
#' 'least', 'entropy', and 'margin', acronyms for the method used to
#' produce the cube.
#'
#' Values for `derived_class` and band are lowercase. This was
#' done to avoid conflicts with `eo_cube` band naming (uppercase).
#' The config entries of a `derived_cube` are located in
#' `derived_cube -> <derived_class>` key.
#'
#' @examples
#' if (sits_run_examples()) {
#' # get S3 class value that a derived_cube of class 'probs' must have
#' .conf_derived_s3class("probs")
#' }
#'
NULL
#' @title Get the S3 class values of a `derived_cube`
#' @noRd
#' @param derived_class A `derived_cube` class name.
#' @return A S3 class.
.conf_derived_s3class <- function(derived_class) {
# derived_class is lowercase
derived_class <- tolower(derived_class)
.conf("derived_cube", derived_class, "s3_class")
}
#' @title Get the S3 class values of a `vector_cube`
#' @noRd
#' @param vector_class A `vector_cube` class name.
#' @return A S3 class.
.conf_vector_s3class <- function(vector_class) {
# derived_class is lowercase