Skip to content

Commit

Permalink
adds normalizePath() calls to spatial utils
Browse files Browse the repository at this point in the history
  • Loading branch information
goergen95 committed Nov 3, 2024
1 parent 832145f commit e1cf359
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions R/spatial-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-get_resources.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit e1cf359

Please sign in to comment.