terra targets integration #809
-
Thanks for creating The reprex below shows how This approach works, but seems wasteful if the raster tif file is very large (often several Gb) because a copy is made and stored. Do alternative approaches exist that do not have this drawback? library(targets)
#> Warning: package 'targets' was built under R version 4.1.3
tar_script({
library(terra)
example_tif_file <- function() {
system.file("ex/elev.tif", package = "terra")
}
create_raster <- function(rasterfile) {
rast(rasterfile)
}
create_packed_raster <- function(rr) {
wrap(rr)
}
list(tarchetypes::tar_file(example_tif, example_tif_file()),
tar_target(raster, create_raster(rasterfile = example_tif),
format = "qs"), tar_target(packed_raster, create_packed_raster(raster),
format = "qs"))
})
library(terra)
#> Warning: package 'terra' was built under R version 4.1.2
#> terra 1.5.21
tar_visnetwork()
#> terra 1.5.21
#> Warning messages:
#> 1: package 'targets' was built under R version 4.1.3
#> 2: package 'terra' was built under R version 4.1.2 tar_make()
#> terra 1.5.21
#> * start target example_tif
#> * built target example_tif
#> * start target raster
#> * built target raster
#> * start target packed_raster
#> * built target packed_raster
#> * end pipeline: 1.27 seconds
#> Warning messages:
#> 1: package 'targets' was built under R version 4.1.3
#> 2: package 'terra' was built under R version 4.1.2
tar_read(raster)
#> class : SpatRaster
#> Error in x@ptr$nrow(): external pointer is not valid
packed_raster <- tar_read(packed_raster)
rast(packed_raster)
#> 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 : memory
#> name : elevation
#> min value : 141
#> max value : 547 Created on 2022-03-16 by the reprex package (v2.0.1.9000) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 17 replies
-
If the large |
Beta Was this translation helpful? Give feedback.
If the large
tif
file is is an output image, I would suggest writing it once to disk and tracking it withformat = "file"
: https://books.ropensci.org/targets/data.html#external-files. If you use it as input data, you can still treat it as an input file (stillformat = "file"
, but only load the subset you need in the target that does the analysis. Maybe this involves writing a function that accepts the file path, creates aterra
pointer to it, only reads in the parts of the file it needs, and returns a small object. Otherwise, it's hard to get around copying the whole file, but can be made easier with a customtar_format()
format. The discussion at #805 is similar, the OP has a gist that d…