Skip to content

Commit

Permalink
Merge pull request #35 from njtierney/terra-suggests-eric
Browse files Browse the repository at this point in the history
Moves `terra` to Suggests
  • Loading branch information
Aariq committed Mar 22, 2024
2 parents e24a4f1 + db7bf61 commit 3429b79
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
7 changes: 3 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ Language: en-GB
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Imports:
rlang,
targets,
terra
rlang,
cli
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
14 changes: 8 additions & 6 deletions R/tar-terra-rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ tar_terra_rast <- function(name,
retrieval = targets::tar_option_get("retrieval"),
cue = targets::tar_option_get("cue")) {

check_pkg_installed("terra")

name <- targets::tar_deparse_language(substitute(name))

envir <- targets::tar_option_get("envir")
Expand All @@ -65,8 +67,12 @@ 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(
Expand Down Expand Up @@ -96,13 +102,9 @@ 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)
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)
Expand Down
19 changes: 10 additions & 9 deletions R/tar-terra-vect.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +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")) {

check_pkg_installed("terra")

name <- targets::tar_deparse_language(substitute(name))

envir <- targets::tar_option_get("envir")
Expand All @@ -72,8 +75,12 @@ tar_terra_vect <- function(name,
tidy_eval = tidy_eval
)

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")
filetype <- rlang::arg_match0(filetype, drv$name)

gdal <- gdal %||% geotargets_option_get("gdal.vector.creation_options")

format <- ifelse(
Expand Down Expand Up @@ -113,13 +120,9 @@ 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)
drv <- drv[drv$type == "vector" & grepl("write", drv$can), ]
drv <- get_gdal_available_driver_list("vector")

if (is.null(filetype)) {
filetype <- "GeoJSON"
Expand Down Expand Up @@ -152,9 +155,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(
Expand Down
16 changes: 16 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,19 @@ 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
)
}
}

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
}

0 comments on commit 3429b79

Please sign in to comment.