Skip to content

Commit

Permalink
Merge pull request #67 from njtierney/resources-fix
Browse files Browse the repository at this point in the history
Pass user specified `resources`
  • Loading branch information
Aariq authored May 13, 2024
2 parents 31d7c6d + 51ca40b commit 4af63c5
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 27 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
* Created `tar_terra_sprc()` that creates a `SpatRasterCollection` object.
* `geotargets_options_get()` and `geotargets_options_set()` can be used to set and get options specific to `geotargets`.
* `geotargets` now requires `targets` version 1.7.0 or higher
* fixed a bug where `resources` supplied to `tar_terra_*()` were being ignored (#66)
24 changes: 15 additions & 9 deletions R/tar-terra-rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ tar_terra_rast <- function(name,

check_pkg_installed("terra")

#ensure that user-passed `resources` doesn't include `custom_format`
if ("custom_format" %in% names(resources)) {
cli::cli_abort("{.val custom_format} cannot be supplied to targets created with {.fn tar_terra_rast}")
}

name <- targets::tar_deparse_language(substitute(name))

envir <- targets::tar_option_get("envir")
Expand Down Expand Up @@ -103,17 +108,18 @@ tar_terra_rast <- function(name,
garbage_collection = garbage_collection,
deployment = deployment,
priority = priority,
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 = ";")
resources = utils::modifyList(
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 = ";")
)
)
)
),
)
), resources),
storage = storage,
retrieval = retrieval,
cue = cue
Expand Down
23 changes: 14 additions & 9 deletions R/tar-terra-sprc.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ tar_terra_sprc <- function(name,
retrieval = targets::tar_option_get("retrieval"),
cue = targets::tar_option_get("cue")) {
check_pkg_installed("terra")
#ensure that user-passed `resources` doesn't include `custom_format`
if ("custom_format" %in% names(resources)) {
cli::cli_abort("{.val custom_format} cannot be supplied to targets created with {.fn tar_terra_sprc}")
}

gdal <- gdal %||% character(0)
filetype <- filetype %||% "GTiff"
Expand Down Expand Up @@ -123,17 +127,18 @@ tar_terra_sprc <- function(name,
garbage_collection = garbage_collection,
deployment = deployment,
priority = priority,
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 = ";")
resources = utils::modifyList(
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 = ";")
)
)
)
)
),
), resources),
storage = storage,
retrieval = retrieval,
cue = cue
Expand Down
24 changes: 15 additions & 9 deletions R/tar-terra-vect.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ tar_terra_vect <- function(name,

check_pkg_installed("terra")

#ensure that user-passed `resources` doesn't include `custom_format`
if ("custom_format" %in% names(resources)) {
cli::cli_abort("{.val custom_format} cannot be supplied to targets created with {.fn tar_terra_vect}")
}

name <- targets::tar_deparse_language(substitute(name))

envir <- targets::tar_option_get("envir")
Expand Down Expand Up @@ -102,17 +107,18 @@ tar_terra_vect <- function(name,
garbage_collection = garbage_collection,
deployment = deployment,
priority = priority,
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 = ";")
resources = utils::modifyList(
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 = ";")
)
)
)
)
),
), resources),
storage = storage,
retrieval = retrieval,
cue = cue
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-geotargets-option.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@ targets::tar_test("geotargets_options_get() retrieves options in correct priorit
)
})


test_that("geotargets_option_set() works", {
op <- getOption("geotargets.gdal.raster.driver")
withr::defer(options("geotargets.gdal.raster.driver" = op))

geotargets_option_set(gdal_raster_driver = "COG")
expect_equal(getOption("geotargets.gdal.raster.driver"), "COG")
expect_equal(geotargets_option_get("gdal.raster.driver"), "COG")
expect_equal(geotargets_option_get("gdal_raster_driver"), "COG")
})

test_that("options aren't reset with multiple calls to geotargets_option_set()", {
op_rast <- getOption("geotargets.gdal.raster.driver")
withr::defer(options("geotargets.gdal.raster.driver" = op_rast))
op_vect <- getOption("geotargets.gdal.vector.driver")
withr::defer(options("geotargets.gdal.vector.driver" = op_vect))

geotargets_option_set(gdal_raster_driver = "GPKG")
geotargets_option_set(gdal_vector_driver = "GPKG")
expect_equal(geotargets_option_get("gdal_vector_driver"), "GPKG")
Expand Down
37 changes: 37 additions & 0 deletions tests/testthat/test-tar-terra.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# test_that() #Included to make RStudio recognize this file as a test
library(targets)
targets::tar_test("tar_terra_rast() works", {
# geotargets::geotargets_option_set(gdal_raster_creation_options = c("COMPRESS=DEFLATE", "TFW=YES"))
targets::tar_script({
Expand Down Expand Up @@ -87,3 +88,39 @@ targets::tar_test("tar_terra_vect() works with multiple workers (tests marshalin
expect_s4_class(targets::tar_read(vect1), "SpatVector")
})

targets::tar_test("user resources are passed correctly", {
library(crew)
persistent <- crew::crew_controller_local(name = "persistent")
transient <- crew::crew_controller_local(name = "transient", tasks_max = 1L)
targets::tar_option_set(
controller = crew::crew_controller_group(persistent, transient),
resources = tar_resources(
crew = tar_resources_crew(controller = "transient")
))
testthat::expect_equal(
tar_terra_rast(x, 1)$settings$resources$crew,
tar_resources_crew(controller = "transient")
)
testthat::expect_equal(
tar_terra_rast(
x, 1,
resources = tar_resources(crew = tar_resources_crew(controller = "persistent"))
)$settings$resources$crew,
tar_resources_crew(controller = "persistent")
)
testthat::expect_equal(
tar_terra_vect(
x, 1,
resources = tar_resources(crew = tar_resources_crew(controller = "persistent"))
)$settings$resources$crew,
tar_resources_crew(controller = "persistent")
)
testthat::expect_equal(
tar_terra_sprc(
x, 1,
resources = tar_resources(crew = tar_resources_crew(controller = "persistent"))
)$settings$resources$crew,
tar_resources_crew(controller = "persistent")
)
})

0 comments on commit 4af63c5

Please sign in to comment.