Skip to content

Commit

Permalink
Merge pull request #45 from njtierney/options-makeover
Browse files Browse the repository at this point in the history
Options makeover
  • Loading branch information
Aariq authored Apr 23, 2024
2 parents 7853db1 + e7cd721 commit 131e3fb
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 314 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ Imports:
cli
Suggests:
terra,
testthat (>= 3.0.0)
testthat (>= 3.0.0),
withr
Config/testthat/edition: 3
URL: https://github.com/njtierney/geotargets, https://njtierney.github.io/geotargets/
BugReports: https://github.com/njtierney/geotargets/issues
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# geotargets 0.0.0.9000 (development version)

* Created `tar_terra_rast()` and `tar_terra_vect()` for targets that create `SpatRaster` and `SpatVector` objects, respectively
* 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
13 changes: 0 additions & 13 deletions R/AAAA.R

This file was deleted.

130 changes: 57 additions & 73 deletions R/geotargets-option.R
Original file line number Diff line number Diff line change
@@ -1,89 +1,73 @@
#' Get or Set geotargets Options
#'
#' Get or set behavior for geospatial data target stores using geotargets-specific global options.
#' Get or set behavior for geospatial data target stores using
#' geotargets-specific global options.
#'
#' @param option_name Character. Option name. See Details.
#' @param gdal_raster_driver character, length 1; set the driver used for raster
#' data in target store (default: `"GTiff"`). Options for driver names can be
#' found here: <https://gdal.org/drivers/raster/index.html>
#' @param gdal_raster_creation_options character; set the GDAL creation options
#' used when writing raster files to target store (default: `""`). You may
#' specify multiple values e.g. `c("COMPRESS=DEFLATE", "TFW=YES")`. Each GDAL
#' driver supports a unique set of creation options. For example, with the
#' default `"GTiff"` driver:
#' <https://gdal.org/drivers/raster/gtiff.html#creation-options>
#' @param gdal_vector_driver character, length 1; set the file type used for
#' vector data in target store (default: `"GeoJSON"`).
#' @param gdal_vector_creation_options character; set the GDAL layer creation
#' options used when writing vector files to target store (default:
#' `"ENCODING=UTF-8"`). You may specify multiple values e.g.
#' `c("WRITE_BBOX=YES", "COORDINATE_PRECISION=10")`. Each GDAL driver supports
#' a unique set of creation options. For example, with the default `"GeoJSON"`
#' driver:
#' <https://gdal.org/drivers/vector/geojson.html#layer-creation-options>
#'
#' @details
#' These options can also be set using `options()`. For example,
#' `geotargets_options_set(gdal_raster_driver = "GTiff")` is equivalent to
#' `options("geotargets.gdal.raster.driver" = "GTiff")`.
#'
#' ## Available Options
#'
#' - `"geotargets.gdal.raster.driver"` - character. Length 1. Set the driver used for raster data in target store (default: `"GTiff"`). Options for driver names can be found here: <https://gdal.org/drivers/raster/index.html>
#'
#' - `"geotargets.gdal.raster.creation_options"` - character. Set the GDAL creation options used when writing raster files to target store (default: `""`). You may specify multiple values e.g. `c("COMPRESS=DEFLATE", "TFW=YES")`. Each GDAL driver supports a unique set of creation options. For example, with the default `"GTiff"` driver: <https://gdal.org/drivers/raster/gtiff.html#creation-options>
#'
#' - `"geotargets.gdal.vector.driver"` - character. Length 1. Set the file type used for vector data in target store (default: `"GeoJSON"`).
#'
#' - `"geotargets.gdal.vector.creation_options"` - character. Set the GDAL layer creation options used when writing vector files to target store (default: `"ENCODING=UTF-8"`). You may specify multiple values e.g. `c("WRITE_BBOX=YES", "COORDINATE_PRECISION=10")`. Each GDAL driver supports a unique set of creation options. For example, with the default `"GeoJSON"` driver: <https://gdal.org/drivers/vector/geojson.html#layer-creation-options>
#'
#' Each option can be overridden with a system environment variable. Options include:
#'
#' - `GEOTARGETS_GDAL_RASTER_DRIVER`
#' - `GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS`
#' - `GEOTARGETS_GDAL_VECTOR_DRIVER`
#' - `GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS`
#'
#' When specifying options that support multiple values using a system environment variable, the separate options should be delimited with a semicolon (";"). For example: `"COMPRESS=DEFLATE;TFW=YES"`.
#'
#' @rdname geotargets-options
#' @export
geotargets_option_get <- function(option_name) {

option_name <- geotargets_repair_option_name(option_name)
option_value <- geotargets_env()[[option_name]]

get_option <- function(option_name, option_value, name){
getOption(option_name, default = option_value %||% name)
}

get_geotargets_gdal_raster_creation_options <- function(option_name, option_value) {
gdal_creation_options <- Sys.getenv(
x = "GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS",
unset = get_option(option_name, option_value, ";")
)
the_option <- strsplit(gdal_creation_options, ";")[[1]]
the_option
}
geotargets_option_set <- function(
gdal_raster_driver = NULL,
gdal_raster_creation_options = NULL,
gdal_vector_driver = NULL,
gdal_vector_creation_options = NULL
) {

get_geotargets_gdal_raster_driver <- function(option_name, option_value) {
Sys.getenv(
x = "GEOTARGETS_GDAL_RASTER_DRIVER",
unset = get_option(option_name, option_value, "GTiff")
)
}

get_geotargets_gdal_vector_creation_options <- function(option_name, option_value) {
gdal_creation_options <- Sys.getenv(
x = "GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS",
unset = get_option(option_name, option_value, "ENCODING=UTF-8")
)
the_options <- strsplit(gdal_creation_options, ";")[[1]]
the_options
}

get_geotargets_gdal_vector_driver <- function(option_name, option_value) {
Sys.getenv(
x = "GEOTARGETS_GDAL_VECTOR_DRIVER",
unset = get_option(option_name, option_value, "GeoJSON")
)
}

switch(option_name,
"geotargets.gdal.raster.creation_options" =
get_geotargets_gdal_raster_creation_options(option_name, option_value),
"geotargets.gdal.raster.driver" =
get_geotargets_gdal_raster_driver(option_name, option_value),
"geotargets.gdal.vector.creation_options" =
get_geotargets_gdal_vector_creation_options(option_name, option_value),
"geotargets.gdal.vector.driver" =
get_geotargets_gdal_vector_driver(option_name, option_value)
options(
"geotargets.gdal.raster.driver" = gdal_raster_driver,
"geotargets.gdal.raster.creation.options" = gdal_raster_creation_options,
"geotargets.gdal.vector.driver" = gdal_raster_creation_options,
"geotargets.gdal.vector.creation.options" = gdal_raster_creation_options
)

}

#' @param option_value Value to assign to option `x`.
#' @param name character; option name to get.
#'
#' @rdname geotargets-options
#' @export
geotargets_option_set <- function(option_name, option_value) {
option_name <- geotargets_repair_option_name(option_name)
geotargets.env[[option_name]] <- option_value
geotargets_option_get <- function(name) {
option_name <- geotargets_repair_option_name(name)
# check if `name` is one of the possible options
option_name <-
rlang::arg_match0(option_name, c(
"geotargets.gdal.raster.driver",
"geotargets.gdal.raster.creation.options",
"geotargets.gdal.vector.driver",
"geotargets.gdal.vector.creation.options"
))

env_name <- gsub("\\.", "_", toupper(option_name))
opt <- getOption(option_name, default = Sys.getenv(env_name))

#replace empty string from Sys.getenv default with NULL
if (opt == "") {
opt <- NULL
}
#return
opt
}
75 changes: 29 additions & 46 deletions R/tar-terra-rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
tar_terra_rast <- function(name,
command,
pattern = NULL,
filetype = NULL,
gdal = NULL,
filetype = geotargets_option_get("gdal.raster.driver"),
gdal = geotargets_option_get("gdal.raster.creation.options"),
...,
tidy_eval = targets::tar_option_get("tidy_eval"),
packages = targets::tar_option_get("packages"),
Expand All @@ -48,6 +48,12 @@ tar_terra_rast <- function(name,
storage = targets::tar_option_get("storage"),
retrieval = targets::tar_option_get("retrieval"),
cue = targets::tar_option_get("cue")) {
filetype <- filetype %||% "GTiff"
gdal <- gdal %||% "ENCODING=UTF-8"

#check that filetype option is available
drv <- get_gdal_available_driver_list("raster")
filetype <- rlang::arg_match0(filetype, drv$name)

check_pkg_installed("terra")

Expand All @@ -67,65 +73,42 @@ tar_terra_rast <- 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")

targets::tar_target_raw(
name = name,
command = command,
pattern = pattern,
packages = packages,
library = library,
format = create_format_terra_raster(filetype = filetype, gdal = gdal, ...),
format = targets::tar_format(
read = function(path) terra::rast(path),
write = function(object, path) {
terra::writeRaster(
object,
path,
filetype = Sys.getenv("GEOTARGETS_GDAL_RASTER_DRIVER"),
overwrite = TRUE,
gdal = Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS")
)
},
marshal = function(object) terra::wrap(object),
unmarshal = function(object) terra::unwrap(object)
),
repository = repository,
iteration = iteration,
error = error,
memory = memory,
garbage_collection = garbage_collection,
deployment = deployment,
priority = priority,
resources = resources,
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)
)
),
storage = storage,
retrieval = retrieval,
cue = cue
)
}

#' @param filetype File format expressed as GDAL driver names passed to `terra::writeRaster()`
#' @param gdal GDAL driver specific datasource creation options passed to `terra::writeRaster()`
#' @param ... Additional arguments not yet used
#' @noRd
create_format_terra_raster <- function(filetype, gdal, ...) {

check_pkg_installed("terra")

drv <- get_gdal_available_driver_list("raster")

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_raster <- eval(substitute(function(object, path) {
terra::writeRaster(
object,
path,
filetype = filetype,
overwrite = TRUE,
gdal = gdal
)
}, list(filetype = filetype, gdal = gdal)))

targets::tar_format(
read = function(path) terra::rast(path),
write = .write_terra_raster,
marshal = function(object) terra::wrap(object),
unmarshal = function(object) terra::unwrap(object)
)
}

Loading

0 comments on commit 131e3fb

Please sign in to comment.