From e1cf359d5d4831d746f892ab480ff6c79d748218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darius=20A=2E=20G=C3=B6rgen?= Date: Sun, 3 Nov 2024 09:20:59 +0000 Subject: [PATCH] adds normalizePath() calls to spatial utils --- NEWS.md | 2 ++ R/spatial-utils.R | 4 ++++ tests/testthat/test-get_resources.R | 4 ++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 375d93c6..289066bd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ - `get_biodiversity_intactness_index()`, `get_iucn()`, and `get_key_biodiversity_areas()` now use `spds_exists()` to check if input files exist +- `spds_exist()` and `make_footprints()` now use `normalizePath()` allowing better + handling of relative file paths ## Internal diff --git a/R/spatial-utils.R b/R/spatial-utils.R index 7104268c..ff48728d 100644 --- a/R/spatial-utils.R +++ b/R/spatial-utils.R @@ -34,6 +34,7 @@ #' spds_exists <- function(path, oo = character(0), what = c("vector", "raster")) { what <- match.arg(what) + path <- normalizePath(path, mustWork = FALSE) util <- switch(what, vector = "ogrinfo", raster = "gdalinfo" @@ -138,6 +139,7 @@ make_footprints <- function(srcs = NULL, if (inherits(srcs, "character")) { what <- match.arg(what) + srcs <- normalizePath(srcs, mustWork = FALSE) srcs <- switch(what, vector = purrr::map2(srcs, oo, function(src, opt) .vector_footprint(src, opt)), raster = purrr::map2(srcs, oo, function(src, opt) .raster_footprint(src, opt)), @@ -390,7 +392,9 @@ prep_resources <- function(x, avail_resources = NULL, resources = NULL, mode = c what = c("vector", "raster")) { what <- match.arg(what) stopifnot(is.character(source) && length(source) == 1) + source <- normalizePath(source, mustWork = FALSE) stopifnot(is.character(destination) && length(destination) == 1) + destination <- normalizePath(destination, mustWork = FALSE) stopifnot(is.null(opts) || is.character(opts)) if (is.null(opts)) opts <- character(0) diff --git a/tests/testthat/test-get_resources.R b/tests/testthat/test-get_resources.R index a772be6c..c05ee3c9 100644 --- a/tests/testthat/test-get_resources.R +++ b/tests/testthat/test-get_resources.R @@ -64,10 +64,10 @@ test_that("test make_footprints works", { file.copy(resource_dir, temp_loc, recursive = TRUE) outdir <- file.path(tempdir(), "mapme.biodiversity", "res") - tifs <- list.files(file.path(outdir, "worldpop"), + tifs <- normalizePath(list.files(file.path(outdir, "worldpop"), pattern = ".tif$", recursive = TRUE, full.names = TRUE - ) + ), mustWork = FALSE) fps <- make_footprints(tifs, what = "raster") expect_true(inherits(fps, "sf"))