mask and/or crop a Raster* based on a Polygon.

mask_crop_subset(r, p, idx = NULL)

Arguments

r

Raster* object

p

SpatialPolygons* object

idx

vector of strings indicating the layer indices to subset.

Value

A Raster* object with resolution and land-sea mask matching those of r and extent matching p.

Details

Please note that cells along the border with centroids falling outside the polygon p will not be returned. If cells along the border are needed, we suggest to identify cells covering the polygon and set all remaining pixels to NA, as described in this post: https://goo.gl/22LwJt.

Examples

if (FALSE) {

  # Define dummy polygon
  dummy_polygon <- as(raster::extent(7, 18, 37, 40), "SpatialPolygons")
  raster::crs(dummy_polygon) <- "+proj=longlat +datum=WGS84 +no_defs"

  # Read RISICO test data
  r_risico <- readRDS(system.file("extdata", "RISICO_raster.rds",
                                  package = "caliver"))

  mask_crop_subset(r = r_risico, p = dummy_polygon)

}