5

I have two rasters with the same extent, resolution, and projection, let's say raster A & B. What I want to do is something like this:

IF A = 3 OR A = 11 THEN A+B
ELSE B

How can I do such a thing with a raster calculator?

1 Answer 1

9

Try this syntax:

(A@1 = 3 + A@1 = 11)* A@1 + B@1
  1. @1 means the first band of the raster
  2. A@1 = 3 returns 1 (TRUE) if A is 3
  3. When A is 3 or 11, (A@1 = 3 + A@1 = 11) part returns 1. Otherwise it returns 0.
2
  • I test it and it works just fine, but I don't really understand the A@1 + B@1 part of the syntax how can it returns B when the condition is false? Can you elaborate on that part? Commented Dec 16, 2019 at 9:39
  • @MuhamadNasirLukman When the condition is false (i.e. A is neither 3 or 11), both of the A@1 = 3 and A@1 = 11 return 0. Then the overall expression becomes 0 * A + B and ends with B.
    – Kazuhito
    Commented Dec 16, 2019 at 10:00

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.