This function creates a buffer around a spatial object and then crops another spatial object to lie within the extent of the buffered object.
buffer_and_crop(to_buffer, to_crop, buffer = NULL, ...)
The function returns the to_crop
object, cropped to the extent
of the buffered to_buffer
object.
This is a simple wrapper for gBuffer
and crop
. If buffer = NULL
, the function simply implements crop
.
# Define an example raster
nrw <- ncl <- 50
r <- raster::raster(nrow = nrw, ncol = ncl)
r[] <- stats::runif(nrw * ncl, 0, 1)
# Buffer and crop the raster around an example location
xy <- sp::SpatialPoints(matrix(c(0, 0), ncol = 2))
r2 <- buffer_and_crop(
to_buffer = xy,
to_crop = r,
buffer = list(width = 10)
)
#> Warning: GEOS support is provided by the sf and terra packages among others
# Visualise outputs
pp <- par(mfrow = c(1, 2))
raster::plot(r)
raster::plot(r2)
par(pp)