CoolFace
Apppublic

abhirsc/spatial-toolkit-py

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes
utils.R142 linesDownload Raw Back to R
1`%||%` <- function(x, y) {2  if (is.null(x) || identical(x, "")) y else x3}4 5is_raster_input <- function(x) {6  inherits(x, "SpatRaster") ||7    (is.character(x) && tolower(tools::file_ext(x)) %in% c("tif", "tiff", "nc", "asc"))8}9 10normalize_raster <- function(x) {11  if (inherits(x, "SpatRaster")) {12    return(x)13  }14 15  terra::rast(x)16}17 18normalize_vector <- function(x) {19  if (inherits(x, "SpatVector")) {20    return(x)21  }22 23  if (inherits(x, "sf")) {24    return(terra::vect(x))25  }26 27  terra::vect(x)28}29 30append_raster_stats <- function(report, stats) {31  if (is.null(stats)) {32    return(report)33  }34 35  stats_df <- data.frame(36    check = paste0("stat_", rownames(stats)),37    value = apply(stats, 1, function(row) paste(names(row), row, collapse = "; ")),38    stringsAsFactors = FALSE39  )40 41  rbind(report, stats_df)42}43 44read_spatial_by_extension <- function(path) {45  extension <- tolower(tools::file_ext(path))46 47  if (extension %in% c("tif", "tiff", "nc", "asc")) {48    return(normalize_raster(path))49  }50 51  normalize_vector(path)52}53 54resolve_uploaded_spatial_path <- function(upload_value) {55  if (is.null(upload_value) || is.null(upload_value$datapath)) {56    stop("No uploaded files were provided.", call. = FALSE)57  }58 59  upload_dir <- file.path(60    tempdir(),61    paste0("spatialtoolkit-upload-", as.integer(stats::runif(1, 1, 1e9)))62  )63  dir.create(upload_dir, recursive = TRUE, showWarnings = FALSE)64 65  copied_paths <- vapply(seq_len(nrow(upload_value)), function(i) {66    destination <- file.path(upload_dir, upload_value$name[[i]])67    file.copy(upload_value$datapath[[i]], destination, overwrite = TRUE)68    destination69  }, character(1))70 71  choose_primary_spatial_path(copied_paths)72}73 74choose_primary_spatial_path <- function(paths) {75  extensions <- tolower(tools::file_ext(paths))76  preferred_order <- c("shp", "gpkg", "geojson", "tif", "tiff", "nc", "asc")77 78  for (extension in preferred_order) {79    matches <- paths[extensions == extension]80    if (length(matches) > 0) {81      return(matches[[1]])82    }83  }84 85  stop("No supported spatial file was found in the dropped files.", call. = FALSE)86}87 88resolve_uploaded_paths <- function(upload_value) {89  if (is.null(upload_value) || is.null(upload_value$datapath)) {90    stop("No uploaded files were provided.", call. = FALSE)91  }92 93  upload_dir <- file.path(94    tempdir(),95    paste0("spatialtoolkit-upload-", as.integer(stats::runif(1, 1, 1e9)))96  )97  dir.create(upload_dir, recursive = TRUE, showWarnings = FALSE)98 99  vapply(seq_len(nrow(upload_value)), function(i) {100    destination <- file.path(upload_dir, upload_value$name[[i]])101    file.copy(upload_value$datapath[[i]], destination, overwrite = TRUE)102    destination103  }, character(1))104}105 106load_uploaded_spatial <- function(upload_value) {107  paths <- resolve_uploaded_paths(upload_value)108  extensions <- tolower(tools::file_ext(paths))109  raster_paths <- paths[extensions %in% c("tif", "tiff", "nc", "asc")]110  vector_paths <- paths[extensions %in% c("shp", "gpkg", "geojson")]111 112  if (length(vector_paths) > 0) {113    primary_path <- choose_primary_spatial_path(vector_paths)114    return(list(115      kind = "vector",116      primary_path = primary_path,117      paths = paths,118      spatial = normalize_vector(primary_path)119    ))120  }121 122  if (length(raster_paths) > 1) {123    return(list(124      kind = "raster_stack",125      primary_path = raster_paths[[1]],126      paths = raster_paths,127      spatial = terra::rast(raster_paths)128    ))129  }130 131  if (length(raster_paths) == 1) {132    return(list(133      kind = "raster",134      primary_path = raster_paths[[1]],135      paths = raster_paths,136      spatial = normalize_raster(raster_paths[[1]])137    ))138  }139 140  stop("No supported spatial file was found in the dropped files.", call. = FALSE)141}142