I'm trying to mask a raster with a ring polygon using the function raster::mask, but only recognizes the outer edges of the polygon, can anyone help me with this? I only need the pixels within the yellow area, but it returns pixel inside the outer edge of the polygon.


buffer<-shapefile("my_shapefile_path") #yellow buffer

output<-mask(raster, buffer)
writeRaster(output, "filename") #output grayscale raster

I only need the pixels within the yellow area

  • How about you divide the task in two steps? First the outer, then the inner clip? – Erik May 22 at 14:33
  • Thanks Erik. How can I clip the inner section? – Claudia May 22 at 14:36
  • I guess like the other operation, simply separate your mask into two parts. – Erik May 22 at 14:37
  • can you post the code you are using? – GCGM May 22 at 15:03
  • Is this image an overlay of the clipped raster (grayscale) and the polygon (yellow with opacity) showing (with the red X) the bit that hasn't been clipped out? It looks a bit of a mess and could do with the explanation. – Spacedman May 22 at 15:19

this could be a comment but not enough rep :( It looks like some set of polygons in your shapefile overlap to form a continuous border around the region you wish to mask. I'm not sure what raster::mask does with the vector data you give it; if it unions it then you'll need to call mask for each feature in your geometry. On the other hand if you've unioned your data then that's the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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