From 6a0f8ccfd82e2becef4dfbd166eb61ab317379f6 Mon Sep 17 00:00:00 2001 From: Felipe Date: Mon, 5 Feb 2024 20:08:29 +0000 Subject: [PATCH 1/5] Fix eProblems with spherical geometry when extracting MODIS time series using sf #1065 --- R/api_cube.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/R/api_cube.R b/R/api_cube.R index adcd3ceb5..b6d2d6c6e 100644 --- a/R/api_cube.R +++ b/R/api_cube.R @@ -1178,6 +1178,12 @@ NULL } .cube_split_chunks_samples <- function(cube, samples_sf) { + # Hold s2 status + s2_status <- sf::sf_use_s2() + suppressMessages(sf::sf_use_s2(FALSE)) + # Back to original status on exit + on.exit(suppressMessages(sf::sf_use_s2(s2_status))) + # Get block size of raster file block <- .raster_file_blocksize(.raster_open_rast(.tile_path(cube))) cube_chunks <- slider::slide(cube, function(tile) { chunks <- .tile_chunks_create( From 65e2c8160f717f9ebccb343c09515b8050e5db25 Mon Sep 17 00:00:00 2001 From: Felipe Date: Mon, 5 Feb 2024 20:42:01 +0000 Subject: [PATCH 2/5] Remove values in case invalid block is read --- R/api_apply.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/api_apply.R b/R/api_apply.R index db94f4033..6a9ee12de 100644 --- a/R/api_apply.R +++ b/R/api_apply.R @@ -94,6 +94,9 @@ values <- .apply_data_read( tile = feature, block = block, in_bands = in_bands ) + if (all(is.na(values))) { + return(NULL) + } # Evaluate expression here # Band and kernel evaluation values <- eval( From c090428d2e2fcf7b759e6df9160b25adf03d81ea Mon Sep 17 00:00:00 2001 From: Felipe Date: Mon, 5 Feb 2024 20:43:04 +0000 Subject: [PATCH 3/5] remove messages from spatial operations --- R/api_space_time_operations.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/api_space_time_operations.R b/R/api_space_time_operations.R index 77627f447..0fcff0df8 100644 --- a/R/api_space_time_operations.R +++ b/R/api_space_time_operations.R @@ -72,7 +72,7 @@ .intersects <- function(x, y) { as_crs <- sf::st_crs(x) y <- sf::st_transform(y, crs = as_crs) - apply(sf::st_intersects(x, y, sparse = FALSE), 1, any) + apply(suppressMessages(sf::st_intersects(x, y, sparse = FALSE)), 1, any) } #' @title Spatial within #' @noRd @@ -100,7 +100,7 @@ .within <- function(x, y) { as_crs <- sf::st_crs(x) y <- sf::st_transform(y, crs = as_crs) - apply(sf::st_within(x, y, sparse = FALSE), 1, any) + apply(suppressMessages(sf::st_within(x, y, sparse = FALSE)), 1, any) } #' @title Spatial contains #' @noRd From 04c20901f92fa7876b4223122bd41c3c7a909bc7 Mon Sep 17 00:00:00 2001 From: Felipe Date: Mon, 5 Feb 2024 20:49:57 +0000 Subject: [PATCH 4/5] adjust data type and properties to new computed indexes --- R/api_conf.R | 33 +++++------------- R/api_source.R | 2 +- R/api_tile.R | 3 +- inst/extdata/config_internals.yml | 56 ++++++++++--------------------- 4 files changed, 28 insertions(+), 66 deletions(-) diff --git a/R/api_conf.R b/R/api_conf.R index a3eac1737..71c102588 100644 --- a/R/api_conf.R +++ b/R/api_conf.R @@ -428,7 +428,8 @@ #' @keywords internal #' @noRd #' @return names associated to the chosen access key -.conf_names <- function(key) { +.conf_names <- function(...) { + key <- c(...) res <- tryCatch( { names(sits_env$config[[key]]) @@ -928,41 +929,23 @@ NULL #' @param source Data source. #' @param collection Collection in the data source. #' @param band Band name -#' @param tile Tile #' @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, tile = NULL) { +.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)) { - data_type <- "INT2S" - # does the file exist? - if (!purrr::is_null(tile)) { - band_path <- .tile_path(tile, band) - if (!purrr::is_null(band_path)) { - rast <- terra::rast(band_path) - data_type <- terra::datatype(rast) - conf_band <- .conf("default_vales", data_type) - } else { - # file does not exist - if (!.has(.conf("default_vales", band))) - conf_band <- .conf("default_vales", band) - else - conf_band <- .conf("default_vales", "INT4S") - } + if (band %in% .conf_names("default_values", "eo_cube")) { + return(.conf("default_values", "eo_cube", band)) } + return(.conf("default_values", "eo_cube", "default")) } - else { - # Get band config value and return it - conf_band <- .conf("sources", source, - "collections", collection, - "bands", band) - } - return(conf_band) + # Get band config value and return it + .conf("sources", source, "collections", collection, "bands", band) } #' @title Config functions for derived_cube #' @noRd diff --git a/R/api_source.R b/R/api_source.R index a758a8434..8eadb9b87 100644 --- a/R/api_source.R +++ b/R/api_source.R @@ -526,7 +526,7 @@ NULL # check source .source_check(source = source) # get collections from source - collections <- .conf_names(c("sources", source, "collections")) + collections <- .conf_names("sources", source, "collections") return(collections) } diff --git a/R/api_tile.R b/R/api_tile.R index 70a757678..6512837a7 100644 --- a/R/api_tile.R +++ b/R/api_tile.R @@ -537,8 +537,7 @@ NULL .tile_band_conf.eo_cube <- function(tile, band) { .conf_eo_band( source = .tile_source(tile), collection = .tile_collection(tile), - band = band[[1]], - tile = tile + band = band[[1]] ) } #' @export diff --git a/inst/extdata/config_internals.yml b/inst/extdata/config_internals.yml index 0cef76abd..891228ae9 100644 --- a/inst/extdata/config_internals.yml +++ b/inst/extdata/config_internals.yml @@ -101,44 +101,24 @@ probs_cube_class : ["probs_cube", "raster_cube", "sits_cube", "tbl_df", "tbl", "data.frame"] # Default values for non-registered bands -default_values : - INT2S : &conf_default_int2s - data_type : "INT2S" - missing_value: -32768 - minimum_value: -10000 - maximum_value: 10000 - offset_value : 0 - scale_factor : 0.0001 - - INT4S : &conf_default_int4s - data_type : "INT4S" - missing_value: -2147483648 - minimum_value: -2147483647 - maximum_value: 2147483647 - offset_value : 0 - scale_factor : 0.0001 - - FLT4S : - data_type : "INT4S" - missing_value: -Inf - minimum_value: -Inf - maximum_value: Inf - offset_value : 0 - scale_factor : 1 - - NDVI : - <<: *conf_default_int2s - - EVI : - <<: *conf_default_int2s - - eo_cube : - data_type : "INT2S" - missing_value: -32768 - minimum_value: -10000 - maximum_value: 10000 - offset_value : 0 - scale_factor : 0.0001 +default_values : + eo_cube : + NDVI : &ndvi_int2s_eo + data_type : "INT2S" + missing_value: -32767 + minimum_value: -10000 + maximum_value: 10000 + offset_value : 0 + scale_factor : 0.0001 + EVI : + <<: *ndvi_int2s_eo + default : + data_type : "FLT4S" + missing_value: -3.402823466385288e+38 + minimum_value: -3.402823466385288e+38 + maximum_value: 1.7014118346015974e+38 + offset_value : 0 + scale_factor : 1 # Derived cube definitions derived_cube : From ca94dba368b84c8df0fc3ce068afcae41fa2eca5 Mon Sep 17 00:00:00 2001 From: Felipe Date: Mon, 5 Feb 2024 21:26:04 +0000 Subject: [PATCH 5/5] add more indices in config_internals --- inst/extdata/config_internals.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/inst/extdata/config_internals.yml b/inst/extdata/config_internals.yml index 891228ae9..757c5a1c2 100644 --- a/inst/extdata/config_internals.yml +++ b/inst/extdata/config_internals.yml @@ -110,8 +110,29 @@ default_values : maximum_value: 10000 offset_value : 0 scale_factor : 0.0001 + NDNI : &ndni_int2s_eo + data_type : "INT2S" + missing_value: -32767 + minimum_value: 0 + maximum_value: 10000 + offset_value : 0 + scale_factor : 0.0001 EVI : <<: *ndvi_int2s_eo + NDWI : + <<: *ndvi_int2s_eo + GNDVI : + <<: *ndvi_int2s_eo + NBR : + <<: *ndvi_int2s_eo + NDSI : + <<: *ndvi_int2s_eo + NDRE : + <<: *ndvi_int2s_eo + SAVI : + <<: *ndvi_int2s_eo + MSAVI2 : + <<: *ndvi_int2s_eo default : data_type : "FLT4S" missing_value: -3.402823466385288e+38