TransWikia.com

Add "buffer" around mask (R, raster)?

Geographic Information Systems Asked by erc on April 30, 2021

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?

One Answer

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

Correct answer by Etzwane on April 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP