I want to crop raster cells around the lines of a polygon,up to some width. I have dig around but i could only manage to come to the following reproducible example. For some unknown reason, the projection is giving me a problem that blocks my advance, and then I do not really know how to proceed to crop the raster using the generated buffer region around the polygon. Maybe is not the best approach either...

brbuff=gBuffer(cl,byid=F, width = .5)
  • What's the problem? All this code does is create a warning, because your buffer is 0.5 degrees, and that's an unusual thing to do because 0.5 degrees is a different distance on the ground depending on where you are N-S on the planet. If all you want is a small buffer around a polygon so you don't clip the raster too close to the edge, this is fine. Carry on with raster::mask (probably).
    – Spacedman
    Commented Dec 16, 2020 at 12:33
  • Thanks, what I want is to only plot raster pixels enclosed within a pre-specified distance of the polygon line. I thought I could define such a band using gBuffer, but i do not know how to follow. plot(mask(alt,brbuff)) did not do what I am looking for. Commented Dec 16, 2020 at 14:47
  • The alt raster is already cut to the extent of Brazil so I'm not sure what you are expecting to see when you mask it with the outline of the same country. (Note that if you want a true distance you need to convert your data to a projected coordinate system and then you can do a buffer that is in kilometres (approximately)).
    – Spacedman
    Commented Dec 16, 2020 at 16:20
  • I was expecting to see only a band of raster values situated close to the interior of the polygon line that draws the country. Commented Dec 16, 2020 at 19:23
  • Sounds like you need to create a negative distance buffer polygon and then use something like rgeos::gDifference to subtract that from the country polygon, leaving a thin polygon ring. If possible you should also look at the sf package for spatial data and use st_buffer and st_difference...
    – Spacedman
    Commented Dec 16, 2020 at 21:32

2 Answers 2


I think your problem is that the buffer of a polygon is always a polygon that is larger (or smaller) by the buffer distance, and isn't a buffer of the border line. You can convert the polygon to a line and buffer that, though:

cl = as(cl,"SpatialLines") # convert polygon to line
linebuff=gBuffer(cl,byid=F, width = 1)
maskalt = mask(alt, linebuff)

enter image description here

This map shows the buffer of the polygon line as a dotted line region together with the mask and the original polygon:

enter image description here


Here is how you can crop a raster using a buffered polygon

Example data

alt <- getData("alt",country="BRA")
bra <- getData("GADM", level=1, country="BRA")
acre <- bra[bra$NAME_1 == "Acre", ]

Cropping alt by a buffer around Acre

acbuf <- buffer(acre, 1) 
acalt <- crop(alt, acbuf)

lines(acre, col="red")
lines(acbuf, col="blue")

Perhaps you also want to mask?

macalt <- mask(acalt, acbuf)
  • 2
    I think the questioner wants the buffer around the line of the polygon. ie cast the POLYLINE to a LINESTRING and then buffer, to get a buffer that is +/- X units from the boundary. Could also do with a negative buffer and a difference as I put in comments but casting to a line is probably a lot quicker.
    – Spacedman
    Commented Dec 17, 2020 at 8:24
  • Both answers worked for me! Thank you so much! Commented Dec 17, 2020 at 11:42

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.