The QGIS raster calculator seems limited, but you can achieve a lot once you know a couple of tricks
These hold true for both SAGA and QGIS raster calculators
true = 1
false = 0
You can use addition to simulate boolean logic
X or Y : x+y > 0
X and y : x+y = 2
I've modified Joseph's answer to use these, and get around the lack of boolean logic in SAGA's grid calculator
ifelse(gt(eq(g1,30)+eq(g1,34),0),ifelse(eq(g2,0/0),g1,g2),g1)
This i've tested, the example below shows my two images. The first is a land cover classification, the second is a gradient based on latitude (generated in saga using ypos()
). The final image, I've taken two of the classified values and replaced them with the gradient value.
Be careful doing this in SAGA itself, it's all to easy to overwrite
your original raster. Probably safer to call from Processing, as Joseph suggested.
In QGIS the same would be as follows. I've assumed your rasters are a (first) and b (second), and you're only using band1 (@1
)
"a@1" + ((((("a@1"=30)+("a@1"=34) >=1) + ("b@1">0)) =2) *("b@1"-"a@1"))
EDIT
Just realised I'm copying over all data pixels from the second image, including zeros. This slightly more complex expression should do the job...
ifelse(gt(eq(g1,30)+eq(g1,34),0),ifelse(eq(g2,0/0),a,ifelse(eq(g2,0),g1,g2)),g1)
Solution using SAGA Raster Calculator
I performed a quick test and you should probably use this expression:
ifelse(a=0,b,a)
instead of the one you provided (also, choose 8 byte floating point number
as Output Data Type
parameter).
Using these sample rasters:
and the provided formula, I get:
where gray zones are equal to 149 and white zones are equal to 183 (there are also few cells with different values in the upper left of the raster, but I think it is due to the way with which I reproduced the problem, so the starting rasters maybe were not perfectly aligned).
EDIT
Solution using QGIS Raster Calculator
I have just found another approach, which seems to work. Use the QGIS Raster Calculator instead of the SAGA Raster Calculator: you will find it from Raster >> Raster Calculator
.
Apply the first calculation using this expression:
("a@1"=0)* "b@1"
where a@1
is the raster A and b@1
is the raster B. This operation replaces all the 0 values from the raster A with the values from the raster B.
Then, do a second calculation on the previous result using this expression:
"result@1" + "a@1"
You will get this result:
as expected (I hope).
Best Answer
OK - after some extensive googling and a fair bit of hair ripped out, I found a solution.
Instead of using a the raster calculator, I used a shape file for the marshes, and "rasterized" them. Instead of specifying a new .tiff, I wrote over the land cover raster that I wanted to update, and boom. Done. Not sure if that is the "correct" way, but it works for what I need.