# QGIS Raster calculator - if statement

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.

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:

A B

``````A = DEM

B = Buildings

if(B>A,A+10,A)
``````
• 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/… Jan 29, 2021 at 13:38
• QGIS raster calculator uses a different logic for if statements. Please, see my answer. Jan 30, 2021 at 16:44
• 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. Oct 25, 2021 at 21:38

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.

• 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. Mar 9, 2022 at 11:25

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.

• 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. Mar 12 at 17:29
• You're sure you have selected the QGIS native Raster calculator, not one of the two others available? Mar 12 at 18:36
• Yeah, absolutely, it's ID is "qgis:rastercalculator". Mar 12 at 20:58
• Indeed, you're right, I can confirm this, see screenshot: i.stack.imgur.com/BpyAV.png A bug, then, I guess... ? Mar 12 at 21:01
• The Processing raster calculator tool may not list the "if" function in the UI, but it definitely supports it. Aug 4 at 6:52

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.

• 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.stack.imgur.com/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. 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 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 Mar 21, 2022 at 15:19
• @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. Mar 21, 2022 at 15:26