`%||%` <- function(x, y) { if (is.null(x) || identical(x, "")) y else x } is_raster_input <- function(x) { inherits(x, "SpatRaster") || (is.character(x) && tolower(tools::file_ext(x)) %in% c("tif", "tiff", "nc", "asc")) } normalize_raster <- function(x) { if (inherits(x, "SpatRaster")) { return(x) } terra::rast(x) } normalize_vector <- function(x) { if (inherits(x, "SpatVector")) { return(x) } if (inherits(x, "sf")) { return(terra::vect(x)) } terra::vect(x) } append_raster_stats <- function(report, stats) { if (is.null(stats)) { return(report) } stats_df <- data.frame( check = paste0("stat_", rownames(stats)), value = apply(stats, 1, function(row) paste(names(row), row, collapse = "; ")), stringsAsFactors = FALSE ) rbind(report, stats_df) } read_spatial_by_extension <- function(path) { extension <- tolower(tools::file_ext(path)) if (extension %in% c("tif", "tiff", "nc", "asc")) { return(normalize_raster(path)) } normalize_vector(path) } resolve_uploaded_spatial_path <- function(upload_value) { if (is.null(upload_value) || is.null(upload_value$datapath)) { stop("No uploaded files were provided.", call. = FALSE) } upload_dir <- file.path( tempdir(), paste0("spatialtoolkit-upload-", as.integer(stats::runif(1, 1, 1e9))) ) dir.create(upload_dir, recursive = TRUE, showWarnings = FALSE) copied_paths <- vapply(seq_len(nrow(upload_value)), function(i) { destination <- file.path(upload_dir, upload_value$name[[i]]) file.copy(upload_value$datapath[[i]], destination, overwrite = TRUE) destination }, character(1)) choose_primary_spatial_path(copied_paths) } choose_primary_spatial_path <- function(paths) { extensions <- tolower(tools::file_ext(paths)) preferred_order <- c("shp", "gpkg", "geojson", "tif", "tiff", "nc", "asc") for (extension in preferred_order) { matches <- paths[extensions == extension] if (length(matches) > 0) { return(matches[[1]]) } } stop("No supported spatial file was found in the dropped files.", call. = FALSE) } resolve_uploaded_paths <- function(upload_value) { if (is.null(upload_value) || is.null(upload_value$datapath)) { stop("No uploaded files were provided.", call. = FALSE) } upload_dir <- file.path( tempdir(), paste0("spatialtoolkit-upload-", as.integer(stats::runif(1, 1, 1e9))) ) dir.create(upload_dir, recursive = TRUE, showWarnings = FALSE) vapply(seq_len(nrow(upload_value)), function(i) { destination <- file.path(upload_dir, upload_value$name[[i]]) file.copy(upload_value$datapath[[i]], destination, overwrite = TRUE) destination }, character(1)) } load_uploaded_spatial <- function(upload_value) { paths <- resolve_uploaded_paths(upload_value) extensions <- tolower(tools::file_ext(paths)) raster_paths <- paths[extensions %in% c("tif", "tiff", "nc", "asc")] vector_paths <- paths[extensions %in% c("shp", "gpkg", "geojson")] if (length(vector_paths) > 0) { primary_path <- choose_primary_spatial_path(vector_paths) return(list( kind = "vector", primary_path = primary_path, paths = paths, spatial = normalize_vector(primary_path) )) } if (length(raster_paths) > 1) { return(list( kind = "raster_stack", primary_path = raster_paths[[1]], paths = raster_paths, spatial = terra::rast(raster_paths) )) } if (length(raster_paths) == 1) { return(list( kind = "raster", primary_path = raster_paths[[1]], paths = raster_paths, spatial = normalize_raster(raster_paths[[1]]) )) } stop("No supported spatial file was found in the dropped files.", call. = FALSE) }