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, ...)

Arguments

to_buffer

A spatial object to be buffered (see gBuffer).

to_crop

A spatial object to be cropped by the buffered object (see crop).

buffer

A named list of arguments, passed to gBuffer to buffer the to_buffer object (e.g., buffer = list(width = 10)).

...

Additional arguments passed to crop.

Value

The function returns the to_crop object, cropped to the extent of the buffered to_buffer object.

Details

This is a simple wrapper for gBuffer and crop. If buffer = NULL, the function simply implements crop.

Author

Edward Lavender

Examples

# 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)