I have two grids, grid A representing observed extents of flooding and grid B representing simulated extents from a hydraulic model. The rasters have the same extent and grid size.

I want to be able to compare each pixel and create a new raster with 4 unique values (A/B/C/D equivalent in the table below) to show the difference between the two rasters:

enter image description here

If this was a shapefile field calculator problem I'd use a CASE logic like the one below but I'm struggling to identify a QGIS raster-based equivalent. Can anyone suggest anything I could try?

    WHEN "Modelled" > 0 AND "Observed" > 0 THEN D
    WHEN "Modelled" = 0 AND "Observed" > 0 THEN C
    WHEN "Modelled" > 0 AND "Observed" = 0 THEN B
    WHEN "Modelled" = 0 AND "Observed" = 0 THEN A
  • Hi, have you tried the Raster Calculator? You can add many different expression with a similar syntax to that one of the vector Field Calculator
    – matteo
    Commented Sep 21, 2016 at 12:43
  • As far as I can see the standard raster calculator doesn't support CASE/ELSE or IF/ELSE statements. SAGA grid calculator will support IF/ELSE but won't allow chaining of statements together; for example A>0 AND B>0. Commented Sep 21, 2016 at 12:53
  • you are right, but maybe you can achieve want you are looking for, with some logic statement. Maybe this answer can help you: gis.stackexchange.com/questions/48752/…
    – matteo
    Commented Sep 21, 2016 at 12:58
  • I tried that but grid calc seems to fall over with the AND used in "eq(g1>0 AND g2>0" - I'll explore some more. Commented Sep 21, 2016 at 13:40
  • @EdRollason - I forgot that AND isn't used. Try something like: ifelse(gt(a,0)+gt(b,0),400,ifelse(eq(a,0)+gt(b,0),300,ifelse(gt(a,0)+eq(b,0),200,ifelse(eq(a,0)+eq(b,0),100,0))))
    – Joseph
    Commented Sep 21, 2016 at 14:54

1 Answer 1


You can use the QGIS raster calculator for this, using boolean masks. An example:

(("grid1@1">0) AND ("grid2@1">0)) * 1
+ (("grid1@1"=0) AND ("grid2@1">0)) * 2
+ (("grid1@1">0) AND ("grid2@1"=0)) * 3
+ (("grid1@1"=0) AND ("grid2@1"=0)) * 4

where you can change 1, 2, 3, and 4 to be whatever values you like.

The way that this works is that the first part of each term is a boolean mask. ("grid1@1">0) AND ("grid2@1">0)) is True when your first condition is met, and it can be multiplied by your categorical value D (I used 1). The next three terms match your next three conditions.


Steven Kay notes in the comments that the same expression can be performed without using AND by replacing it with a *. In this case, the first term looks like

(("grid1@1">0) * ("grid2@1">0)) * 1
  • 1
    good answer, certainly works in 2.16. I think some older versions don't support AND, but you can replace AND with *, as QGIS treats false as 0 and true as 1. OR is slightly trickier, but you can use + and check that the answer > 0.
    – Steven Kay
    Commented Oct 1, 2016 at 12:50
  • Older versions didn't support the AND so I'd looked elsewhere as I couldn't get the more complex syntax to work. Good to know they've added it. I'll test this out on the data. Thanks Commented Oct 3, 2016 at 13:39
  • I can confirm that replacing AND with * returns identical results, at least in QGIS 2.16.
    – Nat Wilson
    Commented Oct 3, 2016 at 15:29
  • My syntax must have been wrong I guess! Commented Oct 4, 2016 at 8:22
  • This took me a while to check but this definitely works in 2.16. Commented Nov 1, 2016 at 14:26

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.