From ff87218b2c170c7ba312f752fb1d731438831163 Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Fri, 15 Mar 2024 15:11:21 -0700 Subject: [PATCH 01/11] move terra to suggests --- DESCRIPTION | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 93fa945..2cf3664 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,9 +33,9 @@ Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.1 Imports: rlang, - targets, - terra + targets Suggests: + terra, testthat (>= 3.0.0) Depends: R (>= 4.1.0) From cc69c1dabf17cb85e0a52a907e193aeea9c79b4f Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Fri, 15 Mar 2024 15:12:06 -0700 Subject: [PATCH 02/11] use_tidy_description() --- DESCRIPTION | 51 +++++++++++++++++++-------------------------------- 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 2cf3664..4a620e2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,43 +2,30 @@ Package: geotargets Title: Targets extensions for geospatial formats Version: 0.0.0.9000 Authors@R: c( - person( - given = "Nicholas", - family = "Tierney", - email = "nicholas.tierney@gmail.com", - role = c("aut", "cre"), - comment = c(ORCID = "0000-0003-1460-8722") - ), - person( - given = "Eric", - family = "Scott", - role = c("aut"), - comment = c(ORCID = "0000-0002-7430-7879") - ), - person( - given = "Andrew", - family = "Brown", - role = c("aut"), - comment = c(ORCID = "0000-0002-4565-533X") - ) - ) -Description: Provides extensions for various geospatial file formats, - such as shapefiles and rasters. See the vignettes for worked - examples and demonstrations and explanations of how to use the various - package extensions. + person("Nicholas", "Tierney", , "nicholas.tierney@gmail.com", role = c("aut", "cre"), + comment = c(ORCID = "0000-0003-1460-8722")), + person("Eric", "Scott", role = "aut", + comment = c(ORCID = "0000-0002-7430-7879")), + person("Andrew", "Brown", role = "aut", + comment = c(ORCID = "0000-0002-4565-533X")) + ) +Description: Provides extensions for various geospatial file formats, such + as shapefiles and rasters. See the vignettes for worked examples and + demonstrations and explanations of how to use the various package + extensions. License: MIT + file LICENSE -Encoding: UTF-8 -Language: en-GB -Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +URL: https://github.com/njtierney/geotargets +BugReports: https://github.com/njtierney/geotargets/issues +Depends: + R (>= 4.1.0) Imports: rlang, targets Suggests: terra, testthat (>= 3.0.0) -Depends: - R (>= 4.1.0) Config/testthat/edition: 3 -URL: https://github.com/njtierney/geotargets -BugReports: https://github.com/njtierney/geotargets/issues +Encoding: UTF-8 +Language: en-GB +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.3.1 From 239bcbdc49208a253685d7755d525bdea5133ee1 Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Fri, 15 Mar 2024 15:16:24 -0700 Subject: [PATCH 03/11] use rlang::check_installed() in tar_*() funs --- R/tar-terra-rast.R | 6 +----- R/tar-terra-vect.R | 11 +---------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index ca9119f..dded312 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -49,6 +49,7 @@ tar_terra_rast <- function(name, retrieval = targets::tar_option_get("retrieval"), cue = targets::tar_option_get("cue")) { + rlang::check_installed("terra") name <- targets::tar_deparse_language(substitute(name)) envir <- targets::tar_option_get("envir") @@ -95,11 +96,6 @@ tar_terra_rast <- function(name, #' @param ... Additional arguments not yet used #' @noRd create_format_terra_raster <- function(filetype, gdal, ...) { - - if (!requireNamespace("terra")) { - stop("package 'terra' is required", call. = FALSE) - } - # get list of drivers available for writing depending on what the user's GDAL supports drv <- terra::gdal(drivers = TRUE) drv <- drv[drv$type == "raster" & grepl("write", drv$can), ] diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index 7cb9c82..e9adcaf 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -57,6 +57,7 @@ tar_terra_vect <- function(name, storage = targets::tar_option_get("storage"), retrieval = targets::tar_option_get("retrieval"), cue = targets::tar_option_get("cue")) { + rlang::check_installed("terra") name <- targets::tar_deparse_language(substitute(name)) envir <- targets::tar_option_get("envir") @@ -112,11 +113,6 @@ tar_terra_vect <- function(name, #' @param ... Additional arguments not yet used #' @noRd create_format_terra_vect <- function(filetype, options, ...) { - - if (!requireNamespace("terra")) { - stop("package 'terra' is required", call. = FALSE) - } - # get list of drivers available for writing depending on what the user's GDAL supports drv <- terra::gdal(drivers = TRUE) drv <- drv[drv$type == "vector" & grepl("write", drv$can), ] @@ -153,11 +149,6 @@ create_format_terra_vect <- function(filetype, options, ...) { #' @param ... Additional arguments not yet used #' @noRd create_format_terra_vect_shz <- function(options, ...) { - - if (!requireNamespace("terra")) { - stop("package 'terra' is required", call. = FALSE) - } - .write_terra_vector <- function(object, path) { terra::writeVector( x = object, From 7f9fca9e49340490ad87ba55848dd447892e4f6c Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Fri, 15 Mar 2024 15:30:56 -0700 Subject: [PATCH 04/11] revert to base R--better errors from tar_make() --- R/tar-terra-rast.R | 4 +++- R/tar-terra-vect.R | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index dded312..66d9d37 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -49,7 +49,9 @@ tar_terra_rast <- function(name, retrieval = targets::tar_option_get("retrieval"), cue = targets::tar_option_get("cue")) { - rlang::check_installed("terra") + if (!requireNamespace("terra")) { + stop("package 'terra' is required", call. = FALSE) + } name <- targets::tar_deparse_language(substitute(name)) envir <- targets::tar_option_get("envir") diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index e9adcaf..817166f 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -57,7 +57,9 @@ tar_terra_vect <- function(name, storage = targets::tar_option_get("storage"), retrieval = targets::tar_option_get("retrieval"), cue = targets::tar_option_get("cue")) { - rlang::check_installed("terra") + if (!requireNamespace("terra")) { + stop("package 'terra' is required", call. = FALSE) + } name <- targets::tar_deparse_language(substitute(name)) envir <- targets::tar_option_get("envir") From 1368d4aadd4f87bb689aec085d6e3f53d2a594ed Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Fri, 15 Mar 2024 15:37:30 -0700 Subject: [PATCH 05/11] move filetype validation --- R/tar-terra-rast.R | 15 ++++++--------- R/tar-terra-vect.R | 16 ++++++---------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index 66d9d37..0b57132 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -68,8 +68,14 @@ tar_terra_rast <- function(name, tidy_eval = tidy_eval ) + # get list of drivers available for writing depending on what the user's GDAL supports + drv <- terra::gdal(drivers = TRUE) + drv <- drv[drv$type == "raster" & grepl("write", drv$can), ] + # 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( @@ -98,15 +104,6 @@ tar_terra_rast <- function(name, #' @param ... Additional arguments not yet used #' @noRd create_format_terra_raster <- function(filetype, gdal, ...) { - # get list of drivers available for writing depending on what the user's GDAL supports - drv <- terra::gdal(drivers = TRUE) - drv <- drv[drv$type == "raster" & grepl("write", drv$can), ] - - filetype <- filetype %||% geotargets_option_get("gdal.raster.driver") - filetype <- rlang::arg_match0(filetype, drv$name) - - 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}). # TODO: It should be fine to have filetype and gdal as NULL diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index 817166f..fbecef1 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -75,8 +75,14 @@ tar_terra_vect <- function(name, tidy_eval = tidy_eval ) + # get list of drivers available for writing depending on what the user's GDAL supports + drv <- terra::gdal(drivers = TRUE) + drv <- drv[drv$type == "vector" & grepl("write", drv$can), ] + # if not specified by user, pull the corresponding geotargets option filetype <- filetype %||% geotargets_option_get("gdal.vector.driver") + filetype <- rlang::arg_match0(filetype, drv$name) + gdal <- gdal %||% geotargets_option_get("gdal.vector.creation_options") format <- ifelse( @@ -115,16 +121,6 @@ tar_terra_vect <- function(name, #' @param ... Additional arguments not yet used #' @noRd create_format_terra_vect <- function(filetype, options, ...) { - # get list of drivers available for writing depending on what the user's GDAL supports - drv <- terra::gdal(drivers = TRUE) - drv <- drv[drv$type == "vector" & grepl("write", drv$can), ] - - if (is.null(filetype)) { - filetype <- "GeoJSON" - } - - filetype <- match.arg(filetype, drv$name) - .write_terra_vector <- function(object, path) { terra::writeVector( object, From 16764c2ef523365717f02249819a4eaabe95837b Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Mon, 18 Mar 2024 09:33:21 -0700 Subject: [PATCH 06/11] revert styling --- DESCRIPTION | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 4a620e2..6b32b64 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,30 +2,34 @@ Package: geotargets Title: Targets extensions for geospatial formats Version: 0.0.0.9000 Authors@R: c( - person("Nicholas", "Tierney", , "nicholas.tierney@gmail.com", role = c("aut", "cre"), - comment = c(ORCID = "0000-0003-1460-8722")), - person("Eric", "Scott", role = "aut", - comment = c(ORCID = "0000-0002-7430-7879")), - person("Andrew", "Brown", role = "aut", - comment = c(ORCID = "0000-0002-4565-533X")) - ) -Description: Provides extensions for various geospatial file formats, such - as shapefiles and rasters. See the vignettes for worked examples and - demonstrations and explanations of how to use the various package - extensions. + person( + given = "Nicholas", + family = "Tierney", + email = "nicholas.tierney@gmail.com", + role = c("aut", "cre"), + comment = c(ORCID = "0000-0003-1460-8722") + ), + person( + given = "Eric", + family = "Scott", + role = c("aut"), + comment = c(ORCID = "0000-0002-7430-7879") + ) + ) +Description: Provides extensions for various geospatial file formats, + such as shapefiles and rasters. See the vignettes for worked + examples and demonstrations and explanations of how to use the various + package extensions. License: MIT + file LICENSE -URL: https://github.com/njtierney/geotargets -BugReports: https://github.com/njtierney/geotargets/issues -Depends: - R (>= 4.1.0) +Encoding: UTF-8 +Language: en-GB +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.3.1 Imports: - rlang, targets Suggests: terra, testthat (>= 3.0.0) Config/testthat/edition: 3 -Encoding: UTF-8 -Language: en-GB -Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +URL: https://github.com/njtierney/geotargets +BugReports: https://github.com/njtierney/geotargets/issues From ef368ca0dac76ba6750323e633e348ffff560bb2 Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Mon, 18 Mar 2024 09:46:39 -0700 Subject: [PATCH 07/11] forgot rlang --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6b32b64..21e0312 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,7 +26,8 @@ Language: en-GB Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.1 Imports: - targets + targets, + rlang Suggests: terra, testthat (>= 3.0.0) From 7ced37c68a27fa1e84831d881ed9696636e3bdbf Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Mon, 18 Mar 2024 09:52:49 -0700 Subject: [PATCH 08/11] whoops, reverted to wrong version I guess. Put Andrew back --- DESCRIPTION | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index 21e0312..9cf8ed3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,6 +14,12 @@ Authors@R: c( family = "Scott", role = c("aut"), comment = c(ORCID = "0000-0002-7430-7879") + ), + person( + given = "Andrew", + family = "Brown", + role = c("aut"), + comment = c(ORCID = "0000-0002-4565-533X") ) ) Description: Provides extensions for various geospatial file formats, From 7dda93c607583a365ea6f0ebacb9edf1d5805f96 Mon Sep 17 00:00:00 2001 From: njtierney Date: Thu, 21 Mar 2024 22:44:54 +1000 Subject: [PATCH 09/11] use check_pkg_installed --- R/tar-terra-rast.R | 9 +++------ R/tar-terra-vect.R | 14 +++++--------- R/utils.R | 9 +++++++++ 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index cea0c67..089cf2e 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -49,9 +49,8 @@ tar_terra_rast <- function(name, retrieval = targets::tar_option_get("retrieval"), cue = targets::tar_option_get("cue")) { - if (!requireNamespace("terra")) { - stop("package 'terra' is required", call. = FALSE) - } + check_pkg_installed("terra") + name <- targets::tar_deparse_language(substitute(name)) envir <- targets::tar_option_get("envir") @@ -105,9 +104,7 @@ tar_terra_rast <- function(name, #' @noRd create_format_terra_raster <- function(filetype, gdal, ...) { - if (!requireNamespace("terra")) { - stop("package 'terra' is required", call. = FALSE) - } + check_pkg_installed("terra") # get list of drivers available for writing depending on what the user's GDAL supports drv <- terra::gdal(drivers = TRUE) diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index 238d03d..8eddcc9 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -57,9 +57,9 @@ tar_terra_vect <- function(name, storage = targets::tar_option_get("storage"), retrieval = targets::tar_option_get("retrieval"), cue = targets::tar_option_get("cue")) { - if (!requireNamespace("terra")) { - stop("package 'terra' is required", call. = FALSE) - } + + check_pkg_installed("terra") + name <- targets::tar_deparse_language(substitute(name)) envir <- targets::tar_option_get("envir") @@ -122,9 +122,7 @@ tar_terra_vect <- function(name, #' @noRd create_format_terra_vect <- function(filetype, options, ...) { - if (!requireNamespace("terra")) { - stop("package 'terra' is required", call. = FALSE) - } + check_pkg_installed("terra") # get list of drivers available for writing depending on what the user's GDAL supports drv <- terra::gdal(drivers = TRUE) @@ -161,9 +159,7 @@ create_format_terra_vect <- function(filetype, options, ...) { #' @noRd create_format_terra_vect_shz <- function(options, ...) { - if (!requireNamespace("terra")) { - stop("package 'terra' is required", call. = FALSE) - } + check_pkg_installed("terra") .write_terra_vector <- eval(substitute(function(object, path) { terra::writeVector( diff --git a/R/utils.R b/R/utils.R index c1dd32b..8494296 100644 --- a/R/utils.R +++ b/R/utils.R @@ -4,3 +4,12 @@ geotargets_repair_option_name <- function(option_name) { option_name <- paste0("geotargets.", option_name) } } + +check_pkg_installed <- function(pkg, call = rlang::caller_env()) { + if (!requireNamespace(pkg, quietly = TRUE)) { + cli::cli_abort( + message = "package {.pkg {pkg}} is required", + call = call + ) + } +} From 5c9e677315e00f620f8adf0c5726dd5474e7bef7 Mon Sep 17 00:00:00 2001 From: njtierney Date: Fri, 22 Mar 2024 12:56:30 +1000 Subject: [PATCH 10/11] add cli --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 9cf8ed3..b9560e3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,7 +33,8 @@ Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.1 Imports: targets, - rlang + rlang, + cli Suggests: terra, testthat (>= 3.0.0) From db7bf61782eff44eb518d164146c8f0ec5a8b512 Mon Sep 17 00:00:00 2001 From: njtierney Date: Fri, 22 Mar 2024 13:49:45 +1000 Subject: [PATCH 11/11] add function to get gdal available driver list - specifying either "vector" or "raster" (currently). This removes a little bit of copied complexity in a few places in the pkg --- R/tar-terra-rast.R | 8 ++------ R/tar-terra-vect.R | 8 ++------ R/utils.R | 7 +++++++ 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index 089cf2e..ffd01b1 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -67,9 +67,7 @@ tar_terra_rast <- function(name, tidy_eval = tidy_eval ) - # get list of drivers available for writing depending on what the user's GDAL supports - drv <- terra::gdal(drivers = TRUE) - drv <- drv[drv$type == "raster" & grepl("write", drv$can), ] + 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") @@ -106,9 +104,7 @@ create_format_terra_raster <- function(filetype, gdal, ...) { check_pkg_installed("terra") - # get list of drivers available for writing depending on what the user's GDAL supports - drv <- terra::gdal(drivers = TRUE) - drv <- drv[drv$type == "raster" & grepl("write", drv$can), ] + drv <- get_gdal_available_driver_list("raster") filetype <- filetype %||% geotargets_option_get("gdal.raster.driver") filetype <- rlang::arg_match0(filetype, drv$name) diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index 8eddcc9..b3441c7 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -75,9 +75,7 @@ tar_terra_vect <- function(name, tidy_eval = tidy_eval ) - # get list of drivers available for writing depending on what the user's GDAL supports - drv <- terra::gdal(drivers = TRUE) - drv <- drv[drv$type == "vector" & grepl("write", drv$can), ] + drv <- get_gdal_available_driver_list("vector") # if not specified by user, pull the corresponding geotargets option filetype <- filetype %||% geotargets_option_get("gdal.vector.driver") @@ -124,9 +122,7 @@ create_format_terra_vect <- function(filetype, options, ...) { check_pkg_installed("terra") - # get list of drivers available for writing depending on what the user's GDAL supports - drv <- terra::gdal(drivers = TRUE) - drv <- drv[drv$type == "vector" & grepl("write", drv$can), ] + drv <- get_gdal_available_driver_list("vector") if (is.null(filetype)) { filetype <- "GeoJSON" diff --git a/R/utils.R b/R/utils.R index 8494296..9d21a94 100644 --- a/R/utils.R +++ b/R/utils.R @@ -13,3 +13,10 @@ check_pkg_installed <- function(pkg, call = rlang::caller_env()) { ) } } + +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), ] + drv +}