# comparing and finding inaccuracies in two raster layers

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
``````

## 1 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.

• Hi, thank you for the help. I have another question regarding NA. I want to get rid of NAs before reclassifying the initial rasters but omit.na didn't seem to work. I'm guessing that due to NA I'll be losing information from both raster layers. @WhiteboxDev – Cynical Realism Dec 19 '14 at 8:59
• Also you can subtract one raster from the another, if result = 0, then it´s the same value in the two rasters. – Pau Dec 19 '14 at 11:39
• @CynicalRealism I'm not sure what NA is; can you clarify? Is it a nodata value (not available)? If so, most software will appropriately ignore system designated nodata values. – WhiteboxDev Dec 19 '14 at 12:44
• @Pau The method described above doesn't involve any subtractions, only summations. I hope that clarifies things. – WhiteboxDev Dec 19 '14 at 12:44
• @Pau Yes, instead of using above method(which i think is quite useful too), i decided to give NA (NoData) new value 3 and decided to add to find aggreement and again subtract in order to find disagreement. – Cynical Realism Dec 19 '14 at 12:50