Skip to content

Commit

Permalink
Merge branch 'master' into tar-stars
Browse files Browse the repository at this point in the history
  • Loading branch information
brownag authored May 11, 2024
2 parents 946e06b + 31d7c6d commit 1ad5242
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 34 deletions.
13 changes: 9 additions & 4 deletions R/geotargets-option.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ geotargets_option_set <- function(
gdal_vector_driver = NULL,
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")
)

}
Expand Down
13 changes: 10 additions & 3 deletions R/tar-terra-rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,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),
Expand All @@ -102,8 +105,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,
Expand Down
32 changes: 12 additions & 20 deletions R/tar-terra-sprc.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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) {
Expand All @@ -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,
Expand All @@ -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,
Expand Down
18 changes: 14 additions & 4 deletions R/tar-terra-vect.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand All @@ -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)
},
Expand Down
9 changes: 9 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ";")
}

4 changes: 1 addition & 3 deletions tests/testthat/test-tar-terra-sprc.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit 1ad5242

Please sign in to comment.