par_grid {chopin} | R Documentation |
Parallelize spatial computation over the computational grids
Description
future::multicore, future::multisession, future::cluster
future.mirai::mirai_multisession in future::plan will parallelize
the work in each grid. For details of the terminology in future
package,
refer to future::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, fun_dist, ..., pad_y = FALSE, .debug = FALSE)
Arguments
grids |
List of two sf/SpatVector objects. Computational grids.
It takes a strict assumption that the grid input is
an output of |
fun_dist |
|
... |
Arguments passed to the argument |
pad_y |
logical(1). Whether to filter y with the padded grid.
Should be TRUE when x is where the values are calculated.
Default is |
.debug |
logical(1). Default is |
Value
a data.frame object with computation results.
For entries of the results, consult the documentation of the function put
in fun_dist
argument.
Note
In dynamic dots (...
), fun_dist
arguments should include
x and y where sf/terra class objects or file paths are accepted.
Virtually any sf/terra functions that accept two arguments
can be put in fun_dist
; however, be advised that
some spatial operations do not necessarily give the
exact result from what would have been done with one 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(s)
Insang Song geoissong@gmail.com
See Also
future::multisession
, future::multicore
, future::cluster
,
future.mirai::mirai_multisession
, future::plan
, par_convert_f
Other Parallelization:
par_cut_coords()
,
par_grid_mirai()
,
par_hierarchy()
,
par_hierarchy_mirai()
,
par_make_grid()
,
par_merge_grid()
,
par_multirasters()
,
par_multirasters_mirai()
,
par_pad_balanced()
,
par_pad_grid()
,
par_split_list()
Examples
lastpar <- par(mfrow = c(1, 1))
library(sf)
library(future)
library(future.mirai)
options(sf_use_s2 = FALSE)
plan(mirai_multisession, workers = 2)
ncpath <- system.file("shape/nc.shp", package = "sf")
ncpoly <- sf::st_read(ncpath)
ncpoly <- sf::st_transform(ncpoly, "EPSG:5070")
# sf object
ncpnts <- sf::st_sample(ncpoly, 2000)
ncpnts <- sf::st_as_sf(ncpnts)
ncpnts$pid <- seq_len(nrow(ncpnts))
# file path
rrast <- terra::rast(ncpoly, nrow = 600, ncol = 1320)
terra::values(rrast) <- rgamma(7.92e5, 4, 2)
# Using raster path
rastpath <- file.path(tempdir(), "ncelev.tif")
terra::writeRaster(rrast, rastpath, overwrite = TRUE)
nccompreg <-
chopin::par_pad_grid(
input = ncpnts,
mode = "grid",
nx = 4L,
ny = 2L,
padding = 5e3L
)
res <-
par_grid(
grids = nccompreg,
fun_dist = extract_at,
x = rastpath,
y = ncpnts,
qsegs = 90L,
radius = 5e3L,
id = "pid"
)
future::plan(future::sequential)
mirai::daemons(0)
par(lastpar)