#' Extract values at points
#'
#' @param s Input RasterStack
#' @param points SpatialPoints* or sf-object containing the points at which to extract values
#' @param n_cores Number of cores used for parallel processing. Default is NULL, indicating no parallelization.
#' @param mem_share Share of memory to be used
#' @param mem_of_total Is 'mem_share' specified as share of total memory (instead of currently available)?
#'
#' @return matrix with extracted values
#' @export
vx_extract <- function(s, points, n_cores = NULL, mem_share = .5, mem_of_total = FALSE){
# split into memory-safe chunks
raster_chunk_list <- split_layers(s, mem_share, mem_of_total)
# process one chunk after the other
# use parallel processing if specified
out_list <- lapply(raster_chunk_list, function(r_chunk){
if(!is.null(n_cores)) {
cl <- parallel::makeCluster(n_cores)
v_list <- parallel::parLapply(cl,
raster::unstack(r_chunk),
varlist = c("points"),
fun = function(ri) {
vx <- velox::velox(ri)
vx$extract_points(points)
})
out_chunk <- do.call(cbind, v_list)
parallel::stopCluster(cl)
} else {
vx <- velox::velox(r_chunk)
out_chunk <- vx$extract_points(points)
}
out_chunk
})
out <- do.call(cbind, out_list)
out
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.