Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moves terra to Suggests #35

Merged
merged 12 commits into from
Mar 22, 2024
55 changes: 21 additions & 34 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]",
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", , "[email protected]", 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.
Aariq marked this conversation as resolved.
Show resolved Hide resolved
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,
terra
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
23 changes: 9 additions & 14 deletions R/tar-terra-rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ 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)
}
Aariq marked this conversation as resolved.
Show resolved Hide resolved
name <- targets::tar_deparse_language(substitute(name))

envir <- targets::tar_option_get("envir")
Expand All @@ -65,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(
Expand Down Expand Up @@ -95,20 +104,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), ]

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
Expand Down
29 changes: 9 additions & 20 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")) {
if (!requireNamespace("terra")) {
stop("package 'terra' is required", call. = FALSE)
}
Aariq marked this conversation as resolved.
Show resolved Hide resolved
name <- targets::tar_deparse_language(substitute(name))

envir <- targets::tar_option_get("envir")
Expand All @@ -72,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(
Expand Down Expand Up @@ -112,21 +121,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), ]

if (is.null(filetype)) {
filetype <- "GeoJSON"
}

filetype <- match.arg(filetype, drv$name)

.write_terra_vector <- function(object, path) {
terra::writeVector(
object,
Expand All @@ -153,11 +147,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,
Expand Down
Loading