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 format creation method, and update tar_terra_rast() #15

Merged
merged 31 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0c71e4f
rename and fix
Aariq Mar 7, 2024
db770ad
rename and separate out format
Aariq Mar 7, 2024
6a64129
ignore project .Rprofile
Aariq Mar 7, 2024
b834996
add simple tests
Aariq Mar 7, 2024
2e93be8
add author
Aariq Mar 7, 2024
30edf4b
add clunky version of filetype arg
Aariq Mar 7, 2024
040b996
save filetype for a different PR
Aariq Mar 12, 2024
706526e
rename test
Aariq Mar 12, 2024
f2beefc
remove @import lines from package doc
Aariq Mar 12, 2024
ef4adfd
remove test
Aariq Mar 12, 2024
a2fa925
add license file
Aariq Mar 12, 2024
9d184f8
fix NOTE
Aariq Mar 12, 2024
c4b67b8
add test
Aariq Mar 12, 2024
de5a7fe
rename
Aariq Mar 12, 2024
6e181c6
rename shapefile fun
Aariq Mar 12, 2024
45de1f1
add missing args
Aariq Mar 12, 2024
bc92bbb
check() wants complete sentences
Aariq Mar 12, 2024
51d6ef9
merge main
Aariq Mar 12, 2024
e350ee5
added documentation and some examples for tar_terra_rast/vect
njtierney Mar 12, 2024
02a8753
depend on R 4.1.0, which comes with base pipe |>
njtierney Mar 12, 2024
f82edd4
implement shapefile method from @brownag https://github.com/njtierney…
njtierney Mar 12, 2024
0eb01c2
Add tests and snapshots
njtierney Mar 12, 2024
ac79682
ctb --> aut
njtierney Mar 12, 2024
c07d070
update pkg examples
njtierney Mar 12, 2024
fe9f45b
add _targets etc to gitignore and r build ignore
njtierney Mar 12, 2024
d70717b
tweak README
njtierney Mar 12, 2024
ef13606
namespace targets::tar_format
njtierney Mar 12, 2024
6a37d66
Add `create_format_terra_raster()` internal method for custom format …
brownag Mar 12, 2024
879efca
Update tar_terra_rast.Rd
brownag Mar 12, 2024
41b6550
Update tar_terra_vect.Rd
brownag Mar 12, 2024
14e9325
reorder
brownag Mar 12, 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
7 changes: 7 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@
^\.github$
^codecov\.yml$
^LICENSE\.md$

^_targets$
^_targets_r$
^_targets\.R$
^_targets_packages\.R$
^_targets\.Rmd$
^_targets\.yaml$
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@
.httr-oauth
.DS_Store
.quarto
.Rprofile

_targets_packages.R
_targets
_targets.R
_targets.Rmd
_targets_r
_targets.yaml
8 changes: 8 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,6 +36,8 @@ Imports:
terra
Suggests:
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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(tar_terra_rast)
export(tar_terra_vect)
importFrom(utils,globalVariables)
65 changes: 0 additions & 65 deletions R/tar-geotiff.R

This file was deleted.

86 changes: 0 additions & 86 deletions R/tar-shapefile.R

This file was deleted.

135 changes: 135 additions & 0 deletions R/tar-terra-rast.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#' Create a terra _SpatRaster_ Target
#'
#' Creates a target for a terra _SpatRaster_ object.
#'
#' @param filetype character. File format expressed as GDAL driver names passed to `terra::writeRaster()`
#' @param gdal character. GDAL driver specific datasource creation options passed to `terra::writeRaster()`
#' @param ... Additional arguments not yet used
#'
#' @inheritParams targets::tar_target
#'
#' @seealso [targets::tar_target_raw()]
#' @export
#' @examples
#' 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_terra_rast(
#' terra_rast_example,
#' system.file("ex/elev.tif", package = "terra") |> terra::rast()
#' )
#' )
#' })
#' targets::tar_make()
#' x <- targets::tar_read(terra_rast_example)
#' })
#'}
tar_terra_rast <- function(name,
command,
pattern = NULL,
filetype = NULL,
gdal = NULL,
...,
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")) {

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
)

# could pull defaults from geotargets package options
if (is.null(filetype)) {
filetype <- "GTiff"
}

if (is.null(gdal)) {
gdal <- "ENCODING=UTF-8"
}

targets::tar_target_raw(
name = name,
command = command,
pattern = pattern,
packages = packages,
library = library,
format = create_format_terra_raster(filetype = filetype, gdal = gdal, ...),
repository = repository,
iteration = iteration,
error = error,
memory = memory,
garbage_collection = garbage_collection,
deployment = deployment,
priority = priority,
resources = resources,
storage = storage,
retrieval = retrieval,
cue = cue
)
}

#' @param filetype File format expressed as GDAL driver names passed to `terra::writeRaster()`
#' @param gdal GDAL driver specific datasource creation options passed to `terra::writeRaster()`
#' @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), ]

if (is.null(filetype)) {
filetype <- "GTiff"
}
njtierney marked this conversation as resolved.
Show resolved Hide resolved

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

.write_terra_raster <- function(object, path) {
njtierney marked this conversation as resolved.
Show resolved Hide resolved
terra::writeRaster(
object,
path,
filetype = NULL,
overwrite = TRUE,
gdal = NULL
)
}
body(.write_terra_raster)[[2]][["filetype"]] <- filetype
body(.write_terra_raster)[[2]][["gdal"]] <- gdal
njtierney marked this conversation as resolved.
Show resolved Hide resolved

targets::tar_format(
read = function(path) terra::rast(path),
write = .write_terra_raster,
marshal = function(object) terra::wrap(object),
unmarshal = function(object) terra::unwrap(object)
)
}

Loading
Loading