Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
Merge commit 'e24a4f1852124e6523bef3d3a152804018e5f7b4'

#Conflicts:
#	R/tar-terra-rast.R
#	R/tar-terra-vect.R
  • Loading branch information
Aariq committed Mar 19, 2024
2 parents 7ced37c + e24a4f1 commit 9857e07
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 29 deletions.
8 changes: 1 addition & 7 deletions R/geotargets-option.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
unset = get_option(option_name, option_value, ";")
)
the_option <- strsplit(gdal_creation_options, ";")[[1]]
the_option
Expand Down Expand Up @@ -87,9 +87,3 @@ 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)
}
}
28 changes: 18 additions & 10 deletions R/tar-terra-rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,29 @@ tar_terra_rast <- function(name,
#' @param ... Additional arguments not yet used
#' @noRd
create_format_terra_raster <- function(filetype, gdal, ...) {
# 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) {

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")

.write_terra_raster <- eval(substitute(function(object, path) {
terra::writeRaster(
object,
path,
filetype = geotargets::geotargets_option_get("gdal.raster.driver"),
filetype = filetype,
overwrite = TRUE,
gdal = geotargets::geotargets_option_get("gdal.raster.creation_options")
gdal = gdal
)
}

body(.write_terra_raster)[[2]][["filetype"]] <- filetype
body(.write_terra_raster)[[2]][["gdal"]] <- gdal
}, list(filetype = filetype, gdal = gdal)))

targets::tar_format(
read = function(path) terra::rast(path),
Expand Down
37 changes: 27 additions & 10 deletions R/tar-terra-vect.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,30 @@ tar_terra_vect <- function(name,
#' @param ... Additional arguments not yet used
#' @noRd
create_format_terra_vect <- function(filetype, options, ...) {
.write_terra_vector <- function(object, path) {

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 <- eval(substitute(function(object, path) {
terra::writeVector(
object,
path,
filetype = NULL,
filetype = filetype,
overwrite = TRUE,
options = NULL
options = options
)
}
body(.write_terra_vector)[[2]][["filetype"]] <- filetype
body(.write_terra_vector)[[2]][["options"]] <- options
}, list(filetype = filetype, options = options)))

targets::tar_format(
read = function(path) terra::vect(path),
Expand All @@ -147,17 +160,21 @@ create_format_terra_vect <- function(filetype, options, ...) {
#' @param ... Additional arguments not yet used
#' @noRd
create_format_terra_vect_shz <- function(options, ...) {
.write_terra_vector <- function(object, path) {

if (!requireNamespace("terra")) {
stop("package 'terra' is required", call. = FALSE)
}

.write_terra_vector <- eval(substitute(function(object, path) {
terra::writeVector(
x = object,
filename = paste0(path, ".shz"),
filetype = "ESRI Shapefile",
overwrite = TRUE,
options = NULL
options = options
)
file.rename(paste0(path, ".shz"), path)
}
body(.write_terra_vector)[[2]][["options"]] <- options
}, list(options = options)))

targets::tar_format(
read = function(path) terra::vect(paste0("/vsizip/{", path, "}")),
Expand Down
6 changes: 6 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# helper for adding "geotargets." when options missing that.
geotargets_repair_option_name <- function(option_name) {
if (!startsWith(option_name, "geotargets.")) {
option_name <- paste0("geotargets.", option_name)
}
}
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ When trying to read in a geospatial raster or vector in targets, then this is fo
You can install the development version of geotargets like so:

``` r
remotes::install_github("njtierney/geotargets")
install.packages("geotargets", repos = c("https://njtierney.r-universe.dev", "https://cran.r-project.org"))
```

## A note on development
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ this is for you :)
You can install the development version of geotargets like so:

``` r
remotes::install_github("njtierney/geotargets")
install.packages("geotargets", repos = c("https://njtierney.r-universe.dev", "https://cran.r-project.org"))
```

## A note on development
Expand Down

0 comments on commit 9857e07

Please sign in to comment.