Saturday, 5 September 2015

R raster: masking a raster by polygon also remove cells partially covered?


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 crop(), mask() does not have the snap="out" option.


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)

enter image description here




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...