From ddfe587ec68c90006532c5033de5001f3d617f17 Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Wed, 13 Mar 2024 16:34:45 -0700 Subject: [PATCH 01/11] add filetype arg to tar_terra_vect() --- R/tar-terra-vect.R | 87 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 13 deletions(-) diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index eadc681..92bfe17 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -37,6 +37,9 @@ #' #' Provides targets format for `terra::vect` objects #' +#' @param filetype character. File format expressed as GDAL driver names passed to `terra::writeVector()` +#' @param gdal character. GDAL driver specific datasource creation options passed to `terra::writeVector()` +#' @param ... Additional arguments not yet used #' @inheritParams targets::tar_target #' #' @export @@ -66,6 +69,9 @@ tar_terra_vect <- function(name, command, pattern = NULL, + filetype = NULL, + gdal = NULL, + ..., packages = targets::tar_option_get("packages"), tidy_eval = targets::tar_option_get("tidy_eval"), library = targets::tar_option_get("library"), @@ -95,19 +101,33 @@ tar_terra_vect <- function(name, tidy_eval = tidy_eval ) - format_terra_shapefile_zip <- targets::tar_format( - read = function(path) terra::vect(paste0("/vsizip/{", path, "}")), - write = function(object, path) { - terra::writeVector( - x = object, - filename = paste0(path, ".shz"), - filetype = "ESRI Shapefile" - ) - file.rename(paste0(path, ".shz"), path) - }, - marshal = function(object) terra::wrap(object), - unmarshal = function(object) terra::unwrap(object) - ) + # TODO: pull defaults from geotargets package options + if (is.null(filetype)) { + filetype <- "GeoJSON" + } + + if (is.null(gdal)) { + gdal <- "ENCODING=UTF-8" + } + + if(filetype == "ESRI Shapefile") { + #special handling of ESRI shapefiles because the output is a dir of multiple files. + format <- targets::tar_format( + read = function(path) terra::vect(paste0("/vsizip/{", path, "}")), + write = function(object, path) { + terra::writeVector( + x = object, + filename = paste0(path, ".shz"), + filetype = "ESRI Shapefile" + ) + file.rename(paste0(path, ".shz"), path) + }, + marshal = function(object) terra::wrap(object), + unmarshal = function(object) terra::unwrap(object) + ) + } else { + format <- create_format_terra_vect(filetype, gdal, ...) + } targets::tar_target_raw( name = name, @@ -129,3 +149,44 @@ tar_terra_vect <- function(name, cue = cue ) } + + +#' @param filetype File format expressed as GDAL driver names passed to `terra::writeVector()` +#' @param gdal GDAL driver specific datasource creation options passed to `terra::writeVector()` +#' @param ... Additional arguments not yet used +#' @noRd +create_format_terra_vect <- function(filetype, gdal, ...) { + + if (!requireNamespace("terra")) { + stop("package 'terra' is required", call. = FALSE) + } + + # get list of drivers available for writing depending on what the user's GDAL supports + drv <- terra::gdal(drivers = TRUE) + drv <- drv[drv$type == "vector" & grepl("write", drv$can), ] + + if (is.null(filetype)) { + filetype <- "GeoJSON" + } + + filetype <- match.arg(filetype, drv$name) + + .write_terra_vector <- function(object, path) { + terra::writeVector( + object, + path, + filetype = NULL, + overwrite = TRUE, + gdal = NULL + ) + } + body(.write_terra_vector)[[2]][["filetype"]] <- filetype + body(.write_terra_vector)[[2]][["gdal"]] <- gdal + + targets::tar_format( + read = function(path) terra::rast(path), + write = .write_terra_vector, + marshal = function(object) terra::wrap(object), + unmarshal = function(object) terra::unwrap(object) + ) +} From f1823941c2328c001bf614939c189c1960c3475b Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Wed, 13 Mar 2024 16:34:53 -0700 Subject: [PATCH 02/11] remove unused test file --- tests/testthat/test-tar-shapefile.R | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 tests/testthat/test-tar-shapefile.R diff --git a/tests/testthat/test-tar-shapefile.R b/tests/testthat/test-tar-shapefile.R deleted file mode 100644 index 3bcae48..0000000 --- a/tests/testthat/test-tar-shapefile.R +++ /dev/null @@ -1,3 +0,0 @@ -test_that("tar_shapefile works", { - expect_equal(2 * 2, 4) -}) From da37064298f136a56706bb3abd15246af368b8af Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Wed, 13 Mar 2024 16:56:59 -0700 Subject: [PATCH 03/11] fix mistakes in copying from raster to vector --- R/tar-terra-vect.R | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index 92bfe17..c525913 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -110,7 +110,7 @@ tar_terra_vect <- function(name, gdal <- "ENCODING=UTF-8" } - if(filetype == "ESRI Shapefile") { + if (filetype == "ESRI Shapefile") { #special handling of ESRI shapefiles because the output is a dir of multiple files. format <- targets::tar_format( read = function(path) terra::vect(paste0("/vsizip/{", path, "}")), @@ -118,7 +118,9 @@ tar_terra_vect <- function(name, terra::writeVector( x = object, filename = paste0(path, ".shz"), - filetype = "ESRI Shapefile" + filetype = "ESRI Shapefile", + options = gdal, + overwrite = TRUE ) file.rename(paste0(path, ".shz"), path) }, @@ -126,7 +128,7 @@ tar_terra_vect <- function(name, unmarshal = function(object) terra::unwrap(object) ) } else { - format <- create_format_terra_vect(filetype, gdal, ...) + format <- create_format_terra_vect(filetype, options = gdal, ...) } targets::tar_target_raw( @@ -135,7 +137,7 @@ tar_terra_vect <- function(name, pattern = pattern, packages = packages, library = library, - format = format_terra_shapefile_zip, + format = format, repository = repository, iteration = iteration, error = error, @@ -152,10 +154,10 @@ tar_terra_vect <- function(name, #' @param filetype File format expressed as GDAL driver names passed to `terra::writeVector()` -#' @param gdal GDAL driver specific datasource creation options passed to `terra::writeVector()` +#' @param options GDAL driver specific datasource creation options passed to `terra::writeVector()` #' @param ... Additional arguments not yet used #' @noRd -create_format_terra_vect <- function(filetype, gdal, ...) { +create_format_terra_vect <- function(filetype, options, ...) { if (!requireNamespace("terra")) { stop("package 'terra' is required", call. = FALSE) @@ -177,14 +179,14 @@ create_format_terra_vect <- function(filetype, gdal, ...) { path, filetype = NULL, overwrite = TRUE, - gdal = NULL + options = NULL ) } body(.write_terra_vector)[[2]][["filetype"]] <- filetype - body(.write_terra_vector)[[2]][["gdal"]] <- gdal + body(.write_terra_vector)[[2]][["options"]] <- options targets::tar_format( - read = function(path) terra::rast(path), + read = function(path) terra::vect(path), write = .write_terra_vector, marshal = function(object) terra::wrap(object), unmarshal = function(object) terra::unwrap(object) From a8557ad0ddd6bbc2e115c8ce76de42408e8b24a7 Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Wed, 13 Mar 2024 16:57:14 -0700 Subject: [PATCH 04/11] add test for alternat filetype --- tests/testthat/test-tar-terra.R | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-tar-terra.R b/tests/testthat/test-tar-terra.R index 45898e2..ff7312f 100644 --- a/tests/testthat/test-tar-terra.R +++ b/tests/testthat/test-tar-terra.R @@ -4,7 +4,7 @@ targets::tar_test("tar_terra_rast() works", { list( geotargets::tar_terra_rast( test_terra_rast, - system.file("ex/elev.tif", package = "terra") |> terra::rast() + terra::rast(system.file("ex/elev.tif", package = "terra")) ) ) }) @@ -30,13 +30,20 @@ targets::tar_test("tar_terra_vect() works", { geotargets::tar_terra_vect( test_terra_vect, lux_area() + ), + geotargets::tar_terra_vect( + test_terra_vect2, + lux_area(), + filetype = "ESRI Shapefile" ) ) }) targets::tar_make() x <- targets::tar_read(test_terra_vect) + y <- targets::tar_read(test_terra_vect2) expect_s4_class(x, "SpatVector") - expect_snapshot( - x - ) + expect_s4_class(y, "SpatVector") + expect_snapshot(x) + expect_snapshot(y) + expect_equivalent(x, y) }) From 4225c0b599e99ce51e6fde5cb3473d9023770059 Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Wed, 13 Mar 2024 16:58:34 -0700 Subject: [PATCH 05/11] document() --- man/tar_terra_vect.Rd | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/man/tar_terra_vect.Rd b/man/tar_terra_vect.Rd index 3db638f..6c34471 100644 --- a/man/tar_terra_vect.Rd +++ b/man/tar_terra_vect.Rd @@ -8,6 +8,9 @@ tar_terra_vect( name, command, pattern = NULL, + filetype = NULL, + gdal = NULL, + ..., packages = targets::tar_option_get("packages"), tidy_eval = targets::tar_option_get("tidy_eval"), library = targets::tar_option_get("library"), @@ -49,6 +52,12 @@ For example, in a pipeline with numeric vector targets \code{x} and \code{y}, branches of \code{z} that each compute \code{x[1] + y[1]}, \code{x[2] + y[2]}, and so on. See the user manual for details.} +\item{filetype}{character. File format expressed as GDAL driver names passed to \code{terra::writeVector()}} + +\item{gdal}{character. GDAL driver specific datasource creation options passed to \code{terra::writeVector()}} + +\item{...}{Additional arguments not yet used} + \item{packages}{Character vector of packages to load right before the target runs or the output data is reloaded for downstream targets. Use \code{tar_option_set()} to set packages From ad5b30a5c382f50f5afc6c0139787fd1dbce3e92 Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Thu, 14 Mar 2024 09:37:11 -0700 Subject: [PATCH 06/11] fix special handling of shapefiles --- R/tar-terra-vect.R | 194 ++++++++++++++++------------- tests/testthat/_snaps/tar-terra.md | 19 ++- tests/testthat/test-tar-terra.R | 6 +- 3 files changed, 128 insertions(+), 91 deletions(-) diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index c525913..688ba72 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -33,15 +33,19 @@ # unmarshal = function(object) terra::unwrap(object) # ) -#' Targets format for terra vectors +#' Create a terra _SpatVector_ target #' -#' Provides targets format for `terra::vect` objects +#' Provides a target format for [terra::SpatVector-class] objects. #' -#' @param filetype character. File format expressed as GDAL driver names passed to `terra::writeVector()` -#' @param gdal character. GDAL driver specific datasource creation options passed to `terra::writeVector()` +#' @param filetype character. File format expressed as GDAL driver names passed to [terra::writeVector()]. See 'Note' for more details +#' @param gdal character. GDAL driver specific datasource creation options passed to [terra::writeVector()]. #' @param ... Additional arguments not yet used #' @inheritParams targets::tar_target #' +#' @note Although you may pass any supported GDAL vector driver to the +#' `filetype` argument, not all formats are guaranteed to work with +#' `geotargets`. At the moment, we have tested `GeoJSON` and `ESRI Shapefile` +#' which both appear to work generally. #' @export #' @examples #' if (Sys.getenv("TAR_LONG_EXAMPLES") == "true") { @@ -67,89 +71,75 @@ #' }) #' } tar_terra_vect <- function(name, - command, - pattern = NULL, - filetype = NULL, - gdal = NULL, - ..., - packages = targets::tar_option_get("packages"), - tidy_eval = targets::tar_option_get("tidy_eval"), - library = targets::tar_option_get("library"), - repository = targets::tar_option_get("repository"), - iteration = targets::tar_option_get("iteration"), - error = targets::tar_option_get("error"), - memory = targets::tar_option_get("memory"), - garbage_collection = targets::tar_option_get("garbage_collection"), - deployment = targets::tar_option_get("deployment"), - priority = targets::tar_option_get("priority"), - resources = targets::tar_option_get("resources"), - storage = targets::tar_option_get("storage"), - retrieval = targets::tar_option_get("retrieval"), - cue = targets::tar_option_get("cue")) { - name <- targets::tar_deparse_language(substitute(name)) - - envir <- targets::tar_option_get("envir") - - command <- targets::tar_tidy_eval( - expr = as.expression(substitute(command)), - envir = envir, - tidy_eval = tidy_eval - ) - pattern <- targets::tar_tidy_eval( - expr = as.expression(substitute(pattern)), - envir = envir, - tidy_eval = tidy_eval - ) - - # TODO: pull defaults from geotargets package options - if (is.null(filetype)) { - filetype <- "GeoJSON" - } - - if (is.null(gdal)) { - gdal <- "ENCODING=UTF-8" - } - - if (filetype == "ESRI Shapefile") { - #special handling of ESRI shapefiles because the output is a dir of multiple files. - format <- targets::tar_format( - read = function(path) terra::vect(paste0("/vsizip/{", path, "}")), - write = function(object, path) { - terra::writeVector( - x = object, - filename = paste0(path, ".shz"), - filetype = "ESRI Shapefile", - options = gdal, - overwrite = TRUE - ) - file.rename(paste0(path, ".shz"), path) - }, - marshal = function(object) terra::wrap(object), - unmarshal = function(object) terra::unwrap(object) - ) - } else { - format <- create_format_terra_vect(filetype, options = gdal, ...) - } - - targets::tar_target_raw( - name = name, - command = command, - pattern = pattern, - packages = packages, - library = library, - format = format, - repository = repository, - iteration = iteration, - error = error, - memory = memory, - garbage_collection = garbage_collection, - deployment = deployment, - priority = priority, - resources = resources, - storage = storage, - retrieval = retrieval, - cue = cue - ) + command, + pattern = NULL, + filetype = NULL, + gdal = NULL, + ..., + packages = targets::tar_option_get("packages"), + tidy_eval = targets::tar_option_get("tidy_eval"), + library = targets::tar_option_get("library"), + repository = targets::tar_option_get("repository"), + iteration = targets::tar_option_get("iteration"), + error = targets::tar_option_get("error"), + memory = targets::tar_option_get("memory"), + garbage_collection = targets::tar_option_get("garbage_collection"), + deployment = targets::tar_option_get("deployment"), + priority = targets::tar_option_get("priority"), + resources = targets::tar_option_get("resources"), + storage = targets::tar_option_get("storage"), + retrieval = targets::tar_option_get("retrieval"), + cue = targets::tar_option_get("cue")) { + name <- targets::tar_deparse_language(substitute(name)) + + envir <- targets::tar_option_get("envir") + + command <- targets::tar_tidy_eval( + expr = as.expression(substitute(command)), + envir = envir, + tidy_eval = tidy_eval + ) + pattern <- targets::tar_tidy_eval( + expr = as.expression(substitute(pattern)), + envir = envir, + tidy_eval = tidy_eval + ) + + # TODO: pull defaults from geotargets package options + if (is.null(filetype)) { + filetype <- "GeoJSON" + } + + if (is.null(gdal)) { + gdal <- "ENCODING=UTF-8" + } + + if (filetype == "ESRI Shapefile") { + #special handling of ESRI shapefiles because the output is a dir of multiple files. + format <- create_format_terra_vect_shz(options = gdal, ...) + } else { + format <- create_format_terra_vect(filetype, options = gdal, ...) + } + + targets::tar_target_raw( + name = name, + command = command, + pattern = pattern, + packages = packages, + library = library, + format = format, + repository = repository, + iteration = iteration, + error = error, + memory = memory, + garbage_collection = garbage_collection, + deployment = deployment, + priority = priority, + resources = resources, + storage = storage, + retrieval = retrieval, + cue = cue + ) } @@ -192,3 +182,33 @@ create_format_terra_vect <- function(filetype, options, ...) { unmarshal = function(object) terra::unwrap(object) ) } + +#' Special handling for ESRI Shapefiles +#' @param options GDAL driver specific datasource creation options passed to `terra::writeVector()` +#' @param ... Additional arguments not yet used +#' @noRd +create_format_terra_vect_shz <- function(options, ...) { + + if (!requireNamespace("terra")) { + stop("package 'terra' is required", call. = FALSE) + } + + .write_terra_vector <- function(object, path) { + terra::writeVector( + x = object, + filename = paste0(path, ".shz"), + filetype = "ESRI Shapefile", + overwrite = TRUE, + options = NULL + ) + file.rename(paste0(path, ".shz"), path) + } + body(.write_terra_vector)[[2]][["options"]] <- options + + targets::tar_format( + read = function(path) terra::vect(paste0("/vsizip/{", path, "}")), + write = .write_terra_vector, + marshal = function(object) terra::wrap(object), + unmarshal = function(object) terra::unwrap(object) + ) +} diff --git a/tests/testthat/_snaps/tar-terra.md b/tests/testthat/_snaps/tar-terra.md index 02703c2..28044fc 100644 --- a/tests/testthat/_snaps/tar-terra.md +++ b/tests/testthat/_snaps/tar-terra.md @@ -22,7 +22,24 @@ geometry : polygons dimensions : 12, 6 (geometries, attributes) extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax) - source : test_terra_vect} (test_terra_vect) + source : test_terra_vect + coord. ref. : lon/lat WGS 84 (EPSG:4326) + names : ID_1 NAME_1 ID_2 NAME_2 AREA POP + type : + values : 1 Diekirch 1 Clervaux 312 18081 + 1 Diekirch 2 Diekirch 218 32543 + 1 Diekirch 3 Redange 259 18664 + +--- + + Code + y + Output + class : SpatVector + geometry : polygons + dimensions : 12, 6 (geometries, attributes) + extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax) + source : test_terra_vect_shz} (test_terra_vect_shz) coord. ref. : lon/lat WGS 84 (EPSG:4326) names : ID_1 NAME_1 ID_2 NAME_2 AREA POP type : diff --git a/tests/testthat/test-tar-terra.R b/tests/testthat/test-tar-terra.R index ff7312f..b3781ce 100644 --- a/tests/testthat/test-tar-terra.R +++ b/tests/testthat/test-tar-terra.R @@ -32,7 +32,7 @@ targets::tar_test("tar_terra_vect() works", { lux_area() ), geotargets::tar_terra_vect( - test_terra_vect2, + test_terra_vect_shz, lux_area(), filetype = "ESRI Shapefile" ) @@ -40,10 +40,10 @@ targets::tar_test("tar_terra_vect() works", { }) targets::tar_make() x <- targets::tar_read(test_terra_vect) - y <- targets::tar_read(test_terra_vect2) + y <- targets::tar_read(test_terra_vect_shz) expect_s4_class(x, "SpatVector") expect_s4_class(y, "SpatVector") expect_snapshot(x) expect_snapshot(y) - expect_equivalent(x, y) + expect_equal(terra::values(x), terra::values(y)) }) From a05675540c50a26c9bd5a5e13a2ed7ba20e14f2c Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Thu, 14 Mar 2024 09:37:26 -0700 Subject: [PATCH 07/11] use links in documentation --- R/tar-terra-rast.R | 6 +++--- man/tar_terra_rast.Rd | 6 +++--- man/tar_terra_vect.Rd | 14 ++++++++++---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index c42704a..be3c193 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -1,9 +1,9 @@ #' Create a terra _SpatRaster_ Target #' -#' Creates a target for a terra _SpatRaster_ object. +#' Provides a target forma for [terra::SpatRaster-class] objects. #' -#' @param filetype character. File format expressed as GDAL driver names passed to `terra::writeRaster()` -#' @param gdal character. GDAL driver specific datasource creation options passed to `terra::writeRaster()` +#' @param filetype character. File format expressed as GDAL driver names passed to [terra::writeRaster()] +#' @param gdal character. GDAL driver specific datasource creation options passed to [terra::writeRaster()] #' @param ... Additional arguments not yet used #' #' @inheritParams targets::tar_target diff --git a/man/tar_terra_rast.Rd b/man/tar_terra_rast.Rd index 727b7b7..1a242de 100644 --- a/man/tar_terra_rast.Rd +++ b/man/tar_terra_rast.Rd @@ -52,9 +52,9 @@ For example, in a pipeline with numeric vector targets \code{x} and \code{y}, branches of \code{z} that each compute \code{x[1] + y[1]}, \code{x[2] + y[2]}, and so on. See the user manual for details.} -\item{filetype}{character. File format expressed as GDAL driver names passed to \code{terra::writeRaster()}} +\item{filetype}{character. File format expressed as GDAL driver names passed to \code{\link[terra:writeRaster]{terra::writeRaster()}}} -\item{gdal}{character. GDAL driver specific datasource creation options passed to \code{terra::writeRaster()}} +\item{gdal}{character. GDAL driver specific datasource creation options passed to \code{\link[terra:writeRaster]{terra::writeRaster()}}} \item{...}{Additional arguments not yet used} @@ -216,7 +216,7 @@ explicitly from another language. rules that decide whether the target is up to date.} } \description{ -Creates a target for a terra \emph{SpatRaster} object. +Provides a target forma for \link[terra:SpatRaster-class]{terra::SpatRaster} objects. } \examples{ if (Sys.getenv("TAR_LONG_EXAMPLES") == "true") { diff --git a/man/tar_terra_vect.Rd b/man/tar_terra_vect.Rd index 6c34471..0827d8f 100644 --- a/man/tar_terra_vect.Rd +++ b/man/tar_terra_vect.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/tar-terra-vect.R \name{tar_terra_vect} \alias{tar_terra_vect} -\title{Targets format for terra vectors} +\title{Create a terra \emph{SpatVector} target} \usage{ tar_terra_vect( name, @@ -52,9 +52,9 @@ For example, in a pipeline with numeric vector targets \code{x} and \code{y}, branches of \code{z} that each compute \code{x[1] + y[1]}, \code{x[2] + y[2]}, and so on. See the user manual for details.} -\item{filetype}{character. File format expressed as GDAL driver names passed to \code{terra::writeVector()}} +\item{filetype}{character. File format expressed as GDAL driver names passed to \code{\link[terra:writeVector]{terra::writeVector()}}. See 'Note' for more details} -\item{gdal}{character. GDAL driver specific datasource creation options passed to \code{terra::writeVector()}} +\item{gdal}{character. GDAL driver specific datasource creation options passed to \code{\link[terra:writeVector]{terra::writeVector()}}.} \item{...}{Additional arguments not yet used} @@ -216,7 +216,13 @@ explicitly from another language. rules that decide whether the target is up to date.} } \description{ -Provides targets format for \code{terra::vect} objects +Provides a target format for \link[terra:SpatVector-class]{terra::SpatVector} objects. +} +\note{ +Although you may pass any supported GDAL vector driver to the +\code{filetype} argument, not all formats are guaranteed to work with +\code{geotargets}. At the moment, we have tested \code{GeoJSON} and \verb{ESRI Shapefile} +which both appear to work generally. } \examples{ if (Sys.getenv("TAR_LONG_EXAMPLES") == "true") { From 52a5aefc0d1a667cd75d1f8a1823acc9a07027de Mon Sep 17 00:00:00 2001 From: "Eric R. Scott" Date: Thu, 14 Mar 2024 15:50:45 -0700 Subject: [PATCH 08/11] Update R/tar-terra-vect.R Co-authored-by: Nicholas Tierney --- R/tar-terra-vect.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index 688ba72..dc08bd3 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -114,12 +114,12 @@ tar_terra_vect <- function(name, gdal <- "ENCODING=UTF-8" } - if (filetype == "ESRI Shapefile") { - #special handling of ESRI shapefiles because the output is a dir of multiple files. - format <- create_format_terra_vect_shz(options = gdal, ...) - } else { - format <- create_format_terra_vect(filetype, options = gdal, ...) - } +format <- ifelse( + test = filetype == "ESRI Shapefile", + #special handling of ESRI shapefiles because the output is a dir of multiple files. + yes = create_format_terra_vect_shz(options = gdal, ...), + no = create_format_terra_vect(filetype, options = gdal, ...) +) targets::tar_target_raw( name = name, From 68bcf58564e8e07cb96de93c0060d1dd46fce0ae Mon Sep 17 00:00:00 2001 From: Nicholas Tierney Date: Fri, 15 Mar 2024 13:25:38 +1100 Subject: [PATCH 09/11] fix typo R/tar-terra-rast.R Co-authored-by: Andrew Gene Brown --- R/tar-terra-rast.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index be3c193..a37090c 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -1,6 +1,6 @@ #' Create a terra _SpatRaster_ Target #' -#' Provides a target forma for [terra::SpatRaster-class] objects. +#' Provides a target format for [terra::SpatRaster-class] objects. #' #' @param filetype character. File format expressed as GDAL driver names passed to [terra::writeRaster()] #' @param gdal character. GDAL driver specific datasource creation options passed to [terra::writeRaster()] From eeb93c43250c18b827f0d23316751931e33f3d55 Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Fri, 15 Mar 2024 09:31:29 -0700 Subject: [PATCH 10/11] synchronize titles --- R/tar-terra-rast.R | 8 +++++--- R/tar-terra-vect.R | 17 +++++++++++------ man/tar_terra_rast.Rd | 10 ++++++---- man/tar_terra_vect.Rd | 6 ++++-- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index 10d4866..ca9119f 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -1,9 +1,11 @@ -#' Create a terra _SpatRaster_ Target +#' Create a terra _SpatRaster_ target #' #' Provides a target format for [terra::SpatRaster-class] objects. #' -#' @param filetype character. File format expressed as GDAL driver names passed to [terra::writeRaster()] -#' @param gdal character. GDAL driver specific datasource creation options passed to [terra::writeRaster()] +#' @param filetype character. File format expressed as GDAL driver names passed +#' to [terra::writeRaster()] +#' @param gdal character. GDAL driver specific datasource creation options +#' passed to [terra::writeRaster()] #' @param ... Additional arguments not yet used #' #' @inheritParams targets::tar_target diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index 9394ef1..0fef98f 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -1,9 +1,11 @@ -#' Targets format for terra vectors +#' Create a terra _SpatVector_ target #' #' Provides a target format for [terra::SpatVector-class] objects. #' -#' @param filetype character. File format expressed as GDAL driver names passed to [terra::writeVector()]. See 'Note' for more details -#' @param gdal character. GDAL driver specific datasource creation options passed to [terra::writeVector()]. +#' @param filetype character. File format expressed as GDAL driver names passed +#' to [terra::writeVector()]. See 'Note' for more details +#' @param gdal character. GDAL driver specific datasource creation options +#' passed to [terra::writeVector()]. #' @param ... Additional arguments not yet used #' @inheritParams targets::tar_target #' @@ -108,8 +110,10 @@ format <- ifelse( } -#' @param filetype File format expressed as GDAL driver names passed to `terra::writeVector()` -#' @param options GDAL driver specific datasource creation options passed to `terra::writeVector()` +#' @param filetype File format expressed as GDAL driver names passed to +#' `terra::writeVector()` +#' @param options GDAL driver specific datasource creation options passed to +#' `terra::writeVector()` #' @param ... Additional arguments not yet used #' @noRd create_format_terra_vect <- function(filetype, options, ...) { @@ -149,7 +153,8 @@ create_format_terra_vect <- function(filetype, options, ...) { } #' Special handling for ESRI Shapefiles -#' @param options GDAL driver specific datasource creation options passed to `terra::writeVector()` +#' @param options GDAL driver specific datasource creation options passed to +#' `terra::writeVector()` #' @param ... Additional arguments not yet used #' @noRd create_format_terra_vect_shz <- function(options, ...) { diff --git a/man/tar_terra_rast.Rd b/man/tar_terra_rast.Rd index 1a242de..d377096 100644 --- a/man/tar_terra_rast.Rd +++ b/man/tar_terra_rast.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/tar-terra-rast.R \name{tar_terra_rast} \alias{tar_terra_rast} -\title{Create a terra \emph{SpatRaster} Target} +\title{Create a terra \emph{SpatRaster} target} \usage{ tar_terra_rast( name, @@ -52,9 +52,11 @@ For example, in a pipeline with numeric vector targets \code{x} and \code{y}, branches of \code{z} that each compute \code{x[1] + y[1]}, \code{x[2] + y[2]}, and so on. See the user manual for details.} -\item{filetype}{character. File format expressed as GDAL driver names passed to \code{\link[terra:writeRaster]{terra::writeRaster()}}} +\item{filetype}{character. File format expressed as GDAL driver names passed +to \code{\link[terra:writeRaster]{terra::writeRaster()}}} -\item{gdal}{character. GDAL driver specific datasource creation options passed to \code{\link[terra:writeRaster]{terra::writeRaster()}}} +\item{gdal}{character. GDAL driver specific datasource creation options +passed to \code{\link[terra:writeRaster]{terra::writeRaster()}}} \item{...}{Additional arguments not yet used} @@ -216,7 +218,7 @@ explicitly from another language. rules that decide whether the target is up to date.} } \description{ -Provides a target forma for \link[terra:SpatRaster-class]{terra::SpatRaster} objects. +Provides a target format for \link[terra:SpatRaster-class]{terra::SpatRaster} objects. } \examples{ if (Sys.getenv("TAR_LONG_EXAMPLES") == "true") { diff --git a/man/tar_terra_vect.Rd b/man/tar_terra_vect.Rd index 0827d8f..553103f 100644 --- a/man/tar_terra_vect.Rd +++ b/man/tar_terra_vect.Rd @@ -52,9 +52,11 @@ For example, in a pipeline with numeric vector targets \code{x} and \code{y}, branches of \code{z} that each compute \code{x[1] + y[1]}, \code{x[2] + y[2]}, and so on. See the user manual for details.} -\item{filetype}{character. File format expressed as GDAL driver names passed to \code{\link[terra:writeVector]{terra::writeVector()}}. See 'Note' for more details} +\item{filetype}{character. File format expressed as GDAL driver names passed +to \code{\link[terra:writeVector]{terra::writeVector()}}. See 'Note' for more details} -\item{gdal}{character. GDAL driver specific datasource creation options passed to \code{\link[terra:writeVector]{terra::writeVector()}}.} +\item{gdal}{character. GDAL driver specific datasource creation options +passed to \code{\link[terra:writeVector]{terra::writeVector()}}.} \item{...}{Additional arguments not yet used} From 1b902e14289e9d61c75cc9f2856b796f618841bc Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Fri, 15 Mar 2024 10:04:19 -0700 Subject: [PATCH 11/11] use option defaults --- R/tar-terra-vect.R | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index 0fef98f..7cb9c82 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -72,21 +72,16 @@ tar_terra_vect <- function(name, tidy_eval = tidy_eval ) - # TODO: pull defaults from geotargets package options - if (is.null(filetype)) { - filetype <- "GeoJSON" - } - - if (is.null(gdal)) { - gdal <- "ENCODING=UTF-8" - } - -format <- ifelse( - test = filetype == "ESRI Shapefile", - #special handling of ESRI shapefiles because the output is a dir of multiple files. - yes = create_format_terra_vect_shz(options = gdal, ...), - no = create_format_terra_vect(filetype, options = gdal, ...) -) + # if not specified by user, pull the corresponding geotargets option + filetype <- filetype %||% geotargets_option_get("gdal.vector.driver") + gdal <- gdal %||% geotargets_option_get("gdal.vector.creation_options") + + format <- ifelse( + test = filetype == "ESRI Shapefile", + #special handling of ESRI shapefiles because the output is a dir of multiple files. + yes = create_format_terra_vect_shz(options = gdal, ...), + no = create_format_terra_vect(filetype, options = gdal, ...) + ) targets::tar_target_raw( name = name,