Search code examples
rrasterr-raster

Extract non common areas between rasters in R


I'm trying to extract the non common areas between two rasters in R.

I have two rasters: yellow and red.

I want to extract all the points of the yellow that DO NOT overlap with red (i.e. all the yellow you see in the plot below).

I tried with the raster package but I got this error:

mask(crop(yellow, red), red)

Error in compareRaster(x, mask) : different extent

Here the extents:

extent(yellow)
class      : Extent 
xmin       : 16.96877 
xmax       : 28.91172 
ymin       : 40.81715 
ymax       : 48.22444 

extent(red)
class      : Extent 
xmin       : 16.74428 
xmax       : 28.91172 
ymin       : 40.23355 
ymax       : 48.89783 

enter image description here


Solution

  • Try the mask() function with the argument inverse == TRUE See: https://www.rdocumentation.org/packages/raster/versions/3.5-15/topics/mask