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

Add tar_stars() #33

Merged
merged 28 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
50ad089
Add `tar_stars()`
brownag Mar 15, 2024
0e20c17
Add `tar_stars_proxy()`
brownag Mar 16, 2024
980c291
fix typo in test-tar-terra.R
brownag Mar 16, 2024
78bdee5
rlang importFrom is now in geotargets-package.R
brownag Mar 16, 2024
bcd64a7
clean up unneeded addition
brownag Mar 16, 2024
56f8222
Merge branch 'master' into tar-stars
brownag Mar 16, 2024
343a3fe
Merge branch 'master' into tar-stars
brownag Mar 16, 2024
f74fe91
tar_stars: Add support for storage of arbitrary multidimensional arrays
brownag Mar 16, 2024
948a2e6
Better tar_format function building following #34, #43
brownag Mar 19, 2024
583dca3
Merge branch 'master' into tar-stars
brownag Mar 20, 2024
23e2b59
Merge branch 'master' into tar-stars
brownag Apr 27, 2024
29ff1f7
Update tar-stars.R
brownag Apr 27, 2024
6036bc4
Merge branch 'tar-stars' of ssh://github.com/brownag/geotargets into …
brownag Apr 27, 2024
a1be44c
Docs
brownag Apr 27, 2024
fb2b99a
cleanup duplicate
brownag Apr 27, 2024
a1841bd
Update R/tar-stars.R
brownag Apr 30, 2024
6cfaa20
Docs
brownag Apr 30, 2024
443f88f
tar_stars cleanup
brownag Apr 30, 2024
920b307
Merge branch 'master' into tar-stars
brownag May 3, 2024
4a062b9
tar_stars: more cleanup after merge
brownag May 3, 2024
eee3fac
update NAMESPACE
brownag May 3, 2024
dbd7343
Update README
brownag May 3, 2024
946e06b
Update R/tar-stars.R
brownag May 11, 2024
1ad5242
Merge branch 'master' into tar-stars
brownag May 11, 2024
a757a45
Update NEWS.md
brownag May 11, 2024
2f26920
comma
brownag May 11, 2024
c0767b0
merge main
Aariq May 29, 2024
bc22086
document()
Aariq May 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ Imports:
Suggests:
crew,
terra,
sf,
stars,
ncmeta,
testthat (>= 3.0.0),
withr
Config/testthat/edition: 3
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

