-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from njtierney/options-makeover
Options makeover
- Loading branch information
Showing
15 changed files
with
289 additions
and
314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.