From 9c0ba1ff85b61506699fa508de6b3cbba93b184d Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Mon, 29 Apr 2024 16:17:29 -0700 Subject: [PATCH 1/8] check terra first --- R/tar-terra-sprc.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/tar-terra-sprc.R b/R/tar-terra-sprc.R index 84ac6c7..0795e7c 100644 --- a/R/tar-terra-sprc.R +++ b/R/tar-terra-sprc.R @@ -61,6 +61,8 @@ tar_terra_sprc <- function(name, storage = targets::tar_option_get("storage"), retrieval = targets::tar_option_get("retrieval"), cue = targets::tar_option_get("cue")) { + check_pkg_installed("terra") + filetype <- filetype %||% "GTiff" gdal <- gdal %||% character(0) @@ -68,7 +70,6 @@ tar_terra_sprc <- function(name, 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)) From c742f8b85701421bf75bf0468cd61183b7b0169c Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Mon, 29 Apr 2024 16:18:06 -0700 Subject: [PATCH 2/8] remove redundant options --- R/tar-terra-sprc.R | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/R/tar-terra-sprc.R b/R/tar-terra-sprc.R index 0795e7c..ba6cf3b 100644 --- a/R/tar-terra-sprc.R +++ b/R/tar-terra-sprc.R @@ -63,14 +63,13 @@ tar_terra_sprc <- function(name, cue = targets::tar_option_get("cue")) { check_pkg_installed("terra") - filetype <- filetype %||% "GTiff" 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) - name <- targets::tar_deparse_language(substitute(name)) envir <- targets::tar_option_get("envir") @@ -87,14 +86,6 @@ 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( function(object, path) { From 88292da8301428b6eb077a2e52081ea1a38c728a Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Mon, 29 Apr 2024 16:29:26 -0700 Subject: [PATCH 3/8] implement semicolon_split and _paste (#61) --- R/tar-terra-rast.R | 4 ++-- R/tar-terra-vect.R | 6 +++--- R/utils.R | 9 +++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index 3bccfc8..30fd26f 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -87,7 +87,7 @@ 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 = semicolon_split(Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS", unset = ";")) ) }, marshal = function(object) terra::wrap(object), @@ -104,7 +104,7 @@ tar_terra_rast <- function(name, 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 = ";")) + "GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS" = semicolon_paste(gdal,)) ) ), storage = storage, diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index 5cc31c0..ed9e1c4 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -106,7 +106,7 @@ tar_terra_vect <- function(name, 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 = ";")) + "GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS" = semicolon_paste(gdal)) ) ), storage = storage, @@ -129,7 +129,7 @@ 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 = semicolon_split(Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", unset = ";")) ) }, marshal = function(object) terra::wrap(object), @@ -151,7 +151,7 @@ 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 = semicolon_split(Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", unset = ";")) ) 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 = ";") +} + From 2501716635657b70aa00a7044b2c3b4d3b968746 Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Mon, 29 Apr 2024 16:39:50 -0700 Subject: [PATCH 4/8] use env vars instead of eval(substitute( --- R/tar-terra-rast.R | 2 +- R/tar-terra-sprc.R | 14 +++++--------- tests/testthat/test-tar-terra-sprc.R | 4 +--- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index 30fd26f..51953fe 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -104,7 +104,7 @@ tar_terra_rast <- function(name, 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" = semicolon_paste(gdal,)) + "GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS" = semicolon_paste(gdal)) ) ), storage = storage, diff --git a/R/tar-terra-sprc.R b/R/tar-terra-sprc.R index ba6cf3b..969dfbf 100644 --- a/R/tar-terra-sprc.R +++ b/R/tar-terra-sprc.R @@ -86,8 +86,7 @@ tar_terra_sprc <- function(name, tidy_eval = tidy_eval ) - .write_terra_rasters_sprc <- eval( - substitute( + .write_terra_rasters_sprc <- function(object, path) { for (i in seq(object)) { if (i > 1) { @@ -98,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 + gdal = semicolon_paste(c(Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS"), opt)) ) } - }, - list(filetype = filetype, gdal = gdal) - ) - ) + } targets::tar_target_raw( name = name, @@ -131,7 +127,7 @@ tar_terra_sprc <- function(name, 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) + "GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS" = semicolon_paste(gdal)) ) ), storage = storage, 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) }) From 6e7e3103f8c30f78b394703a9c9bcf778ec85296 Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Mon, 29 Apr 2024 16:40:05 -0700 Subject: [PATCH 5/8] reminder fix later --- R/geotargets-option.R | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) 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") ) } From 426b09ad0cb787fe79d89f53a803455dd97d2d1e Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Mon, 29 Apr 2024 16:57:19 -0700 Subject: [PATCH 6/8] had to add geotargets::: (not sure why, and this is not ideal) --- R/tar-terra-rast.R | 4 ++-- R/tar-terra-sprc.R | 4 ++-- R/tar-terra-vect.R | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index 51953fe..d0ec56a 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -87,7 +87,7 @@ tar_terra_rast <- function(name, path, filetype = Sys.getenv("GEOTARGETS_GDAL_RASTER_DRIVER"), overwrite = TRUE, - gdal = semicolon_split(Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS", unset = ";")) + gdal = geotargets:::semicolon_split(Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS", unset = ";")) ) }, marshal = function(object) terra::wrap(object), @@ -104,7 +104,7 @@ tar_terra_rast <- function(name, 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" = semicolon_paste(gdal)) + "GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS" = geotargets:::semicolon_paste(gdal)) ) ), storage = storage, diff --git a/R/tar-terra-sprc.R b/R/tar-terra-sprc.R index 969dfbf..97f8af8 100644 --- a/R/tar-terra-sprc.R +++ b/R/tar-terra-sprc.R @@ -99,7 +99,7 @@ tar_terra_sprc <- function(name, filename = path, filetype = Sys.getenv("GEOTARGETS_GDAL_RASTER_DRIVER"), overwrite = (i == 1), - gdal = semicolon_paste(c(Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS"), opt)) + gdal = geotargets:::semicolon_paste(c(Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS"), opt)) ) } } @@ -127,7 +127,7 @@ tar_terra_sprc <- function(name, 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" = semicolon_paste(gdal)) + "GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS" = geotargets:::semicolon_paste(gdal)) ) ), storage = storage, diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index ed9e1c4..da8f181 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -106,7 +106,7 @@ tar_terra_vect <- function(name, 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" = semicolon_paste(gdal)) + "GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS" = geotargets:::semicolon_paste(gdal)) ) ), storage = storage, @@ -129,7 +129,7 @@ create_format_terra_vect <- function() { path, filetype = Sys.getenv("GEOTARGETS_GDAL_VECTOR_DRIVER"), overwrite = TRUE, - options = semicolon_split(Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", unset = ";")) + options = geotargets:::semicolon_split(Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", unset = ";")) ) }, marshal = function(object) terra::wrap(object), @@ -151,7 +151,7 @@ create_format_terra_vect_shz <- function() { filename = paste0(path, ".shz"), filetype = "ESRI Shapefile", overwrite = TRUE, - options = semicolon_split(Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", unset = ";")) + options = geotargets:::semicolon_split(Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", unset = ";")) ) file.rename(paste0(path, ".shz"), path) }, From 05304633ae559255f2c53f2028a67c8cc7e288d3 Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Mon, 29 Apr 2024 17:16:12 -0700 Subject: [PATCH 7/8] revert change appending user options to APPEND_SUBDATASET=YES --- R/tar-terra-sprc.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/tar-terra-sprc.R b/R/tar-terra-sprc.R index 97f8af8..8595213 100644 --- a/R/tar-terra-sprc.R +++ b/R/tar-terra-sprc.R @@ -99,7 +99,7 @@ tar_terra_sprc <- function(name, filename = path, filetype = Sys.getenv("GEOTARGETS_GDAL_RASTER_DRIVER"), overwrite = (i == 1), - gdal = geotargets:::semicolon_paste(c(Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS"), opt)) + gdal = opt ) } } From 7be1613c77918e0bf98689e1b894e423472f5c89 Mon Sep 17 00:00:00 2001 From: njtierney Date: Fri, 3 May 2024 13:09:20 +1000 Subject: [PATCH 8/8] revert back to useing strsplit and paste0 --- R/tar-terra-rast.R | 13 ++++++++++--- R/tar-terra-sprc.R | 8 ++++++-- R/tar-terra-vect.R | 18 ++++++++++++++---- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index d0ec56a..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 = geotargets:::semicolon_split(Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS", unset = ";")) + 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" = geotargets:::semicolon_paste(gdal)) + 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 8595213..c5068b5 100644 --- a/R/tar-terra-sprc.R +++ b/R/tar-terra-sprc.R @@ -126,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" = geotargets:::semicolon_paste(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 da8f181..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" = geotargets:::semicolon_paste(gdal)) + 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 = geotargets:::semicolon_split(Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", unset = ";")) + 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 = geotargets:::semicolon_split(Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", unset = ";")) + options = strsplit( + Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", + unset = ";"), + ";")[[1]] ) file.rename(paste0(path, ".shz"), path) },