-
Notifications
You must be signed in to change notification settings - Fork 4
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
Facilitate targets that are a list of geospatial rasters #3
Comments
I was hoping that format_geotiffs <- tar_format(
read = function(path) purrr::map(path, terra::rast),
write = function(object, path) {
purrr::map2(
.x = object,
.y = path,
.f = function(x, y) {
terra::writeRaster(
x = x,
filename = y,
filetype = "GTiff",
overwrite = TRUE
)
}
)
},
marshal = function(object) purrr::map(object, terra::wrap),
unmarshal = function(object) purrr::map(object, terra::unwrap)
) would work but it only returns the first element in the list when loading it :( |
If the rasters are conformal (i.e. same resolution, extent, CRS) then it would probably be best to call If the rasters have different resolution, extent, or CRS, you can create a SpatRasterCollection object with targets::tar_script({
library(terra)
library(targets)
elev_scale <- function(z = 1, projection = "EPSG:4326") {
terra::project(terra::rast(system.file("ex", "elev.tif", package = "terra")) * z,
projection)
}
format_sprc_geotiff <- tar_format(
read = function(path) terra::sprc(path),
write = function(object, path) {
for (i in seq(object)) {
if (i > 1) {
opt <- "APPEND_SUBDATASET=YES"
} else opt <- ""
terra::writeRaster(
x = object[i],
filename = path,
filetype = "GTiff",
overwrite = (i == 1),
gdal = opt
)
}
},
marshal = function(object) terra::wrap(object),
unmarshal = function(object) terra::unwrap(object)
)
tar_target(
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"))),
format = format_sprc_geotiff
)
})
targets::tar_make()
#> terra 1.7.73
#> ▶ dispatched target raster_elevs
#> ● completed target raster_elevs [0.177 seconds]
#> ▶ completed pipeline [0.33 seconds]
#> Warning message:
#> [rast] skipped sub-datasets (see 'describe(sds=TRUE)'):
#> /tmp/RtmpX6R4Zb/reprex-26a50397dc957-flat-ram/_targets/scratch/raster_elevs
targets::tar_load("raster_elevs")
raster_elevs[1]
#> class : SpatRaster
#> dimensions : 90, 95, 1 (nrow, ncol, nlyr)
#> resolution : 0.008333333, 0.008333333 (x, y)
#> extent : 5.741667, 6.533333, 49.44167, 50.19167 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> source : raster_elevs
#> name : elevation
#> min value : 141
#> max value : 547
raster_elevs[2]
#> class : SpatRaster
#> dimensions : 115, 114, 1 (nrow, ncol, nlyr)
#> resolution : 683.4048, 683.4048 (x, y)
#> extent : 1480982, 1558890, 5478149, 5556741 (xmin, xmax, ymin, ymax)
#> coord. ref. : +proj=igh +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
#> source : raster_elevs
#> name : elevation
#> min value : 282.8344
#> max value : 1087.3800 Created on 2024-03-03 with reprex v2.0.2 |
The other option would be dynamic branching that creates a downstream target for each element of |
Thanks for providing examples here @brownag ! I am new to spatial stuff and didn't know that you could combine rasters like that. That's really neat. Looking forward to making a nice rich set of vignettes to demonstrate all these examples! |
Confirmed that dynamic branching does not work "out of the box". An example (not reproducible, sorry, will investigate reprex later) tar_plan(
years = 2022:2023,
tar_target(
name = prism_tmean,
command = get_prism_tmean(years),
pattern = map(years),
format = "file"
),
tar_terra_rast(
gdd_doy,
calc_gdd_doy(dir = prism_tmean, gdd_threshold = 200),
pattern = map(prism_tmean)
)
) This pipeline runs without error, but > c(tar_read(gdd_doy_1dda6797), tar_read(gdd_doy_14d2965f))
class : SpatRaster
dimensions : 621, 1405, 2 (nrow, ncol, nlyr)
resolution : 0.04166667, 0.04166667 (x, y)
extent : -125.0208, -66.47917, 24.0625, 49.9375 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat NAD83 (EPSG:4269)
sources : gdd_doy_1dda6797
gdd_doy_14d2965f
names : 2022, 2023
min values : 8, 9
max values : 214, 211 As a workaround, I can use This kind of stuff should at least be in a vignette. |
I've now run into a situation where I have a large multi-layer raster (1.4GB) that doesn't seem to download correctly when S3 bucket storage is used. It would be nice if there was a frictionless way of optionally saving each layer as a separate target (i.e. file in the _targets/ store) with dynamic branching. |
i've just used the |
For example, in:
Created on 2024-03-04 with reprex v2.1.0
Session info
The text was updated successfully, but these errors were encountered: