Search code examples
rrasterr-raster

Extract buffer from a raster in R


I am beginner in R and spatial analysis. I have a raster where the cells of habitat polygons are equal to 1.

 img <- readPNG("Paysage.png")
 map <- raster(img[,,2],xmn=0, xmx=999, ymn=0, ymx=999)
 projection(map) <- "+proj=utm +zone=18 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"
 map[] <- polygons[,c("id_polygon")] 
 map[ map > 0] <- 1

From this raster, how can I build, for each polygon, a raster in which all polygon cells that are situated in a buffer of 1 km around a given polygon are equal to 1 and all other matrix cells are equal to 0.

Thanks very much for your help.


Solution

  • Use the buffer function in the raster package: Just make sure you set to NA all of the cells that you do not want to have the buffer grow from:

    map[map==0]=NA
    library(raster)
    b <- buffer(map, width=1000)