I am working with a RasterStack, from which I only need the data for a certain region, defined in a SpatialPolygonsDataFrame. I used the following code which works well:

test.ex <- crop(dat.stack, extent(myregion))
test <- mask(test.ex, myregion)

Is it however possible to add a "buffer" around my area of interest, so that not only the grid cells of the RasterStack covered by the polgyon of my region but also one additional grid cell are added around the outline of the resulting raster?

1 Answer 1


I havn't tested, but I think it works like that : If your region is delimited by a shapefile, you can use the function "gbuffer" from package "rgeos", then use mask:

# 500m buffer if shapefile's crs: +proj=utm /// +units=m
buffer = gBuffer(myregion,width = 500) 
# inverse=F will mask outside of buffer I think
test = mask(test.ex, buffer, inverse=F) 

If you want to use an extent class for the buffer, you have to add first:

extentofmyregion = as(extent(myregion), "SpatialPolygons")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.