export(geotargets_option_get)
export(geotargets_option_set)
export(tar_stars)
export(tar_stars_proxy)
export(tar_terra_rast)
export(tar_terra_sprc)
export(tar_terra_vect)
importFrom(rlang,"%||%")
importFrom(rlang,arg_match0)
importFrom(utils,globalVariables)
2 changes: 0 additions & 2 deletions R/geotargets-option.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#' `geotargets_options_set(gdal_raster_driver = "GTiff")` is equivalent to
#' `options("geotargets.gdal.raster.driver" = "GTiff")`.
#'
#'
#' @rdname geotargets-options
#' @export
geotargets_option_set <- function(
Expand All @@ -36,7 +35,6 @@ geotargets_option_set <- function(
gdal_vector_driver = NULL,
gdal_vector_creation_options = NULL
) {

options(
"geotargets.gdal.raster.driver" = gdal_raster_driver %||% geotargets_option_get("gdal.raster.driver"),
"geotargets.gdal.raster.creation.options" = gdal_raster_creation_options %||% geotargets_option_get("gdal.raster.creation.options"),
Expand Down
1 change: 1 addition & 0 deletions R/geotargets-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ NULL

# clear CRAN checks spotting floating global variables
#' @importFrom utils globalVariables
#' @importFrom rlang %||%
utils::globalVariables(
c(
"packages"
Expand Down
283 changes: 283 additions & 0 deletions R/tar-stars.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
#' Create a stars _stars_ Target
#'
#' Provides a target format for stars objects.
#'
#' @param driver character. File format expressed as GDAL driver names passed to [stars::write_stars()]. See [sf::st_drivers()].
#' @param options character. GDAL driver specific datasource creation options passed to [stars::write_stars()]
brownag marked this conversation as resolved.
Show resolved Hide resolved
#' @param proxy logical. Passed to [stars::read_stars()]. If `TRUE` the target will be read as an object of class `stars_proxy`. Otherwise, the object is class `stars`.
#' @param mdim logical. Use the [Multidimensional Raster Data Model](https://gdal.org/user/multidim_raster_data_model.html) via [stars::write_mdim()]? Default: `FALSE`. Only supported for some drivers, e.g. `"netCDF"` or `"Zarr"`.
#' @param ncdf logical. Use the NetCDF library directly to read data via [stars::read_ncdf()]? Default: `FALSE`. Only supported for `driver="netCDF"`.
#' @param ... Additional arguments not yet used
#'
#' @inheritParams targets::tar_target
#' @seealso [targets::tar_target_raw()]
#' @export
#' @examplesIf rlang::is_installed("stars")
#' if (Sys.getenv("TAR_LONG_EXAMPLES") == "true") {
#' targets::tar_dir({ # tar_dir() runs code from a temporary directory.
#' library(geotargets)
#' targets::tar_script({
#' list(
#' geotargets::tar_stars(
#' stars_example,
#' stars::read_stars(system.file("tif", "olinda_dem_utm25s.tif", package = "stars"))
#' )
#' )
#' })
#' targets::tar_make()
#' x <- targets::tar_read(stars_example)
#' })
#'}
tar_stars <- function(name,
command,
pattern = NULL,
proxy = FALSE,
mdim = FALSE,
ncdf = FALSE,
driver = geotargets_option_get("gdal.raster.driver"),
options = geotargets_option_get("gdal.raster.creation.options"),
...,
tidy_eval = targets::tar_option_get("tidy_eval"),
packages = targets::tar_option_get("packages"),
library = targets::tar_option_get("library"),
repository = targets::tar_option_get("repository"),
iteration = targets::tar_option_get("iteration"),
error = targets::tar_option_get("error"),
memory = targets::tar_option_get("memory"),
garbage_collection = targets::tar_option_get("garbage_collection"),
deployment = targets::tar_option_get("deployment"),
priority = targets::tar_option_get("priority"),
resources = targets::tar_option_get("resources"),
storage = targets::tar_option_get("storage"),
retrieval = targets::tar_option_get("retrieval"),
cue = targets::tar_option_get("cue")) {

check_pkg_installed("stars")

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

envir <- targets::tar_option_get("envir")

command <- targets::tar_tidy_eval(
expr = as.expression(substitute(command)),
envir = envir,
tidy_eval = tidy_eval
)

pattern <- targets::tar_tidy_eval(
expr = as.expression(substitute(pattern)),
envir = envir,
tidy_eval = tidy_eval
)

tar_stars_raw(
name = name,
command = command,
pattern = pattern,
proxy = proxy,
mdim = mdim,
ncdf = ncdf,
driver = driver,
options = options,
packages = packages,
library = library,
repository = repository,
iteration = iteration,
error = error,
memory = memory,
garbage_collection = garbage_collection,
deployment = deployment,
priority = priority,
resources = resources,
storage = storage,
retrieval = retrieval,
cue = cue
)
}

#' @export
#' @rdname tar_stars
tar_stars_proxy <- function(name,
command,
pattern = NULL,
mdim = FALSE,
ncdf = FALSE,
driver = geotargets_option_get("gdal.raster.driver"),
options = geotargets_option_get("gdal.raster.creation.options"),
...,
tidy_eval = targets::tar_option_get("tidy_eval"),
packages = targets::tar_option_get("packages"),
library = targets::tar_option_get("library"),
repository = targets::tar_option_get("repository"),
iteration = targets::tar_option_get("iteration"),
error = targets::tar_option_get("error"),
memory = targets::tar_option_get("memory"),
garbage_collection = targets::tar_option_get("garbage_collection"),
deployment = targets::tar_option_get("deployment"),
priority = targets::tar_option_get("priority"),
resources = targets::tar_option_get("resources"),
storage = targets::tar_option_get("storage"),
retrieval = targets::tar_option_get("retrieval"),
cue = targets::tar_option_get("cue")) {

check_pkg_installed("stars")

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

envir <- targets::tar_option_get("envir")

command <- targets::tar_tidy_eval(
expr = as.expression(substitute(command)),
envir = envir,
tidy_eval = tidy_eval
)

pattern <- targets::tar_tidy_eval(
expr = as.expression(substitute(pattern)),
envir = envir,
tidy_eval = tidy_eval
)

tar_stars_raw(
name = name,
command = command,
pattern = pattern,
proxy = TRUE,
mdim = mdim,
ncdf = ncdf,
driver = driver,
options = options,
...,
packages = packages,
library = library,
repository = repository,
iteration = iteration,
error = error,
memory = memory,
garbage_collection = garbage_collection,
deployment = deployment,
priority = priority,
resources = resources,
storage = storage,
retrieval = retrieval,
cue = cue
)
}


#' tar_stars method with no tidy eval etc.
#' @noRd
tar_stars_raw <- function(name,
command,
pattern = NULL,
proxy,
mdim = FALSE,
ncdf = FALSE,
driver = geotargets_option_get("gdal.raster.driver"),
options = geotargets_option_get("gdal.raster.creation.options"),
...,
tidy_eval = targets::tar_option_get("tidy_eval"),
packages = targets::tar_option_get("packages"),
library = targets::tar_option_get("library"),
repository = targets::tar_option_get("repository"),
iteration = targets::tar_option_get("iteration"),
error = targets::tar_option_get("error"),
memory = targets::tar_option_get("memory"),
garbage_collection = targets::tar_option_get("garbage_collection"),
deployment = targets::tar_option_get("deployment"),
priority = targets::tar_option_get("priority"),
resources = targets::tar_option_get("resources"),
storage = targets::tar_option_get("storage"),
retrieval = targets::tar_option_get("retrieval"),
cue = targets::tar_option_get("cue")) {

driver <- driver %||% "GTiff"
options <- options %||% character(0)

# get drivers available for writing (depends on user's GDAL build)
drv <- sf::st_drivers(what = "raster")
drv <- drv[drv$write, ]

driver <- rlang::arg_match0(driver, drv$name)

.read_stars <- eval(substitute(
function(path) {

## TODO: it appears envvar custom resources do not work in read function?
READ_FUN <- stars::read_stars
# mdim <- as.logical(Sys.getenv("GEOTARGETS_GDAL_RASTER_MDIM", unset = FALSE))
if (mdim) {
READ_FUN <- stars::read_mdim
}

# ncdf <- as.logical(Sys.getenv("GEOTARGETS_USE_NCMETA", unset = FALSE))
if (ncdf && requireNamespace("ncmeta")) {
READ_FUN <- stars::read_ncdf
}

# proxy <- as.logical(Sys.getenv("GEOTARGETS_PROXY", unset = FALSE))
READ_FUN(path, proxy = proxy)

}, list(ncdf = ncdf, mdim = mdim, proxy = proxy)))

# TODO: should multidimensional array use the same `options` as 2D?
.write_stars <- function(object, path) {

WRITE_FUN <- stars::write_stars

mdim <- as.logical(Sys.getenv("GEOTARGETS_GDAL_RASTER_MDIM",
unset = FALSE))
if (mdim) {
WRITE_FUN <- stars::write_mdim
}

dr <- Sys.getenv("GEOTARGETS_GDAL_RASTER_DRIVER")

# stars requires character(0), not "", for no options set
co <- Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS")
co2 <- strsplit(co, ";")[[1]]

WRITE_FUN(
object,
path,
overwrite = TRUE,
driver = dr,
options = co
)
}

targets::tar_target_raw(
name = name,
command = command,
pattern = pattern,
packages = packages,
library = library,
format = targets::tar_format(
read = .read_stars,
write = .write_stars,
marshal = function(object) object, # Not currently used
unmarshal = function(object) object
),
repository = repository,
iteration = iteration,
error = error,
memory = memory,
garbage_collection = garbage_collection,
deployment = deployment,
priority = priority,
resources = targets::tar_resources(
custom_format = targets::tar_resources_custom_format(
#these envvars are used in read and write functions of format
envvars = c("GEOTARGETS_GDAL_RASTER_DRIVER" = driver,
"GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS" =
paste0(options, collapse = ";"),
"GEOTARGETS_GDAL_RASTER_MDIM" = mdim,
"GEOTARGETS_PROXY" = proxy,
"GEOTARGETS_USE_NCMETA" = ncdf)
)
),
brownag marked this conversation as resolved.
Show resolved Hide resolved
storage = storage,
retrieval = retrieval,
cue = cue
)
}
1 change: 0 additions & 1 deletion R/tar-terra-rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#' @param ... Additional arguments not yet used
#'
#' @inheritParams targets::tar_target
#' @importFrom rlang %||% arg_match0
#' @seealso [targets::tar_target_raw()]
#' @export
#' @examples
Expand Down
Loading