[GIS] comparing and finding inaccuracies in two raster layers

r

I have two raster layers with urban cells and rural cells. I used an overlay function to compare these two raster layers and see the differences. Before overlaying I gave urban cell the value 1 and rural cell the value 0. After overlaying I get the result: 0,1,2,NA:
Clearly, 2 and 0 mean that the urban and rural cells match and 1 mean it doesn't. however just looking at 1 i cannot see if the error was occured due to urban cell or rural cell.
Does anyone know how can I see where the inaccuracy in this raster layer is if i take one of the raster layers as reference?

b 
class       : RasterLayer 
dimensions  : 190, 333, 63270  (nrow, ncol, ncell)
resolution  : 0.008344257, 0.008344257  (x, y)
extent      : 12.10615, 14.88479, 51.68836, 53.27377  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0  

 c
class       : RasterLayer 
dimensions  : 190, 333, 63270  (nrow, ncol, ncell)
resolution  : 0.008344257, 0.008344257  (x, y)
extent      : 12.10615, 14.88479, 51.68836, 53.27377  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 


 uc <- overlay(c, b, fun=function(x,y){return(x+y)})
 urx <- overlay(c, b, fun=function(x,y){return(x*y)})  

 cellStats(uc, stat = "sum",na.rm = TRUE) 
 unique(getValues(uc)) 
 [1] NA  0  1  2

Best Answer

So you have two Boolean rasters containing the same classes, urban and rural, and you would like to know the combined classes, retaining information for both the class (rural and urban) as well as the source raster (1 or 2). I would suggest using base-2 (binary) numbers for your initial class values. Reclassify your initial rasters such that:

Raster1 Rural = 1 (00000001)

Raster1 Urban = 2 (00000010)

Raster2 Rural = 4 (00000100)

Raster2 Urban = 8 (00001000)

Now when you sum the two images together pixels will contain only the following possible values:

Rural Agreement = 5 (00000101)

Urban Agreement = 10 (00001010)

Rural Raster1/Urban Raster2 = 9 (00001001)

Urban Raster1/Rural Raster2 = 6 (00000110)

You can then reclass these values to a more logical sequence (e.g. 1, 2, 3, 4) as need be. This is the trick for representing more than one piece of information in a pixel's single numerical value. The key is that the numerical values themselves are nothing more than flags, as indicate by their binary representation.