From ec47e066a24c420d50fd50245eeb5aeb3f2e4032 Mon Sep 17 00:00:00 2001 From: Gilberto Camara Date: Wed, 14 Feb 2024 17:29:12 -0300 Subject: [PATCH] Revert "Revert "Adjust data type and properties to new computed indexes in `sits_apply()`"" --- R/api_apply.R | 3 +++ R/api_conf.R | 33 ++++++++------------------------- R/api_cube.R | 6 ++++++ R/api_source.R | 2 +- R/api_space_time_operations.R | 4 ++-- R/api_tile.R | 3 +-- 6 files changed, 21 insertions(+), 30 deletions(-) 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( 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_cube.R b/R/api_cube.R index 479a90b25..c9fba1fe3 100644 --- a/R/api_cube.R +++ b/R/api_cube.R @@ -1196,6 +1196,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( 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_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 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