I want to mask out a raster based on the values of a polygon: i.e., set to NA all values of the raster that are not covered by the polygons. However, it seems that
raster::mask() masks out cells (i.e. set them to NA) even if they are partially covered?
This is similar to the question: Cropping a raster by a polygon - cells missing that are partially outside the polygon, but unlike
mask() does not have the
Check: here the right point of the triangle touches a cell, yet that cells is masked out.
library(raster) library(sp) r <- raster(xmn=1, xmx=5, ymn=1, ymx=5, nrows=4, ncols=4) r <- 1:length(r) Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2))) Sr2 = Polygon(cbind(c(4.1,4,2),c(2,3,2))) SpP = SpatialPolygons(list(Polygons(list(Sr1), "s1"), Polygons(list(Sr2), "s2")), 1:2) plot(mask(r, SpP)) plot(SpP, add=TRUE)