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.temp_dir <- tempdir(check = TRUE)
url_nccnty <-
paste0(
"https://raw.githubusercontent.com/",
"ropensci/chopin/refs/heads/0.9.0-cran/",
"tests/testdata/nc_hierarchy.gpkg"
)
url_ncelev <-
paste0(
"https://raw.githubusercontent.com/",
"ropensci/chopin/refs/heads/0.9.0-cran/",
"tests/testdata/nc_srtm15_otm.tif"
)
nccnty_path <- file.path(temp_dir, "nc_hierarchy.gpkg")
ncelev_path <- file.path(temp_dir, "nc_srtm15_otm.tif")
# download data
download.file(url_nccnty, nccnty_path, mode = "wb", quiet = TRUE)
download.file(url_ncelev, ncelev_path, mode = "wb", quiet = TRUE)
nccnty <- terra::vect(nccnty_path)
ncelev <- terra::rast(ncelev_path)
ncgrid <- par_pad_grid(ncsamp, mode = "grid", nx = 4L, ny = 2L, padding = 10000)
plot(ncgrid$original)
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/RtmpqPzA7Q/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/RtmpqPzA7Q/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 37 17.28892
## 2 66 1.50942
## 3 205 10.13954
## 4 226 10.26802
## 5 228 10.45569
## 6 356 18.44416
## pid mean
## 9995 9411 6.224306
## 9996 9527 7.061981
## 9997 9614 7.933888
## 9998 9682 8.719346
## 9999 9765 -4.467493
## 10000 9910 6.317059
ncelev <- terra::rast(ncelev_path)
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] 3000 2
## mean base_raster
## 1 16.108290 /tmp/RtmpqPzA7Q/nc_srtm15_otm.tif
## 2 848.958801 /tmp/RtmpqPzA7Q/nc_srtm15_otm.tif
## 3 305.764740 /tmp/RtmpqPzA7Q/nc_srtm15_otm.tif
## 4 9.908516 /tmp/RtmpqPzA7Q/nc_srtm15_otm.tif
## 5 143.033066 /tmp/RtmpqPzA7Q/nc_srtm15_otm.tif
## 6 16.845604 /tmp/RtmpqPzA7Q/nc_srtm15_otm.tif
## mean base_raster
## 2995 15.73794 /tmp/RtmpqPzA7Q/test5.tif
## 2996 834.72028 /tmp/RtmpqPzA7Q/test5.tif
## 2997 250.09413 /tmp/RtmpqPzA7Q/test5.tif
## 2998 25.30032 /tmp/RtmpqPzA7Q/test5.tif
## 2999 941.84381 /tmp/RtmpqPzA7Q/test5.tif
## 3000 30.31591 /tmp/RtmpqPzA7Q/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.