5

I'm fairly new to QGIS, but I'm getting the hang of it. I have two layers - one DEM and one raster made from shape file (Buildings), but need one with higher Elevation where the buildings are for terraflow. But I can't seem to get the raster calculator to work with if Statement. I found a Workaround by assigning a fixed elevation value to the building raster, that is higher than any Point on DEM.

merged

But on DEMs with high Elevation difference this means that some Buildings are over 50m tall!!! would like to make an if statement like this:

DEM A Buildings B

A = DEM

B = Buildings

if(B>A,A+10,A) 
3
  • You can use the GRASS raster calculator, accessible via the QGIS processing toolbox. It has an ifelse condition you can use: gis.stackexchange.com/questions/110248/…
    – Babel
    Commented Jan 29, 2021 at 13:38
  • QGIS raster calculator uses a different logic for if statements. Please, see my answer.
    – xunilk
    Commented Jan 30, 2021 at 16:44
  • 1
    In your building raster, if you assigned 10 to pixels representing buildings, and 0 otherwise, you could do a simple addition of A + B, no need for conditional logic. Or even better, keep it as a byte raster (0 and 1), and do A + B * 10. Commented Oct 25, 2021 at 21:38

3 Answers 3

5

QGIS raster calculator uses a different logic for if statements. For implementing this expression if(B>A,A+10,A), you need to write in Raster Calculator Expression Window following formula:

( "B@1" >  "A@1" )  *  ( "A@1" + 10 ) + ( "B@1" <= "A@1" ) * "A@1"

and select output layer as C.tif.

I tested it with two arbitrary random raster and it worked as expected. It was corroborated by using Value Tool QGIS plugin.

1
  • Any guess how to treat NoData values? I have seen ("A@1"=0)*1 should return 1 instead of 0 (NoData). But anyway the output is still just NoData. I have to remove NoData with gdal_translate first.
    – Albine Pro
    Commented Mar 9, 2022 at 11:25
5

With the release of QGIS version 3.22 Białowieża these days, the long awaited, simpler conditional statement is introduced, using the new if function: see changelog and documentation.


As noted in the commnents below: The QGIS native raster calculator (the one under discussion here) can be found in Menu Raster > Raster Calculator

It can be found, alternatively, in the processing toolbox (Menu Processing > Toolbox), where apart from this one, there are two other raster calculators: GDAL and SAGA. SAGA raster calculator has an ifelse condition included for a long time already, see e.g.: https://gis.stackexchange.com/a/383687/88814.

enter image description here

5
  • 1
    Weirdly enough the QGIS raster calculator available in the Processing toolbox does not have the if function, only the one available in the Raster menu. Commented Mar 12, 2023 at 17:29
  • You're sure you have selected the QGIS native Raster calculator, not one of the two others available?
    – Babel
    Commented Mar 12, 2023 at 18:36
  • Yeah, absolutely, it's ID is "qgis:rastercalculator". Commented Mar 12, 2023 at 20:58
  • Indeed, you're right, I can confirm this, see screenshot: i.sstatic.net/BpyAV.png A bug, then, I guess... ?
    – Babel
    Commented Mar 12, 2023 at 21:01
  • 1
    The Processing raster calculator tool may not list the "if" function in the UI, but it definitely supports it.
    – user2856
    Commented Aug 4, 2023 at 6:52
1

This is a great achievement (adding the IF statement). Just wanted to add a helpful tip as it is a little confusing where to find this tool. There are 3 versions of the Raster Calculator in 3.22 QGIS. 2 (earlier versions) can be found in the Processing toolbox but do not contain the new IF functionality. The third one (that contains the new functionality) is found under the Raster menu and not in the Processing toolbox. Perhaps in a later version some rationalisation could be done. However, well done to all involved in adding a much needed functionality.

4
  • QGIS has three different raster calculators: native QGIS (the one that offers the if condition since 3.22), GDAL and SAGA. All three can be found in the processing toolbox, see: i.sstatic.net/Wy0Wv.png - as it should be: the processing toolbox is a central hub where you can find all tools, algorithms, plungins etc. at your disposal inside QGIS.
    – Babel
    Commented Dec 9, 2021 at 11:16
  • SAGA raster calculator has an ifelse condition included for a long time, see e.g.: gis.stackexchange.com/a/383687/88814
    – Babel
    Commented Dec 9, 2021 at 11:17
  • @Babel for clarity, there are three raster calculators in the Processing Toolbox, and another native raster calculator GUI found in the Raster menu. Whilst both native interfaces support the new if function, there are differences between them, and an argument for rationalisation: See #44016 Commented Mar 21, 2022 at 15:19
  • 2
    @Colin Mazengarb The new if function is supported in both the menu and processing toolbox interfaces of the native raster calculator, however a button for it is only currently present in the menu version. Commented Mar 21, 2022 at 15: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.