Skip to contents

Large raster files usually exceed the memory capacity in size. This function can be helpful to process heterogenous raster files with homogeneous summary functions. Heterogenous raster files refer to rasters with different spatial extents and resolutions. Cropping a large raster into a small subset even consumes a lot of memory and adds processing time. This function leverages terra SpatRaster proxy to distribute computation jobs over multiple cores. It is assumed that users have multiple large raster files in their disk, then each file path is assigned to a thread. Each thread will directly read raster values from the disk using C++ pointers that operate in terra functions. For use, it is strongly recommended to use vector data with small and confined spatial extent for computation to avoid out-of-memory error. For this, users may need to make subsets of input vector objects in advance.

Usage

par_multirasters(filenames, debug = FALSE, fun_dist, ...)

Arguments

filenames

character(n). A vector or list of full file paths of raster files. n is the total number of raster files.

debug

logical(1). Default is FALSE. If a unit computation fails, the error message and the file path where the error occurred will be included in the output.

fun_dist

sf, terra, or chopin functions.

...

Arguments passed to the argument fun_dist. The second place should get a vector or raster dataset from which you want to extract or calculate values. For example, a raster dataset when vector-raster overlay is performed.

Value

a data.frame object with computation results. For entries of the results, consult the function used in fun_dist argument.

Author

Insang Song geoissong@gmail.com

Examples

if (FALSE) {
library(terra)
library(sf)
library(chopin)
library(future)
library(doFuture)
sf::sf_use_s2(FALSE)
registerDoFuture()
plan(multicore)

ncpath <- system.file("extdata/nc_hierarchy.gpkg", package = "chopin")
nccnty <- terra::vect(ncpath, layer = "county")
ncelev <-
  terra::unwrap(
    readRDS(
      system.file("extdata/nc_srtm15_otm.rds", package = "chopin")
    )
  )
terra::crs(ncelev) <- "EPSG:5070"
names(ncelev) <- c("srtm15")
tdir <- tempdir(check = TRUE)
terra::writeRaster(ncelev, file.path(tdir, "test1.tif"), overwrite = TRUE)
terra::writeRaster(ncelev, file.path(tdir, "test2.tif"), overwrite = TRUE)
terra::writeRaster(ncelev, file.path(tdir, "test3.tif"), overwrite = TRUE)
testfiles <- list.files(tdir, pattern = "tif$", full.names = TRUE)

res <- par_multirasters(
  filenames = testfiles,
  fun_dist = extract_at_poly,
  polys = nccnty,
  surf = ncelev,
  id = "GEOID",
  func = "mean"
)
}