## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set(warning = FALSE, message = FALSE) ## ----------------------------------------------------------------------------- library(chopin) library(terra) library(sf) library(collapse) library(dplyr) library(future) library(future.mirai) library(future.apply) ## ----------------------------------------------------------------------------- temp_dir <- tempdir(check = TRUE) url_nccnty <- paste0( "https://raw.githubusercontent.com/", "ropensci/chopin/refs/heads/main/", "tests/testdata/nc_hierarchy.gpkg" ) url_ncelev <- paste0( "https://raw.githubusercontent.com/", "ropensci/chopin/refs/heads/main/", "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) ## ----------------------------------------------------------------------------- ncsamp <- terra::spatSample( nccnty, 1e4L ) ncsamp$pid <- seq_len(nrow(ncsamp)) ## ----------------------------------------------------------------------------- ncgrid <- par_pad_grid(ncsamp, mode = "grid", nx = 4L, ny = 2L, padding = 10000) plot(ncgrid$original) ## ----------------------------------------------------------------------------- future::plan(future.mirai::mirai_multisession, workers = 2L) ## ----------------------------------------------------------------------------- pg <- par_grid( grids = ncgrid, pad_y = FALSE, .debug = TRUE, fun_dist = extract_at, x = ncelev_path, y = sf::st_as_sf(ncsamp), id = "pid", radius = 1e4, func = "mean" ) ## ----------------------------------------------------------------------------- nccnty <- sf::st_read(nccnty_path, layer = "county") nctrct <- sf::st_read(nccnty_path, layer = "tracts") ## ----------------------------------------------------------------------------- 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) head(px) tail(px) ## ----------------------------------------------------------------------------- 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) head(pm) tail(pm)