From ceddcf6d4e8454c8a580447f0ea3402bdaa503fc Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Tue, 12 Mar 2024 21:14:27 -0700 Subject: [PATCH 01/20] Add `geotargets_get_option()` and `geotargets_set_option()` --- NAMESPACE | 2 ++ R/AAAA.R | 10 +++++++ R/geotargets-option.R | 50 +++++++++++++++++++++++++++++++++ man/geotargets-options.Rd | 33 ++++++++++++++++++++++ tests/testthat/test-tar-terra.R | 1 + 5 files changed, 96 insertions(+) create mode 100644 R/AAAA.R create mode 100644 R/geotargets-option.R create mode 100644 man/geotargets-options.Rd diff --git a/NAMESPACE b/NAMESPACE index 822d790..c3b87e1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,7 @@ # Generated by roxygen2: do not edit by hand +export(geotargets_option_get) +export(geotargets_option_set) export(tar_terra_rast) export(tar_terra_vect) importFrom(utils,globalVariables) diff --git a/R/AAAA.R b/R/AAAA.R new file mode 100644 index 0000000..07cd632 --- /dev/null +++ b/R/AAAA.R @@ -0,0 +1,10 @@ +geotargets.env <- new.env() + +geotargets_env <- function() { + geotargets.env +} + +.onAttach <- function(lib, pkg) { + geotargets.env$geotargets.raster.gdal_creation_options <- "ENCODING=UTF-8" + geotargets.env$geotargets.raster.gdal_driver_name <- "GTiff" +} diff --git a/R/geotargets-option.R b/R/geotargets-option.R new file mode 100644 index 0000000..fc5be77 --- /dev/null +++ b/R/geotargets-option.R @@ -0,0 +1,50 @@ +#' Get or Set geotargets Options +#' +#' Get or set behavior for geospatial data target stores using geotargets-specific global options. +#' +#' @param x Character. Option name. See Details. +#' +#' @details +#' +#' ## Available Options +#' +#' - `"geotargets.raster.gdal_creation_options"` - set the GDAL creation options used when writing raster files to target store (default: `"ENCODING=UTF-8"`) +#' +#' - `"geotargets.raster.gdal_driver_name"` - set the file type used for raster data in target store (default: `"GTiff"`) +#' +#' Each option can be overridden with a system environment variable. Options include: +#' +#' - `GEOTARGETS_RASTER_GDAL_CREATION_OPTIONS` +#' - `GEOTARGETS_RASTER_GDAL_DRIVER_NAME` +#' +#' @rdname geotargets-options +#' @export +geotargets_option_get <- function(x) { + if (!startsWith(x, "geotargets.")) { + x <- paste0("geotargets.", x) + } + + value <- geotargets_env()[[x]] + + switch(x, + "geotargets.raster.gdal_creation_options" = { + strsplit(Sys.getenv("GEOTARGETS_RASTER_GDAL_CREATION_OPTIONS", + unset = getOption(x, default = ifelse(is.null(value), "ENCODING=UTF-8", value))), + ";")[[1]] + }, + "geotargets.raster.gdal_driver_name" = { + Sys.getenv("GEOTARGETS_RASTER_GDAL_DRIVER_NAME", + unset = getOption(x, default = ifelse(is.null(value), "GTiff", value)) + ) + }) +} + +#' @param value Value to assign to option `x`. +#' @rdname geotargets-options +#' @export +geotargets_option_set <- function(x, value) { + if (!startsWith(x, "geotargets.")) { + x <- paste0("geotargets.", x) + } + geotargets.env[[x]] <- value +} diff --git a/man/geotargets-options.Rd b/man/geotargets-options.Rd new file mode 100644 index 0000000..270140f --- /dev/null +++ b/man/geotargets-options.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/geotargets-option.R +\name{geotargets_option_get} +\alias{geotargets_option_get} +\alias{geotargets_option_set} +\title{Get or Set geotargets Options} +\usage{ +geotargets_option_get(x) + +geotargets_option_set(x, value) +} +\arguments{ +\item{x}{Character. Option name. See Details.} + +\item{value}{Value to assign to option \code{x}.} +} +\description{ +Get or set behavior for geospatial data target stores using geotargets-specific global options. +} +\details{ +\subsection{Available Options}{ +\itemize{ +\item \code{"geotargets.raster.gdal_creation_options"} - set the GDAL creation options used when writing raster files to target store (default: \code{"ENCODING=UTF-8"}) +\item \code{"geotargets.raster.gdal_driver_name"} - set the file type used for raster data in target store (default: \code{"GTiff"}) +} + +Each option can be overridden with a system environment variable. Options include: +\itemize{ +\item \code{GEOTARGETS_RASTER_GDAL_CREATION_OPTIONS} +\item \code{GEOTARGETS_RASTER_GDAL_DRIVER_NAME} +} +} +} diff --git a/tests/testthat/test-tar-terra.R b/tests/testthat/test-tar-terra.R index 45898e2..5c9d587 100644 --- a/tests/testthat/test-tar-terra.R +++ b/tests/testthat/test-tar-terra.R @@ -1,5 +1,6 @@ # test_that() #Included to make RStudio recognize this file as a test targets::tar_test("tar_terra_rast() works", { + geotargets::geotargets_option_set("raster_gdal_creation_options", c("COMPRESS=DEFLATE", "TFW=YES")) targets::tar_script({ list( geotargets::tar_terra_rast( From 0641219fe6c2c539f4bf439134964cc7b592012a Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Tue, 12 Mar 2024 21:15:14 -0700 Subject: [PATCH 02/20] create_format_terra_raster: use `geotargets_option_get()` for `filetype` and `gdal` --- R/tar-terra-rast.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index c42704a..59d8c32 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -117,9 +117,9 @@ create_format_terra_raster <- function(filetype, gdal, ...) { terra::writeRaster( object, path, - filetype = NULL, + filetype = geotargets::geotargets_option_get("raster.gdal_driver_name"), overwrite = TRUE, - gdal = NULL + gdal = geotargets::geotargets_option_get("raster.gdal_creation_options") ) } body(.write_terra_raster)[[2]][["filetype"]] <- filetype From 5b71a2afaa096dbcbb06481d6ab498734b9bba50 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Tue, 12 Mar 2024 22:24:10 -0700 Subject: [PATCH 03/20] Apply option for null conditions --- R/tar-terra-rast.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index 59d8c32..3b7775f 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -65,11 +65,11 @@ tar_terra_rast <- function(name, # could pull defaults from geotargets package options if (is.null(filetype)) { - filetype <- "GTiff" + filetype <- geotargets::geotargets_option_get("raster.gdal_driver_name") } if (is.null(gdal)) { - gdal <- "ENCODING=UTF-8" + gdal <- geotargets::geotargets_option_get("raster.gdal_creation_options") } targets::tar_target_raw( @@ -108,7 +108,7 @@ create_format_terra_raster <- function(filetype, gdal, ...) { drv <- drv[drv$type == "raster" & grepl("write", drv$can), ] if (is.null(filetype)) { - filetype <- "GTiff" + filetype <- geotargets::geotargets_option_get("raster.gdal_driver_name") } filetype <- match.arg(filetype, drv$name) From 353b69974bdf3f8e12aa23d778f098b6f1805d81 Mon Sep 17 00:00:00 2001 From: njtierney Date: Wed, 13 Mar 2024 13:45:39 +1100 Subject: [PATCH 04/20] fix code coverage using instructions from codecov --- .github/workflows/test-coverage.yaml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index 21b8a93..bb763cc 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -42,9 +42,8 @@ jobs: find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true shell: bash - - name: Upload test results - if: failure() - uses: actions/upload-artifact@v4 + - name: Upload coverage reports to Codecov + uses: codecov/codecov-action@v4.0.1 with: - name: coverage-test-failures - path: ${{ runner.temp }}/package + token: ${{ secrets.CODECOV_TOKEN }} + slug: njtierney/geotargets From d1dbb36d4b0e6634a5ec54011727c1cbd1fb2354 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Wed, 13 Mar 2024 07:31:24 -0700 Subject: [PATCH 05/20] Import rlang for #20 - use `%||%` and `arg_match0() `in `tar_terra_rast()` --- DESCRIPTION | 1 + NAMESPACE | 2 ++ R/tar-terra-rast.R | 28 +++++++++++++--------------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ab2f53d..93fa945 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -32,6 +32,7 @@ Language: en-GB Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.1 Imports: + rlang, targets, terra Suggests: diff --git a/NAMESPACE b/NAMESPACE index c3b87e1..4532f2e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,4 +4,6 @@ export(geotargets_option_get) export(geotargets_option_set) export(tar_terra_rast) export(tar_terra_vect) +importFrom(rlang,"%||%") +importFrom(rlang,arg_match0) importFrom(utils,globalVariables) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index 3b7775f..7a8adc7 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -7,7 +7,7 @@ #' @param ... Additional arguments not yet used #' #' @inheritParams targets::tar_target -#' +#' @importFrom rlang %||% arg_match0 #' @seealso [targets::tar_target_raw()] #' @export #' @examples @@ -63,14 +63,9 @@ tar_terra_rast <- function(name, tidy_eval = tidy_eval ) - # could pull defaults from geotargets package options - if (is.null(filetype)) { - filetype <- geotargets::geotargets_option_get("raster.gdal_driver_name") - } - - if (is.null(gdal)) { - gdal <- geotargets::geotargets_option_get("raster.gdal_creation_options") - } + # if not specified by user, pull the corresponding geotargets option + filetype <- filetype %||% geotargets_option_get("raster.gdal_driver_name") + gdal <- gdal %||% geotargets_option_get("raster.gdal_creation_options") targets::tar_target_raw( name = name, @@ -107,21 +102,24 @@ create_format_terra_raster <- function(filetype, gdal, ...) { drv <- terra::gdal(drivers = TRUE) drv <- drv[drv$type == "raster" & grepl("write", drv$can), ] - if (is.null(filetype)) { - filetype <- geotargets::geotargets_option_get("raster.gdal_driver_name") - } + filetype <- filetype %||% geotargets_option_get("raster.gdal_driver_name") + filetype <- rlang::arg_match0(filetype, drv$name) - filetype <- match.arg(filetype, drv$name) + gdal <- gdal %||% geotargets_option_get("raster.gdal_creation_options") + # NOTE: Option getting functions are set in the .write_terra_raster function template + # to resolve issue with body<- not working in some evaluation contexts ({covr}). + # TODO: It should be fine to have filetype and gdal as NULL .write_terra_raster <- function(object, path) { terra::writeRaster( object, path, - filetype = geotargets::geotargets_option_get("raster.gdal_driver_name"), + filetype = geotargets_option_get("raster.gdal_driver_name"), overwrite = TRUE, - gdal = geotargets::geotargets_option_get("raster.gdal_creation_options") + gdal = geotargets_option_get("raster.gdal_creation_options") ) } + body(.write_terra_raster)[[2]][["filetype"]] <- filetype body(.write_terra_raster)[[2]][["gdal"]] <- gdal From f634c301432549d20f9da928ba9771839828fe90 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Wed, 13 Mar 2024 07:39:02 -0700 Subject: [PATCH 06/20] geotargets:: is required inside tar_format() write function body - RE: https://github.com/njtierney/geotargets/pull/19#discussion_r1522577808 --- R/tar-terra-rast.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index 7a8adc7..d5d005b 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -114,9 +114,9 @@ create_format_terra_raster <- function(filetype, gdal, ...) { terra::writeRaster( object, path, - filetype = geotargets_option_get("raster.gdal_driver_name"), + filetype = geotargets::geotargets_option_get("raster.gdal_driver_name"), overwrite = TRUE, - gdal = geotargets_option_get("raster.gdal_creation_options") + gdal = geotargets::geotargets_option_get("raster.gdal_creation_options") ) } From 827de6afe3b80e24afd4f555ed6a7513a27df235 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Wed, 13 Mar 2024 07:39:43 -0700 Subject: [PATCH 07/20] geotargets_option_get/set: Update argument names --- R/geotargets-option.R | 26 +++++++++++++------------- man/geotargets-options.Rd | 8 ++++---- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/R/geotargets-option.R b/R/geotargets-option.R index fc5be77..696d5ec 100644 --- a/R/geotargets-option.R +++ b/R/geotargets-option.R @@ -2,7 +2,7 @@ #' #' Get or set behavior for geospatial data target stores using geotargets-specific global options. #' -#' @param x Character. Option name. See Details. +#' @param option_name Character. Option name. See Details. #' #' @details #' @@ -19,32 +19,32 @@ #' #' @rdname geotargets-options #' @export -geotargets_option_get <- function(x) { - if (!startsWith(x, "geotargets.")) { - x <- paste0("geotargets.", x) +geotargets_option_get <- function(option_name) { + if (!startsWith(option_name, "geotargets.")) { + option_name <- paste0("geotargets.", option_name) } - value <- geotargets_env()[[x]] + option_value <- geotargets_env()[[option_name]] - switch(x, + switch(option_name, "geotargets.raster.gdal_creation_options" = { strsplit(Sys.getenv("GEOTARGETS_RASTER_GDAL_CREATION_OPTIONS", - unset = getOption(x, default = ifelse(is.null(value), "ENCODING=UTF-8", value))), + unset = getOption(option_name, default = ifelse(is.null(option_value), "ENCODING=UTF-8", option_value))), ";")[[1]] }, "geotargets.raster.gdal_driver_name" = { Sys.getenv("GEOTARGETS_RASTER_GDAL_DRIVER_NAME", - unset = getOption(x, default = ifelse(is.null(value), "GTiff", value)) + unset = getOption(option_name, default = ifelse(is.null(option_value), "GTiff", option_value)) ) }) } -#' @param value Value to assign to option `x`. +#' @param option_value Value to assign to option `x`. #' @rdname geotargets-options #' @export -geotargets_option_set <- function(x, value) { - if (!startsWith(x, "geotargets.")) { - x <- paste0("geotargets.", x) +geotargets_option_set <- function(option_name, option_value) { + if (!startsWith(option_name, "geotargets.")) { + option_name <- paste0("geotargets.", option_name) } - geotargets.env[[x]] <- value + geotargets.env[[option_name]] <- option_value } diff --git a/man/geotargets-options.Rd b/man/geotargets-options.Rd index 270140f..b79777f 100644 --- a/man/geotargets-options.Rd +++ b/man/geotargets-options.Rd @@ -5,14 +5,14 @@ \alias{geotargets_option_set} \title{Get or Set geotargets Options} \usage{ -geotargets_option_get(x) +geotargets_option_get(option_name) -geotargets_option_set(x, value) +geotargets_option_set(option_name, option_value) } \arguments{ -\item{x}{Character. Option name. See Details.} +\item{option_name}{Character. Option name. See Details.} -\item{value}{Value to assign to option \code{x}.} +\item{option_value}{Value to assign to option \code{x}.} } \description{ Get or set behavior for geospatial data target stores using geotargets-specific global options. From 884513b3fb9d87fb8d40bc1b230fb4b772c7cbdd Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Wed, 13 Mar 2024 07:42:57 -0700 Subject: [PATCH 08/20] Abstract option-get logic - for https://github.com/njtierney/geotargets/pull/19#discussion_r1522572137 --- R/geotargets-option.R | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/R/geotargets-option.R b/R/geotargets-option.R index 696d5ec..96f2395 100644 --- a/R/geotargets-option.R +++ b/R/geotargets-option.R @@ -26,17 +26,32 @@ geotargets_option_get <- function(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_raster_gdal_creation_options <- function(option_name, option_value) { + gdal_creation_options <- Sys.getenv( + x = "GEOTARGETS_RASTER_GDAL_CREATION_OPTIONS", + unset = get_option(option_name, option_value, "ENCODING=UTF-8") + ) + the_option <- strsplit(gdal_creation_options, ";")[[1]] + the_option + } + + get_geotargets_raster_gdal_driver_name <- function(option_name, option_value) { + Sys.getenv( + x = "GEOTARGETS_RASTER_GDAL_DRIVER_NAME", + unset = get_option(option_name, option_value, "GTiff") + ) + } + switch(option_name, - "geotargets.raster.gdal_creation_options" = { - strsplit(Sys.getenv("GEOTARGETS_RASTER_GDAL_CREATION_OPTIONS", - unset = getOption(option_name, default = ifelse(is.null(option_value), "ENCODING=UTF-8", option_value))), - ";")[[1]] - }, - "geotargets.raster.gdal_driver_name" = { - Sys.getenv("GEOTARGETS_RASTER_GDAL_DRIVER_NAME", - unset = getOption(option_name, default = ifelse(is.null(option_value), "GTiff", option_value)) - ) - }) + "geotargets.raster.gdal_creation_options" = + get_geotargets_raster_gdal_creation_options(option_name, option_value), + "geotargets.raster.gdal_driver_name" = + get_geotargets_raster_gdal_driver_name(option_name, option_value) + ) } #' @param option_value Value to assign to option `x`. From 8b8cb7f2142dc326802c35737decd7766b916023 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Wed, 13 Mar 2024 07:48:50 -0700 Subject: [PATCH 09/20] Rename options (gdal prefix first) - RE: https://github.com/njtierney/geotargets/pull/19#discussion_r1522594423 --- R/geotargets-option.R | 24 ++++++++++++------------ R/tar-terra-rast.R | 8 ++++---- man/geotargets-options.Rd | 8 ++++---- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/R/geotargets-option.R b/R/geotargets-option.R index 96f2395..22c0bad 100644 --- a/R/geotargets-option.R +++ b/R/geotargets-option.R @@ -8,14 +8,14 @@ #' #' ## Available Options #' -#' - `"geotargets.raster.gdal_creation_options"` - set the GDAL creation options used when writing raster files to target store (default: `"ENCODING=UTF-8"`) +#' - `"geotargets.gdal.raster.creation_options"` - set the GDAL creation options used when writing raster files to target store (default: `"ENCODING=UTF-8"`) #' -#' - `"geotargets.raster.gdal_driver_name"` - set the file type used for raster data in target store (default: `"GTiff"`) +#' - `"geotargets.gdal.raster.driver_name"` - set the file type used for raster data in target store (default: `"GTiff"`) #' #' Each option can be overridden with a system environment variable. Options include: #' -#' - `GEOTARGETS_RASTER_GDAL_CREATION_OPTIONS` -#' - `GEOTARGETS_RASTER_GDAL_DRIVER_NAME` +#' - `GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS` +#' - `GEOTARGETS_GDAL_RASTER_DRIVER_NAME` #' #' @rdname geotargets-options #' @export @@ -30,27 +30,27 @@ geotargets_option_get <- function(option_name) { getOption(option_name, default = option_value %||% name) } - get_geotargets_raster_gdal_creation_options <- function(option_name, option_value) { + get_geotargets_gdal_raster_creation_options <- function(option_name, option_value) { gdal_creation_options <- Sys.getenv( - x = "GEOTARGETS_RASTER_GDAL_CREATION_OPTIONS", + x = "GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS", unset = get_option(option_name, option_value, "ENCODING=UTF-8") ) the_option <- strsplit(gdal_creation_options, ";")[[1]] the_option } - get_geotargets_raster_gdal_driver_name <- function(option_name, option_value) { + get_geotargets_gdal_raster_driver_name <- function(option_name, option_value) { Sys.getenv( - x = "GEOTARGETS_RASTER_GDAL_DRIVER_NAME", + x = "GEOTARGETS_GDAL_RASTER_DRIVER_NAME", unset = get_option(option_name, option_value, "GTiff") ) } switch(option_name, - "geotargets.raster.gdal_creation_options" = - get_geotargets_raster_gdal_creation_options(option_name, option_value), - "geotargets.raster.gdal_driver_name" = - get_geotargets_raster_gdal_driver_name(option_name, option_value) + "geotargets.gdal.raster.creation_options" = + get_geotargets_gdal_raster_creation_options(option_name, option_value), + "geotargets.gdal.raster.driver_name" = + get_geotargets_gdal_raster_driver_name(option_name, option_value) ) } diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index d5d005b..213c06d 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -102,10 +102,10 @@ create_format_terra_raster <- function(filetype, gdal, ...) { drv <- terra::gdal(drivers = TRUE) drv <- drv[drv$type == "raster" & grepl("write", drv$can), ] - filetype <- filetype %||% geotargets_option_get("raster.gdal_driver_name") + filetype <- filetype %||% geotargets_option_get("gdal.raster.driver_name") filetype <- rlang::arg_match0(filetype, drv$name) - gdal <- gdal %||% geotargets_option_get("raster.gdal_creation_options") + gdal <- gdal %||% geotargets_option_get("gdal.raster.creation_options") # NOTE: Option getting functions are set in the .write_terra_raster function template # to resolve issue with body<- not working in some evaluation contexts ({covr}). @@ -114,9 +114,9 @@ create_format_terra_raster <- function(filetype, gdal, ...) { terra::writeRaster( object, path, - filetype = geotargets::geotargets_option_get("raster.gdal_driver_name"), + filetype = geotargets::geotargets_option_get("gdal.raster.driver_name"), overwrite = TRUE, - gdal = geotargets::geotargets_option_get("raster.gdal_creation_options") + gdal = geotargets::geotargets_option_get("gdal.raster.creation_options") ) } diff --git a/man/geotargets-options.Rd b/man/geotargets-options.Rd index b79777f..91e4e40 100644 --- a/man/geotargets-options.Rd +++ b/man/geotargets-options.Rd @@ -20,14 +20,14 @@ Get or set behavior for geospatial data target stores using geotargets-specific \details{ \subsection{Available Options}{ \itemize{ -\item \code{"geotargets.raster.gdal_creation_options"} - set the GDAL creation options used when writing raster files to target store (default: \code{"ENCODING=UTF-8"}) -\item \code{"geotargets.raster.gdal_driver_name"} - set the file type used for raster data in target store (default: \code{"GTiff"}) +\item \code{"geotargets.gdal.raster.creation_options"} - set the GDAL creation options used when writing raster files to target store (default: \code{"ENCODING=UTF-8"}) +\item \code{"geotargets.gdal.raster.driver_name"} - set the file type used for raster data in target store (default: \code{"GTiff"}) } Each option can be overridden with a system environment variable. Options include: \itemize{ -\item \code{GEOTARGETS_RASTER_GDAL_CREATION_OPTIONS} -\item \code{GEOTARGETS_RASTER_GDAL_DRIVER_NAME} +\item \code{GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS} +\item \code{GEOTARGETS_GDAL_RASTER_DRIVER_NAME} } } } From afe94ef8296a14f65792f24d70d386e43ede741d Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Wed, 13 Mar 2024 08:31:46 -0700 Subject: [PATCH 10/20] Fix .onAttach for new option name --- R/AAAA.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/AAAA.R b/R/AAAA.R index 07cd632..d6770c4 100644 --- a/R/AAAA.R +++ b/R/AAAA.R @@ -5,6 +5,6 @@ geotargets_env <- function() { } .onAttach <- function(lib, pkg) { - geotargets.env$geotargets.raster.gdal_creation_options <- "ENCODING=UTF-8" - geotargets.env$geotargets.raster.gdal_driver_name <- "GTiff" + geotargets.env$geotargets.gdal.raster.creation_options <- "ENCODING=UTF-8" + geotargets.env$geotargets.gdal.raster.driver_name <- "GTiff" } From e21c5b227c5ff6110b2e038e747c74f395ab560d Mon Sep 17 00:00:00 2001 From: njtierney Date: Fri, 15 Mar 2024 13:56:48 +1100 Subject: [PATCH 11/20] updates readme, closes #28 --- .Rbuildignore | 1 + CODE_OF_CONDUCT.md | 126 +++++++++++++++++++++++++++++++++++++++++++++ README.Rmd | 92 ++++++++++++++++++++++++++++++--- README.md | 102 +++++++++++++++++++++++++++++++++--- 4 files changed, 308 insertions(+), 13 deletions(-) create mode 100644 CODE_OF_CONDUCT.md diff --git a/.Rbuildignore b/.Rbuildignore index 89469e1..efaebf1 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -11,3 +11,4 @@ ^_targets_packages\.R$ ^_targets\.Rmd$ ^_targets\.yaml$ +^CODE_OF_CONDUCT\.md$ diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..0c08474 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,126 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, caste, color, religion, or sexual +identity and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the overall + community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or advances of + any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email address, + without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at nicholas.tierney@gmail.com. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series of +actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or permanent +ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within the +community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.1, available at +. + +Community Impact Guidelines were inspired by +[Mozilla's code of conduct enforcement ladder][https://github.com/mozilla/inclusion]. + +For answers to common questions about this code of conduct, see the FAQ at +. Translations are available at . + +[homepage]: https://www.contributor-covenant.org diff --git a/README.Rmd b/README.Rmd index 40d895c..bd45d7e 100644 --- a/README.Rmd +++ b/README.Rmd @@ -22,7 +22,24 @@ knitr::opts_chunk$set( [![Codecov test coverage](https://codecov.io/gh/njtierney/geotargets/branch/master/graph/badge.svg)](https://app.codecov.io/gh/njtierney/geotargets?branch=master) -The goal of geotargets is to extend targets to work with geospatial data, like shapefiles and rasters. +`geotargets` extends targets to work with geospatial data formats, such as rasters and vectors (e.g., shapefiles). + +A relatively common gotcha moment when using popular libraries like `terra` with targets is running into erros with read and write. Due to the limitations that come with the underlying C++ implementation in the `terra` library, there are specific ways to write and read these objects. See `?terra` for details. `geotargets` helps handle these write and read steps, so you don't have to worry about them and can use targets as you are used to. + +In essence, if you've ever come across the error: + +``` +Error in .External(list(name = "CppMethod__invoke_notvoid", address = , : + NULL value passed as symbol address +``` + +or + +``` +Error: external pointer is not valid +``` + +When trying to read in a geospatial raster or vector in targets, then this is for you :) ## Installation @@ -32,11 +49,74 @@ You can install the development version of geotargets like so: remotes::install_github("njtierney/geotargets") ``` -## Package still under development +## A note on development + +`geotargets` is still undergoing development, and we would love for people to use the package to kick the tyres. We are using it in our own work, but feel it is appropriate that users use the package with the understanding the API could change in subtle or breaking ways. + +# Examples + +Below we show two examples of target factories: + +- `tar_terra_rast()` +- `tar_terra_vect()` + +You would use these in place of `tar_target()` in your targets pipeline, when you are doing work with terra raster or terra vector data. + +It is a bit tricky to implement targets workflows in a README, but if you would like to see and download working examples for yourself, see the repo, [demo-geotargets](https://github.com/njtierney/demo-geotargets) + +## `tar_terra_rast()`: targets with terra rasters + +```{r} +#| label: tar-terra-rast +#| eval: false +library(targets) +tar_dir({ # tar_dir() runs code from a temporary directory. + tar_script({ + library(targets) + library(geotargets) + list( + tar_terra_rast( + terra_rast_example, + system.file("ex/elev.tif", package = "terra") |> terra::rast() + ) + ) + }) + tar_make() + x <- tar_read(terra_rast_example) + x +}) +``` + -Currently geotargets provides: +## `tar_terra_vect()`: targets with terra vectors + +```{r} +#| label: tar-terra-vect +#| eval: false +tar_dir({ # tar_dir() runs code from a temporary directory. + tar_script({ + library(geotargets) + lux_area <- function(projection = "EPSG:4326") { + terra::project( + terra::vect(system.file("ex", "lux.shp", + package = "terra" + )), + projection + ) + } + list( + tar_terra_vect( + terra_vect_example, + lux_area() + ) + ) + }) + tar_make() + x <- tar_read(terra_rast_example) + x +}) +``` -- `tar_terra_rast` -- `tar_terra_vect` +## Code of Conduct -These are under active development. +Please note that the geotargets project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/1/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms. diff --git a/README.md b/README.md index 7cb1300..278a2ca 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,28 @@ Targetopia](https://img.shields.io/badge/R_Targetopia-member-blue?style=flat&lab coverage](https://codecov.io/gh/njtierney/geotargets/branch/master/graph/badge.svg)](https://app.codecov.io/gh/njtierney/geotargets?branch=master) -The goal of geotargets is to extend targets to work with geospatial -data, like shapefiles and rasters. +`geotargets` extends targets to work with geospatial data formats, such +as rasters and vectors (e.g., shapefiles). + +A relatively common gotcha moment when using popular libraries like +`terra` with targets is running into erros with read and write. Due to +the limitations that come with the underlying C++ implementation in the +`terra` library, there are specific ways to write and read these +objects. See `?terra` for details. `geotargets` helps handle these write +and read steps, so you don’t have to worry about them and can use +targets as you are used to. + +In essence, if you’ve ever come across the error: + + Error in .External(list(name = "CppMethod__invoke_notvoid", address = , : + NULL value passed as symbol address + +or + + Error: external pointer is not valid + +When trying to read in a geospatial raster or vector in targets, then +this is for you :) ## Installation @@ -26,11 +46,79 @@ You can install the development version of geotargets like so: remotes::install_github("njtierney/geotargets") ``` -## Package still under development +## A note on development + +`geotargets` is still undergoing development, and we would love for +people to use the package to kick the tyres. We are using it in our own +work, but feel it is appropriate that users use the package with the +understanding the API could change in subtle or breaking ways. + +# Examples + +Below we show two examples of target factories: + +- `tar_terra_rast()` +- `tar_terra_vect()` -Currently geotargets provides: +You would use these in place of `tar_target()` in your targets pipeline, +when you are doing work with terra raster or terra vector data. + +It is a bit tricky to implement targets workflows in a README, but if +you would like to see and download working examples for yourself, see +the repo, +[demo-geotargets](https://github.com/njtierney/demo-geotargets) + +## `tar_terra_rast()`: targets with terra rasters + +``` r +library(targets) +tar_dir({ # tar_dir() runs code from a temporary directory. + tar_script({ + library(targets) + library(geotargets) + list( + tar_terra_rast( + terra_rast_example, + system.file("ex/elev.tif", package = "terra") |> terra::rast() + ) + ) + }) + tar_make() + x <- tar_read(terra_rast_example) + x +}) +``` + +## `tar_terra_vect()`: targets with terra vectors + +``` r +tar_dir({ # tar_dir() runs code from a temporary directory. + tar_script({ + library(geotargets) + lux_area <- function(projection = "EPSG:4326") { + terra::project( + terra::vect(system.file("ex", "lux.shp", + package = "terra" + )), + projection + ) + } + list( + tar_terra_vect( + terra_vect_example, + lux_area() + ) + ) + }) + tar_make() + x <- tar_read(terra_rast_example) + x +}) +``` -- `tar_terra_rast` -- `tar_terra_vect` +## Code of Conduct -These are under active development. +Please note that the geotargets project is released with a [Contributor +Code of +Conduct](https://contributor-covenant.org/version/2/1/CODE_OF_CONDUCT.html). +By contributing to this project, you agree to abide by its terms. From a709181e353d9428d903a85ece7d23a3635dcfcb Mon Sep 17 00:00:00 2001 From: njtierney Date: Fri, 15 Mar 2024 14:42:06 +1100 Subject: [PATCH 12/20] Tidying up old lettuce and improving coverage * Remove old lettuce (code with comments that isn't used anymore) * remove utils.R file (for now) as it only had code in there that removed a .zip extensions, which isn't used anymore. This also takes our code coverage up to 100% :dizzy: --- R/tar-terra-vect.R | 35 ----------------------------------- R/utils.R | 11 ----------- 2 files changed, 46 deletions(-) delete mode 100644 R/utils.R diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index eadc681..2379388 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -1,38 +1,3 @@ -# format_terra_vect_shapefile <- targets::tar_format( -# read = function(path) { -# terra::vect( -# paste0( -# "/vsizip/", -# file.path( -# path, -# replace_dot_zip_with_shp(path) -# ) -# ) -# ) -# }, -# write = function(object, path) { -# terra::writeVector( -# x = object, -# filename = replace_dot_zip_with_shp(path), -# filetype = "ESRI Shapefile", -# overwrite = TRUE -# ) -# zf <- list.files( -# pattern = replace_dot_zip( -# x = path, -# replacement = "" -# ) -# ) -# utils::zip( -# zipfile = path, -# files = zf -# ) -# unlink(zf) -# }, -# marshal = function(object) terra::wrap(object), -# unmarshal = function(object) terra::unwrap(object) -# ) - #' Targets format for terra vectors #' #' Provides targets format for `terra::vect` objects diff --git a/R/utils.R b/R/utils.R deleted file mode 100644 index 61f77ad..0000000 --- a/R/utils.R +++ /dev/null @@ -1,11 +0,0 @@ -replace_dot_zip <- function(x, replacement) { - gsub( - pattern = "\\.zip", - replacement = replacement, - x = basename(x) - ) -} - -replace_dot_zip_with_shp <- function(x) { - replace_dot_zip(x, ".shp") -} From 2da0e56c2957f51b1e3fbc898e91523aed9a383e Mon Sep 17 00:00:00 2001 From: njtierney Date: Fri, 15 Mar 2024 14:42:33 +1100 Subject: [PATCH 13/20] minor tweaks to README wording --- README.Rmd | 4 ++-- README.md | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.Rmd b/README.Rmd index bd45d7e..dec2c3d 100644 --- a/README.Rmd +++ b/README.Rmd @@ -51,7 +51,7 @@ remotes::install_github("njtierney/geotargets") ## A note on development -`geotargets` is still undergoing development, and we would love for people to use the package to kick the tyres. We are using it in our own work, but feel it is appropriate that users use the package with the understanding the API could change in subtle or breaking ways. +`geotargets` is still undergoing development, and we would love for people to use the package to kick the tyres. We are using it in our own work, but want users to know that the API could change in subtle or breaking ways. # Examples @@ -62,7 +62,7 @@ Below we show two examples of target factories: You would use these in place of `tar_target()` in your targets pipeline, when you are doing work with terra raster or terra vector data. -It is a bit tricky to implement targets workflows in a README, but if you would like to see and download working examples for yourself, see the repo, [demo-geotargets](https://github.com/njtierney/demo-geotargets) +It is a bit tricky to implement targets workflows in a README, but if you would like to see and download working examples for yourself, see the repo, [demo-geotargets](https://github.com/njtierney/demo-geotargets). ## `tar_terra_rast()`: targets with terra rasters diff --git a/README.md b/README.md index 278a2ca..c5c1ef6 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,8 @@ remotes::install_github("njtierney/geotargets") `geotargets` is still undergoing development, and we would love for people to use the package to kick the tyres. We are using it in our own -work, but feel it is appropriate that users use the package with the -understanding the API could change in subtle or breaking ways. +work, but want users to know that the API could change in subtle or +breaking ways. # Examples @@ -66,7 +66,7 @@ when you are doing work with terra raster or terra vector data. It is a bit tricky to implement targets workflows in a README, but if you would like to see and download working examples for yourself, see the repo, -[demo-geotargets](https://github.com/njtierney/demo-geotargets) +[demo-geotargets](https://github.com/njtierney/demo-geotargets). ## `tar_terra_rast()`: targets with terra rasters From ae486a806ae4e20e51d285f9e78257f772373dae Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Thu, 14 Mar 2024 21:02:00 -0700 Subject: [PATCH 14/20] Remove _NAME/_name from GDAL raster driver option and env var names --- R/AAAA.R | 2 +- R/geotargets-option.R | 12 ++++++------ R/tar-terra-rast.R | 6 +++--- man/geotargets-options.Rd | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/R/AAAA.R b/R/AAAA.R index d6770c4..0aac182 100644 --- a/R/AAAA.R +++ b/R/AAAA.R @@ -6,5 +6,5 @@ geotargets_env <- function() { .onAttach <- function(lib, pkg) { geotargets.env$geotargets.gdal.raster.creation_options <- "ENCODING=UTF-8" - geotargets.env$geotargets.gdal.raster.driver_name <- "GTiff" + geotargets.env$geotargets.gdal.raster.driver <- "GTiff" } diff --git a/R/geotargets-option.R b/R/geotargets-option.R index 22c0bad..9ecf77f 100644 --- a/R/geotargets-option.R +++ b/R/geotargets-option.R @@ -10,12 +10,12 @@ #' #' - `"geotargets.gdal.raster.creation_options"` - set the GDAL creation options used when writing raster files to target store (default: `"ENCODING=UTF-8"`) #' -#' - `"geotargets.gdal.raster.driver_name"` - set the file type used for raster data in target store (default: `"GTiff"`) +#' - `"geotargets.gdal.raster.driver"` - set the file type used for raster data in target store (default: `"GTiff"`) #' #' Each option can be overridden with a system environment variable. Options include: #' #' - `GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS` -#' - `GEOTARGETS_GDAL_RASTER_DRIVER_NAME` +#' - `GEOTARGETS_GDAL_RASTER_DRIVER` #' #' @rdname geotargets-options #' @export @@ -39,9 +39,9 @@ geotargets_option_get <- function(option_name) { the_option } - get_geotargets_gdal_raster_driver_name <- function(option_name, option_value) { + get_geotargets_gdal_raster_driver <- function(option_name, option_value) { Sys.getenv( - x = "GEOTARGETS_GDAL_RASTER_DRIVER_NAME", + x = "GEOTARGETS_GDAL_RASTER_DRIVER", unset = get_option(option_name, option_value, "GTiff") ) } @@ -49,8 +49,8 @@ geotargets_option_get <- function(option_name) { switch(option_name, "geotargets.gdal.raster.creation_options" = get_geotargets_gdal_raster_creation_options(option_name, option_value), - "geotargets.gdal.raster.driver_name" = - get_geotargets_gdal_raster_driver_name(option_name, option_value) + "geotargets.gdal.raster.driver" = + get_geotargets_gdal_raster_driver(option_name, option_value) ) } diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index 213c06d..5866a05 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -64,7 +64,7 @@ tar_terra_rast <- function(name, ) # if not specified by user, pull the corresponding geotargets option - filetype <- filetype %||% geotargets_option_get("raster.gdal_driver_name") + filetype <- filetype %||% geotargets_option_get("raster.gdal_driver") gdal <- gdal %||% geotargets_option_get("raster.gdal_creation_options") targets::tar_target_raw( @@ -102,7 +102,7 @@ create_format_terra_raster <- function(filetype, gdal, ...) { drv <- terra::gdal(drivers = TRUE) drv <- drv[drv$type == "raster" & grepl("write", drv$can), ] - filetype <- filetype %||% geotargets_option_get("gdal.raster.driver_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") @@ -114,7 +114,7 @@ create_format_terra_raster <- function(filetype, gdal, ...) { terra::writeRaster( object, path, - filetype = geotargets::geotargets_option_get("gdal.raster.driver_name"), + filetype = geotargets::geotargets_option_get("gdal.raster.driver"), overwrite = TRUE, gdal = geotargets::geotargets_option_get("gdal.raster.creation_options") ) diff --git a/man/geotargets-options.Rd b/man/geotargets-options.Rd index 91e4e40..3b08609 100644 --- a/man/geotargets-options.Rd +++ b/man/geotargets-options.Rd @@ -21,13 +21,13 @@ Get or set behavior for geospatial data target stores using geotargets-specific \subsection{Available Options}{ \itemize{ \item \code{"geotargets.gdal.raster.creation_options"} - set the GDAL creation options used when writing raster files to target store (default: \code{"ENCODING=UTF-8"}) -\item \code{"geotargets.gdal.raster.driver_name"} - set the file type used for raster data in target store (default: \code{"GTiff"}) +\item \code{"geotargets.gdal.raster.driver"} - set the file type used for raster data in target store (default: \code{"GTiff"}) } Each option can be overridden with a system environment variable. Options include: \itemize{ \item \code{GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS} -\item \code{GEOTARGETS_GDAL_RASTER_DRIVER_NAME} +\item \code{GEOTARGETS_GDAL_RASTER_DRIVER} } } } From 1b3627dba0cbbd0d9542f1e59778b2b0eedaf696 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Thu, 14 Mar 2024 21:09:51 -0700 Subject: [PATCH 15/20] only specify geotargets default options once (in `geotargets_option_get()`) --- R/AAAA.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/AAAA.R b/R/AAAA.R index 0aac182..849fd5b 100644 --- a/R/AAAA.R +++ b/R/AAAA.R @@ -5,6 +5,6 @@ geotargets_env <- function() { } .onAttach <- function(lib, pkg) { - geotargets.env$geotargets.gdal.raster.creation_options <- "ENCODING=UTF-8" - geotargets.env$geotargets.gdal.raster.driver <- "GTiff" + geotargets.env$geotargets.gdal.raster.creation_options <- geotargets_option_get("gdal.raster.creation_options") + geotargets.env$geotargets.gdal.raster.driver <- geotargets_option_get("gdal.raster.driver") } From aac10b48839512db879eebfb46cc51b5f43a361f Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Thu, 14 Mar 2024 21:47:44 -0700 Subject: [PATCH 16/20] Add GDAL vector driver and vector creation options --- R/AAAA.R | 3 +++ R/geotargets-option.R | 35 +++++++++++++++++++++++++++++++---- man/geotargets-options.Rd | 12 +++++++++--- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/R/AAAA.R b/R/AAAA.R index 849fd5b..4b046dd 100644 --- a/R/AAAA.R +++ b/R/AAAA.R @@ -7,4 +7,7 @@ geotargets_env <- function() { .onAttach <- function(lib, pkg) { geotargets.env$geotargets.gdal.raster.creation_options <- geotargets_option_get("gdal.raster.creation_options") geotargets.env$geotargets.gdal.raster.driver <- geotargets_option_get("gdal.raster.driver") + + geotargets.env$geotargets.gdal.vector.creation_options <- geotargets_option_get("gdal.vector.creation_options") + geotargets.env$geotargets.gdal.vector.driver <- geotargets_option_get("gdal.vector.driver") } diff --git a/R/geotargets-option.R b/R/geotargets-option.R index 9ecf77f..8b87067 100644 --- a/R/geotargets-option.R +++ b/R/geotargets-option.R @@ -8,14 +8,22 @@ #' #' ## Available Options #' -#' - `"geotargets.gdal.raster.creation_options"` - set the GDAL creation options used when writing raster files to target store (default: `"ENCODING=UTF-8"`) +#' - `"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: #' -#' - `"geotargets.gdal.raster.driver"` - set the file type used for raster data in target store (default: `"GTiff"`) +#' - `"geotargets.gdal.raster.creation_options"` - character. Set the GDAL creation options used when writing raster files to target store (default: `"ENCODING=UTF-8"`). 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: +#' +#' - `"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: #' #' Each option can be overridden with a system environment variable. Options include: #' -#' - `GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS` #' - `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 @@ -46,11 +54,30 @@ geotargets_option_get <- function(option_name) { ) } + 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_option <- strsplit(gdal_creation_options, ";")[[1]] + the_option + } + + 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) + 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) ) } diff --git a/man/geotargets-options.Rd b/man/geotargets-options.Rd index 3b08609..2214ce3 100644 --- a/man/geotargets-options.Rd +++ b/man/geotargets-options.Rd @@ -20,14 +20,20 @@ Get or set behavior for geospatial data target stores using geotargets-specific \details{ \subsection{Available Options}{ \itemize{ -\item \code{"geotargets.gdal.raster.creation_options"} - set the GDAL creation options used when writing raster files to target store (default: \code{"ENCODING=UTF-8"}) -\item \code{"geotargets.gdal.raster.driver"} - set the file type used for raster data in target store (default: \code{"GTiff"}) +\item \code{"geotargets.gdal.raster.driver"} - character. Length 1. Set the driver used for raster data in target store (default: \code{"GTiff"}). Options for driver names can be found here: \url{https://gdal.org/drivers/raster/index.html} +\item \code{"geotargets.gdal.raster.creation_options"} - character. Set the GDAL creation options used when writing raster files to target store (default: \code{"ENCODING=UTF-8"}). You may specify multiple values e.g. \code{c("COMPRESS=DEFLATE", "TFW=YES")}. Each GDAL driver supports a unique set of creation options. For example, with the default \code{"GTiff"} driver: \url{https://gdal.org/drivers/raster/gtiff.html#creation-options} +\item \code{"geotargets.gdal.vector.driver"} - character. Length 1. Set the file type used for vector data in target store (default: \code{"GeoJSON"}). +\item \code{"geotargets.gdal.vector.creation_options"} - character. Set the GDAL layer creation options used when writing vector files to target store (default: \code{"ENCODING=UTF-8"}). You may specify multiple values e.g. \code{c("WRITE_BBOX=YES", "COORDINATE_PRECISION=10")}. Each GDAL driver supports a unique set of creation options. For example, with the default \code{"GeoJSON"} driver: \url{https://gdal.org/drivers/vector/geojson.html#layer-creation-options} } Each option can be overridden with a system environment variable. Options include: \itemize{ -\item \code{GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS} \item \code{GEOTARGETS_GDAL_RASTER_DRIVER} +\item \code{GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS} +\item \code{GEOTARGETS_GDAL_VECTOR_DRIVER} +\item \code{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: \code{"COMPRESS=DEFLATE;TFW=YES"}. } } From 1f6c7b6f61ac500cfc43dd6977e29e035fca5c35 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Thu, 14 Mar 2024 21:56:17 -0700 Subject: [PATCH 17/20] Add `geotargets_repair_option_name()` --- R/geotargets-option.R | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/R/geotargets-option.R b/R/geotargets-option.R index 8b87067..fe631ff 100644 --- a/R/geotargets-option.R +++ b/R/geotargets-option.R @@ -28,10 +28,8 @@ #' @rdname geotargets-options #' @export geotargets_option_get <- function(option_name) { - if (!startsWith(option_name, "geotargets.")) { - option_name <- paste0("geotargets.", option_name) - } + option_name <- geotargets_repair_option_name(option_name) option_value <- geotargets_env()[[option_name]] get_option <- function(option_name, option_value, name){ @@ -69,6 +67,7 @@ geotargets_option_get <- function(option_name) { 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), @@ -85,8 +84,12 @@ geotargets_option_get <- function(option_name) { #' @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_repair_option_name <- function(option_name) { if (!startsWith(option_name, "geotargets.")) { option_name <- paste0("geotargets.", option_name) } - geotargets.env[[option_name]] <- option_value } From ace6b06be12f48ca6b0287765835499fe3a64421 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Thu, 14 Mar 2024 21:59:17 -0700 Subject: [PATCH 18/20] tar_terra_rast: fix option names --- R/tar-terra-rast.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index 5866a05..de18c69 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -64,8 +64,8 @@ tar_terra_rast <- function(name, ) # if not specified by user, pull the corresponding geotargets option - filetype <- filetype %||% geotargets_option_get("raster.gdal_driver") - gdal <- gdal %||% geotargets_option_get("raster.gdal_creation_options") + filetype <- filetype %||% geotargets_option_get("gdal.raster.driver") + gdal <- gdal %||% geotargets_option_get("gdal.raster.creation_options") targets::tar_target_raw( name = name, From 80ce7cfb2c6920738cf4c9e2fd6660b3dcf691b3 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Thu, 14 Mar 2024 22:23:07 -0700 Subject: [PATCH 19/20] Update R/geotargets-option.R Co-authored-by: Nicholas Tierney --- R/geotargets-option.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/geotargets-option.R b/R/geotargets-option.R index fe631ff..7bea914 100644 --- a/R/geotargets-option.R +++ b/R/geotargets-option.R @@ -57,8 +57,8 @@ geotargets_option_get <- function(option_name) { x = "GEOTARGETS_GDAL_VECTOR_CREATION_OPTIONS", unset = get_option(option_name, option_value, "ENCODING=UTF-8") ) - the_option <- strsplit(gdal_creation_options, ";")[[1]] - the_option + the_options <- strsplit(gdal_creation_options, ";")[[1]] + the_options } get_geotargets_gdal_vector_driver <- function(option_name, option_value) { From 6898e716d0412b674655c33ba0ad818cd24b208b Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Thu, 14 Mar 2024 22:56:52 -0700 Subject: [PATCH 20/20] Default creation option for raster should be empty, not ENCODING --- R/geotargets-option.R | 4 ++-- man/geotargets-options.Rd | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/geotargets-option.R b/R/geotargets-option.R index 7bea914..b682432 100644 --- a/R/geotargets-option.R +++ b/R/geotargets-option.R @@ -10,7 +10,7 @@ #' #' - `"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: #' -#' - `"geotargets.gdal.raster.creation_options"` - character. Set the GDAL creation options used when writing raster files to target store (default: `"ENCODING=UTF-8"`). 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: +#' - `"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: #' #' - `"geotargets.gdal.vector.driver"` - character. Length 1. Set the file type used for vector data in target store (default: `"GeoJSON"`). #' @@ -39,7 +39,7 @@ geotargets_option_get <- function(option_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, "ENCODING=UTF-8") + unset = get_option(option_name, option_value, "") ) the_option <- strsplit(gdal_creation_options, ";")[[1]] the_option diff --git a/man/geotargets-options.Rd b/man/geotargets-options.Rd index 2214ce3..e1cda96 100644 --- a/man/geotargets-options.Rd +++ b/man/geotargets-options.Rd @@ -21,7 +21,7 @@ Get or set behavior for geospatial data target stores using geotargets-specific \subsection{Available Options}{ \itemize{ \item \code{"geotargets.gdal.raster.driver"} - character. Length 1. Set the driver used for raster data in target store (default: \code{"GTiff"}). Options for driver names can be found here: \url{https://gdal.org/drivers/raster/index.html} -\item \code{"geotargets.gdal.raster.creation_options"} - character. Set the GDAL creation options used when writing raster files to target store (default: \code{"ENCODING=UTF-8"}). You may specify multiple values e.g. \code{c("COMPRESS=DEFLATE", "TFW=YES")}. Each GDAL driver supports a unique set of creation options. For example, with the default \code{"GTiff"} driver: \url{https://gdal.org/drivers/raster/gtiff.html#creation-options} +\item \code{"geotargets.gdal.raster.creation_options"} - character. Set the GDAL creation options used when writing raster files to target store (default: \code{""}). You may specify multiple values e.g. \code{c("COMPRESS=DEFLATE", "TFW=YES")}. Each GDAL driver supports a unique set of creation options. For example, with the default \code{"GTiff"} driver: \url{https://gdal.org/drivers/raster/gtiff.html#creation-options} \item \code{"geotargets.gdal.vector.driver"} - character. Length 1. Set the file type used for vector data in target store (default: \code{"GeoJSON"}). \item \code{"geotargets.gdal.vector.creation_options"} - character. Set the GDAL layer creation options used when writing vector files to target store (default: \code{"ENCODING=UTF-8"}). You may specify multiple values e.g. \code{c("WRITE_BBOX=YES", "COORDINATE_PRECISION=10")}. Each GDAL driver supports a unique set of creation options. For example, with the default \code{"GeoJSON"} driver: \url{https://gdal.org/drivers/vector/geojson.html#layer-creation-options} }