Skip to content

Commit 73858d6

Browse files
committed
wbt_file_path: add support for terra objects that have a file source for raster and vector inputs
- for #119
1 parent 6cbf9c6 commit 73858d6

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# whitebox 2.3.4
22

3-
* Exported `wbt_file_path()`, a function previously used internally for creating safe, expanded, quoted, paths for building WhiteboxTools commands.
3+
* 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.
44

55
# whitebox 2.3.3
66

R/wbt.R

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,8 @@ wbt_system_call <- function(argstring,
11271127
#'
11281128
#' @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.
11291129
#'
1130-
#' @param x character. Vector of file paths or strings of file paths for passing as arguments to WhiteboxTools.
1130+
#' @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()`
1131+
#'
11311132
#' @param shell_quote logical. Shell quotes around result? Default: `TRUE`
11321133
#' @param delimiter character. Either `","` (default) or `";"` allowed by WhiteboxTools.
11331134
#' @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,
11521153
#' wbt_file_path(c("~/abc.tif", "~/def.tif"))
11531154
#'
11541155
wbt_file_path <- function(x, shell_quote = TRUE, delimiter = ",", check_exists = FALSE) {
1156+
if (inherits(x, c("RasterLayer", "RasterStack"))) {
1157+
if (requireNamespace("terra")) {
1158+
x <- terra::rast(x)
1159+
}
1160+
}
1161+
1162+
if (inherits(x, c('SpatRaster','SpatRasterCollection',
1163+
'SpatVector', 'SpatVectorProxy'))) {
1164+
if (requireNamespace("terra")) {
1165+
x2 <- paste0(terra::sources(x), collapse = delimiter)
1166+
if (nchar(x2) == 0) {
1167+
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 <https://www.whiteboxgeo.com/manual/wbt_book/supported_formats.html> for details.", call. = FALSE)
1168+
}
1169+
x <- x2
1170+
}
1171+
}
1172+
11551173
delimiter <- match.arg(trimws(delimiter), c(",", ";"))
11561174
x <- path.expand(strsplit(
11571175
paste0(as.character(x), collapse = ","), ";|,"

man/wbt_file_path.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)