Skip to contents

future::multicore, future::multisession, future::cluster will parallelize the work in each grid. For details of the terminology in future package, refer to plan. This function assumes that users have one raster file and a sizable and spatially distributed target locations. Each thread will process the nearest integer of $|N_g| / |N_t|$ grids where $|N_g|$ denotes the number of grids and $|N_t|$ denotes the number of threads.

Usage

par_grid(grids, grid_target_id = NULL, debug = FALSE, fun_dist, ...)

Arguments

grids

sf/SpatVector object. Computational grids. It takes a strict assumption that the grid input is an output of par_make_gridset. If missing or NULL is entered, par_make_gridset is internally called. In this case, the first element of the ellipsis argument ... is considered input in par_make_gridset and mode is fixed as "grid"`. See par_make_gridset() for details.

grid_target_id

character(1) or numeric(2). Default is NULL. If NULL, all grid_ids are used. "id_from:id_to" format or c(unique(grid_id)[id_from], unique(grid_id)[id_to])

debug

logical(1). Default is FALSE. Otherwise, if a unit computation fails, the error message and the CGRIDID value 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.

Note

In dynamic dots (...), the first and second arguments should be the fun_dist arguments where sf/SpatVector objects are accepted. Virtually any sf/terra functions that accept two arguments can be put in fun_dist, but please be advised that some spatial operations do not necessarily give the exact result from what would have been done single-thread. For example, distance calculated through this function may return the lower value than actual because the computational region was reduced. This would be the case especially where the target features are spatially sparsely distributed.

Author

Insang Song geoissong@gmail.com

Examples

if (FALSE) {
ncpath <- system.file("shape/nc.shp", package = "sf")
ncpoly <- terra::vect(ncpath) |>
  terra::project("EPSG:5070")
ncpnts <-
  readRDS(
    system.file("extdata/nc_random_point.rds", package = "chopin")
  )
ncpnts <- terra::vect(ncpnts)
ncpnts <- terra::project(ncpnts, "EPSG:5070")
ncelev <-
  terra::unwrap(
    readRDS(system.file("extdata/nc_srtm15_otm.rds", package = "chopin"))
  )
terra::crs(ncelev) <- "EPSG:5070"
names(ncelev) <- c("srtm15")

ncsamp <-
  terra::spatSample(
    terra::ext(ncelev),
    1e4L,
    lonlat = FALSE,
    as.points = TRUE
  )
ncsamp$kid <- sprintf("K-%05d", seq(1, nrow(ncsamp)))
nccompreg <-
  par_make_gridset(
    input = ncpnts,
    mode = "grid",
    nx = 6L,
    ny = 4L,
    padding = 3e4L
  )
res <-
  par_grid(
    grids = nccompreg,
    grid_target_id = NULL,
    fun_dist = extract_at_buffer,
    points = ncpnts,
    surf = ncelev,
    qsegs = 90L,
    radius = 5e3L,
    id = "pid"
  )
}