chopin
automatically distributes geospatial data
computation over multiple threads.chopin
workflowpar_pad_*
functions to par_grid
,
or running par_hierarchy
, or par_multirasters
functions at once.par_*
functions operate on
future
backends, users should define the future plan before
running the functions. multicore
plan supports
terra
objects which may lead to faster computation, but it
is not supported in Windows. An alternative is
future.mirai
’s mirai_multisession
plan, which
is supported in many platforms and generally faster than plain future
multisession plan.workers
argument should be defined with an integer
value to specify the number of threads to be used.extract_at
runs on the grid
polygons.## Reading layer `county' from data source
## `/tmp/RtmplqIWec/Rinst1f7675d5ba81/chopin/extdata/nc_hierarchy.gpkg'
## using driver `GPKG'
## Simple feature collection with 100 features and 1 field
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: 1054155 ymin: 1341756 xmax: 1838923 ymax: 1690176
## Projected CRS: NAD83 / Conus Albers
## Reading layer `tracts' from data source
## `/tmp/RtmplqIWec/Rinst1f7675d5ba81/chopin/extdata/nc_hierarchy.gpkg'
## using driver `GPKG'
## Simple feature collection with 2672 features and 1 field
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 1054155 ymin: 1341756 xmax: 1838923 ymax: 1690176
## Projected CRS: NAD83 / Conus Albers
px <-
par_hierarchy(
# from here the par_hierarchy-specific arguments
regions = nctrct,
regions_id = "GEOID",
length_left = 5,
pad = 10000,
pad_y = FALSE,
.debug = TRUE,
# from here are the dispatched function definition
# for parallel workers
fun_dist = extract_at,
# below should follow the arguments of the dispatched function
x = ncelev,
y = sf::st_as_sf(ncsamp),
id = "pid",
radius = 1e4,
func = "mean"
)
dim(px)
## [1] 10000 2
## pid mean
## 1 51 -10.539042
## 2 101 7.581138
## 3 122 15.151628
## 4 169 12.429558
## 5 277 7.064094
## 6 352 19.157986
## pid mean
## 9995 9630 -4.369706
## 9996 9644 4.831831
## 9997 9710 -4.033235
## 9998 9737 6.478515
## 9999 9871 -2.455561
## 10000 9981 8.212685
ncelev <-
system.file("extdata/nc_srtm15_otm.tif", package = "chopin")
ncelev <- terra::rast(ncelev)
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)
terra::writeRaster(ncelev, file.path(tdir, "test4.tif"), overwrite = TRUE)
terra::writeRaster(ncelev, file.path(tdir, "test5.tif"), overwrite = TRUE)
rasts <- list.files(tdir, pattern = "tif$", full.names = TRUE)
pm <-
par_multirasters(
filenames = rasts,
fun_dist = extract_at,
x = NA,
y = sf::st_as_sf(ncsamp)[1:500, ],
id = "pid",
radius = 1e4,
func = "mean",
.debug = TRUE
)
dim(pm)
## [1] 2500 2
## mean base_raster
## 1 1126.963257 /tmp/Rtmpl89J2a/test1.tif
## 2 193.032501 /tmp/Rtmpl89J2a/test1.tif
## 3 642.440857 /tmp/Rtmpl89J2a/test1.tif
## 4 1035.783447 /tmp/Rtmpl89J2a/test1.tif
## 5 6.199244 /tmp/Rtmpl89J2a/test1.tif
## 6 826.078552 /tmp/Rtmpl89J2a/test1.tif
## mean base_raster
## 2495 9.272566 /tmp/Rtmpl89J2a/test5.tif
## 2496 222.980743 /tmp/Rtmpl89J2a/test5.tif
## 2497 -9.296117 /tmp/Rtmpl89J2a/test5.tif
## 2498 625.492615 /tmp/Rtmpl89J2a/test5.tif
## 2499 143.216751 /tmp/Rtmpl89J2a/test5.tif
## 2500 661.181885 /tmp/Rtmpl89J2a/test5.tif
chopin
works best with two-dimensional
(planar) geometries. Users should disable
s2
spherical geometry mode in sf
by setting
sf::sf_use_s2(FALSE)
. Running any chopin
functions at spherical or three-dimensional (e.g., including M/Z
dimensions) geometries may produce incorrect or unexpected results.