geotargets
extends targets to work with geospatial data formats, such
as rasters and vectors (e.g., shapefiles).
A relatively common gotcha moment when using popular libraries like
terra
with targets is running into erros with read and write. Due to
the limitations that come with the underlying C++ implementation in the
terra
library, there are specific ways to write and read these
objects. See ?terra
for details. geotargets
helps handle these write
and read steps, so you don’t have to worry about them and can use
targets as you are used to.
In essence, if you’ve ever come across the error:
Error in .External(list(name = "CppMethod__invoke_notvoid", address = <pointer: 0x0>, :
NULL value passed as symbol address
or
Error: external pointer is not valid
When trying to read in a geospatial raster or vector in targets, then this is for you :)
You can install the development version of geotargets like so:
install.packages("geotargets", repos = c("https://njtierney.r-universe.dev", "https://cran.r-project.org"))
geotargets
is still undergoing development, and we would love for
people to use the package to kick the tyres. We are using it in our own
work, but want users to know that the API could change in subtle or
breaking ways.
Below we show three examples of target factories:
tar_terra_rast()
tar_terra_vect()
tar_terra_sprc()
You would use these in place of tar_target()
in your targets pipeline,
when you are doing work with terra raster or terra vector data.
It is a bit tricky to implement targets workflows in a README, but if you would like to see and download working examples for yourself, see the repo, demo-geotargets.
library(targets)
tar_dir({ # tar_dir() runs code from a temporary directory.
tar_script({
library(targets)
library(geotargets)
list(
tar_terra_rast(
terra_rast_example,
system.file("ex/elev.tif", package = "terra") |> terra::rast()
)
)
})
tar_make()
x <- tar_read(terra_rast_example)
x
})
tar_dir({ # tar_dir() runs code from a temporary directory.
tar_script({
library(geotargets)
lux_area <- function(projection = "EPSG:4326") {
terra::project(
terra::vect(system.file("ex", "lux.shp",
package = "terra"
)),
projection
)
}
list(
tar_terra_vect(
terra_vect_example,
lux_area()
)
)
})
tar_make()
x <- tar_read(terra_vect_example)
x
})
targets::tar_dir({ # tar_dir() runs code from a temporary directory.
library(geotargets)
targets::tar_script({
elev_scale <- function(z = 1, projection = "EPSG:4326") {
terra::project(
terra::rast(system.file("ex", "elev.tif", package = "terra")) * z,
projection
)
}
list(
tar_terra_sprc(
raster_elevs,
# two rasters, one unaltered, one scaled by factor of 2 and
# reprojected to interrupted good homolosine
command = terra::sprc(list(
elev_scale(1),
elev_scale(2, "+proj=igh")
))
)
)
})
targets::tar_make()
x <- targets::tar_read(raster_elevs)
})
Please note that the geotargets project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.