diff --git a/R/geotargets-option.R b/R/geotargets-option.R index 04ef3dd..3c05527 100644 --- a/R/geotargets-option.R +++ b/R/geotargets-option.R @@ -37,11 +37,16 @@ geotargets_option_set <- function( gdal_vector_creation_options = NULL ) { + # TODO do this programmatically with formals() or something? `options()` also accepts a named list options( - "geotargets.gdal.raster.driver" = gdal_raster_driver %||% geotargets_option_get("gdal.raster.driver"), - "geotargets.gdal.raster.creation.options" = gdal_raster_creation_options %||% geotargets_option_get("gdal.raster.creation.options"), - "geotargets.gdal.vector.driver" = gdal_vector_driver %||% geotargets_option_get("gdal.vector.driver"), - "geotargets.gdal.vector.creation.options" = gdal_vector_creation_options %||% geotargets_option_get("gdal.vector.creation.options") + "geotargets.gdal.raster.driver" = gdal_raster_driver %||% + geotargets_option_get("gdal.raster.driver"), + "geotargets.gdal.raster.creation.options" = gdal_raster_creation_options %||% + geotargets_option_get("gdal.raster.creation.options"), + "geotargets.gdal.vector.driver" = gdal_vector_driver %||% + geotargets_option_get("gdal.vector.driver"), + "geotargets.gdal.vector.creation.options" = gdal_vector_creation_options %||% + geotargets_option_get("gdal.vector.creation.options") ) } diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index 3bccfc8..2449873 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -87,7 +87,10 @@ tar_terra_rast <- function(name, path, filetype = Sys.getenv("GEOTARGETS_GDAL_RASTER_DRIVER"), overwrite = TRUE, - gdal = strsplit(Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS", unset = ";"), ";")[[1]] + gdal = strsplit( + Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS", + unset = ";"), + ";")[[1]] ) }, marshal = function(object) terra::wrap(object), @@ -103,8 +106,12 @@ tar_terra_rast <- function(name, resources = targets::tar_resources( custom_format = targets::tar_resources_custom_format( #these envvars are used in write function of format - envvars = c("GEOTARGETS_GDAL_RASTER_DRIVER" = filetype, - "GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS" = paste0(gdal, collapse = ";")) + envvars = c( + "GEOTARGETS_GDAL_RASTER_DRIVER" = filetype, + "GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS" = ( + paste0(gdal, collapse = ";") + ) + ) ) ), storage = storage, diff --git a/R/tar-terra-sprc.R b/R/tar-terra-sprc.R index 84ac6c7..c5068b5 100644 --- a/R/tar-terra-sprc.R +++ b/R/tar-terra-sprc.R @@ -61,15 +61,15 @@ tar_terra_sprc <- function(name, storage = targets::tar_option_get("storage"), retrieval = targets::tar_option_get("retrieval"), cue = targets::tar_option_get("cue")) { - filetype <- filetype %||% "GTiff" + check_pkg_installed("terra") + gdal <- gdal %||% character(0) + filetype <- filetype %||% "GTiff" # check that filetype option is available drv <- get_gdal_available_driver_list("raster") filetype <- rlang::arg_match0(filetype, drv$name) - check_pkg_installed("terra") - name <- targets::tar_deparse_language(substitute(name)) envir <- targets::tar_option_get("envir") @@ -86,16 +86,7 @@ tar_terra_sprc <- function(name, tidy_eval = tidy_eval ) - drv <- get_gdal_available_driver_list("raster") - - # if not specified by user, pull the corresponding geotargets option - filetype <- filetype %||% geotargets_option_get("gdal.raster.driver") - filetype <- rlang::arg_match0(filetype, drv$name) - - gdal <- gdal %||% geotargets_option_get("gdal.raster.creation.options") - - .write_terra_rasters_sprc <- eval( - substitute( + .write_terra_rasters_sprc <- function(object, path) { for (i in seq(object)) { if (i > 1) { @@ -106,15 +97,12 @@ tar_terra_sprc <- function(name, terra::writeRaster( x = object[i], filename = path, - filetype = filetype, + filetype = Sys.getenv("GEOTARGETS_GDAL_RASTER_DRIVER"), overwrite = (i == 1), gdal = opt ) } - }, - list(filetype = filetype, gdal = gdal) - ) - ) + } targets::tar_target_raw( name = name, @@ -138,8 +126,12 @@ tar_terra_sprc <- function(name, resources = targets::tar_resources( custom_format = targets::tar_resources_custom_format( #these envvars are used in write function of format - envvars = c("GEOTARGETS_GDAL_RASTER_DRIVER" = filetype, - "GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS" = gdal) + envvars = c( + "GEOTARGETS_GDAL_RASTER_DRIVER" = filetype, + "GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS" = ( + paste0(gdal, collapse = ";") + ) + ) ) ), storage = storage, diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index 5cc31c0..9801e0c 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -105,8 +105,12 @@ tar_terra_vect <- function(name, resources = targets::tar_resources( custom_format = targets::tar_resources_custom_format( #these envvars are used in write function of format - envvars = c("GEOTARGETS_GDAL_VECTOR_DRIVER" = filetype, - "GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS" = paste0(gdal, collapse = ";")) + envvars = c( + "GEOTARGETS_GDAL_VECTOR_DRIVER" = filetype, + "GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS" = ( + paste0(gdal, collapse = ";") + ) + ) ) ), storage = storage, @@ -129,7 +133,10 @@ create_format_terra_vect <- function() { path, filetype = Sys.getenv("GEOTARGETS_GDAL_VECTOR_DRIVER"), overwrite = TRUE, - options = strsplit(Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", unset = ";"), ";")[[1]] + options = strsplit( + Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", + unset = ";"), + ";")[[1]] ) }, marshal = function(object) terra::wrap(object), @@ -151,7 +158,10 @@ create_format_terra_vect_shz <- function() { filename = paste0(path, ".shz"), filetype = "ESRI Shapefile", overwrite = TRUE, - options = strsplit(Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", unset = ";"), ";")[[1]] + options = strsplit( + Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", + unset = ";"), + ";")[[1]] ) file.rename(paste0(path, ".shz"), path) }, diff --git a/R/utils.R b/R/utils.R index b6d4b1d..b723273 100644 --- a/R/utils.R +++ b/R/utils.R @@ -25,3 +25,12 @@ get_gdal_available_driver_list <- function(driver_type) { } drv } + +semicolon_split <- function(env_vars){ + strsplit(env_vars, ";")[[1]] +} + +semicolon_paste <- function(vec){ + paste0(vec, collapse = ";") +} + diff --git a/tests/testthat/test-tar-terra-sprc.R b/tests/testthat/test-tar-terra-sprc.R index e2d6bf2..e3e5b73 100644 --- a/tests/testthat/test-tar-terra-sprc.R +++ b/tests/testthat/test-tar-terra-sprc.R @@ -31,7 +31,5 @@ targets::tar_test("tar_terra_sprc() works", { targets::tar_make() x <- targets::tar_read(raster_elevs) expect_s4_class(x, "SpatRasterCollection") - expect_snapshot( - x - ) + expect_snapshot(x) })