Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes after options, terra, and tar_terra_sprc() updates #60

Merged
merged 6 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/geotargets-option.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ geotargets_option_get <- function(name) {
opt <- getOption(option_name, default = Sys.getenv(env_name))

#replace empty string from Sys.getenv default with NULL
if (opt == "") {
if (length(opt) == 1 && opt == "") {
opt <- NULL
}
#return
Expand Down
6 changes: 3 additions & 3 deletions R/tar-terra-rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ tar_terra_rast <- function(name,
retrieval = targets::tar_option_get("retrieval"),
cue = targets::tar_option_get("cue")) {
filetype <- filetype %||% "GTiff"
gdal <- gdal %||% "ENCODING=UTF-8"
gdal <- gdal %||% character(0)

#check that filetype option is available
drv <- get_gdal_available_driver_list("raster")
Expand Down Expand Up @@ -87,7 +87,7 @@ tar_terra_rast <- function(name,
path,
filetype = Sys.getenv("GEOTARGETS_GDAL_RASTER_DRIVER"),
overwrite = TRUE,
gdal = Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS")
gdal = strsplit(Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS", unset = ";"), ";")[[1]]
)
},
marshal = function(object) terra::wrap(object),
Expand All @@ -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" = gdal)
"GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS" = paste0(gdal, collapse = ";"))
)
),
storage = storage,
Expand Down
4 changes: 2 additions & 2 deletions R/tar-terra-sprc.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ tar_terra_sprc <- function(name,
retrieval = targets::tar_option_get("retrieval"),
cue = targets::tar_option_get("cue")) {
filetype <- filetype %||% "GTiff"
gdal <- gdal %||% "ENCODING=UTF-8"
gdal <- gdal %||% character(0)

# check that filetype option is available
drv <- get_gdal_available_driver_list("raster")
Expand Down Expand Up @@ -92,7 +92,7 @@ tar_terra_sprc <- function(name,
filetype <- filetype %||% geotargets_option_get("gdal.raster.driver")
filetype <- rlang::arg_match0(filetype, drv$name)

gdal <- gdal %||% geotargets_option_get("gdal.raster.creation_options")
gdal <- gdal %||% geotargets_option_get("gdal.raster.creation.options")

.write_terra_rasters_sprc <- eval(
substitute(
Expand Down
6 changes: 3 additions & 3 deletions R/tar-terra-vect.R
Original file line number Diff line number Diff line change
Expand Up @@ -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" = gdal)
"GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS" = paste0(gdal, collapse = ";"))
)
),
storage = storage,
Expand All @@ -129,7 +129,7 @@ create_format_terra_vect <- function() {
path,
filetype = Sys.getenv("GEOTARGETS_GDAL_VECTOR_DRIVER"),
overwrite = TRUE,
options = Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS")
options = strsplit(Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", unset = ";"), ";")[[1]]
)
},
marshal = function(object) terra::wrap(object),
Expand All @@ -151,7 +151,7 @@ create_format_terra_vect_shz <- function() {
filename = paste0(path, ".shz"),
filetype = "ESRI Shapefile",
overwrite = TRUE,
options = Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS")
options = strsplit(Sys.getenv("GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", unset = ";"), ";")[[1]]
)
file.rename(paste0(path, ".shz"), path)
},
Expand Down
8 changes: 6 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ check_pkg_installed <- function(pkg, call = rlang::caller_env()) {
}
}

get_gdal_available_driver_list <- function(driver_type){
get_gdal_available_driver_list <- function(driver_type) {
# get list of drivers available for writing depending on what the user's GDAL supports
drv <- terra::gdal(drivers = TRUE)
drv <- drv[drv$type == driver_type & grepl("write", drv$can), ]
if (utils::packageVersion("terra") > "1.7-74") {
drv <- drv[drv[[driver_type]] & grepl("write", drv$can), ]
} else {
drv <- drv[drv$type == driver_type & grepl("write", drv$can), ]
}
drv
}
14 changes: 14 additions & 0 deletions tests/testthat/_snaps/tar-terra-sprc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# tar_terra_sprc() works

Code
x
Output
class : SpatRasterCollection
length : 2
nrow : 90, 115
ncol : 95, 114
nlyr : 1, 1
extent : 5.741667, 1558890, 49.44167, 5556741 (xmin, xmax, ymin, ymax)
crs (first) : lon/lat WGS 84 (EPSG:4326)
names : raster_elevs, raster_elevs

2 changes: 1 addition & 1 deletion tests/testthat/test-tar-terra-sprc.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
targets::tar_test("tar_terra_sprc() works", {
geotargets::geotargets_option_set(
"raster_gdal_creation_options",
gdal_raster_creation_options =
c("COMPRESS=DEFLATE", "TFW=YES")
)
targets::tar_script({
Expand Down