From 73858d6f6d6747baa7ee1b08654fe4e21370685a Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Sat, 18 Nov 2023 12:25:21 -0800 Subject: [PATCH] wbt_file_path: add support for terra objects that have a file source for raster and vector inputs - for #119 --- NEWS.md | 2 +- R/wbt.R | 20 +++++++++++++++++++- man/wbt_file_path.Rd | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 555da8d4b..a6ccb2293 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # whitebox 2.3.4 - * Exported `wbt_file_path()`, a function previously used internally for creating safe, expanded, quoted, paths for building WhiteboxTools commands. + * Exported `wbt_file_path()`, a function previously used internally for creating safe, expanded, quoted, paths for building WhiteboxTools commands. This function also supports the input of `terra` objects that are backed by file sources supported by WhiteboxTools. # whitebox 2.3.3 diff --git a/R/wbt.R b/R/wbt.R index 6cf813744..75cac05f6 100644 --- a/R/wbt.R +++ b/R/wbt.R @@ -1127,7 +1127,8 @@ wbt_system_call <- function(argstring, #' #' @details If an input vector contains `";"` or `","` this is considered, path expansion is performed on the substrings. If the input vector has length greater than `1`, the vector is concatenated with `","` or `";"` to create a single output string. #' -#' @param x character. Vector of file paths or strings of file paths for passing as arguments to WhiteboxTools. +#' @param x character or `terra` object. Vector of file paths or strings of file paths for passing as arguments to WhiteboxTools. If the object is of class `SpatRaster`, `SpatRasterCollection`, `SpatVector` or `SpatVectorProxy` the sources are extracted with `terra::sources()` +#' #' @param shell_quote logical. Shell quotes around result? Default: `TRUE` #' @param delimiter character. Either `","` (default) or `";"` allowed by WhiteboxTools. #' @param check_exists logical. Check if file(s) in x exist? Useful for input values. Default: `FALSE` @@ -1152,6 +1153,23 @@ wbt_system_call <- function(argstring, #' wbt_file_path(c("~/abc.tif", "~/def.tif")) #' wbt_file_path <- function(x, shell_quote = TRUE, delimiter = ",", check_exists = FALSE) { + if (inherits(x, c("RasterLayer", "RasterStack"))) { + if (requireNamespace("terra")) { + x <- terra::rast(x) + } + } + + if (inherits(x, c('SpatRaster','SpatRasterCollection', + 'SpatVector', 'SpatVectorProxy'))) { + if (requireNamespace("terra")) { + x2 <- paste0(terra::sources(x), collapse = delimiter) + if (nchar(x2) == 0) { + stop("The supplied 'terra' object for `", as.character(substitute(x)),"` is not backed by a file. Try loading the object directly from the source file with `terra::rast()` or `terra::vect()`. Various raster formats and ESRI Shapefile are supported. See for details.", call. = FALSE) + } + x <- x2 + } + } + delimiter <- match.arg(trimws(delimiter), c(",", ";")) x <- path.expand(strsplit( paste0(as.character(x), collapse = ","), ";|," diff --git a/man/wbt_file_path.Rd b/man/wbt_file_path.Rd index 484188f36..75697dd5e 100644 --- a/man/wbt_file_path.Rd +++ b/man/wbt_file_path.Rd @@ -7,7 +7,7 @@ wbt_file_path(x, shell_quote = TRUE, delimiter = ",", check_exists = FALSE) } \arguments{ -\item{x}{character. Vector of file paths or strings of file paths for passing as arguments to WhiteboxTools.} +\item{x}{character or \code{terra} object. Vector of file paths or strings of file paths for passing as arguments to WhiteboxTools. If the object is of class \code{SpatRaster}, \code{SpatRasterCollection}, \code{SpatVector} or \code{SpatVectorProxy} the sources are extracted with \code{terra::sources()}} \item{shell_quote}{logical. Shell quotes around result? Default: \code{TRUE}}