# Writing Conditional in QGIS raster calculator

I have a Digital Elevation Model raster with negative values in QGIS software, I would like to know how I can multiply all negative values by -1, ie all values less than 0, turning them into positive values.

I tried this:

``````("DEM" < 0 ) * -1
``````
• Which Version of QGIS are you using? Perhaps add that to your question. Commented Dec 3, 2021 at 0:32
• Try: `("DEM@1"<0)*"DEM@1"*-1+("DEM@1">=0)*"DEM@1"` which should convert all values less than 0 into the corresponding positive values and leave all pixels with value of zero or greater unchanged. Works in 3.16. Commented Dec 3, 2021 at 2:36
• @BenW: How about you turn this into an answer and grab some internet points?
– Erik
Commented Dec 3, 2021 at 7:18
• @Erik, thanks for the encouragement :-) with plenty of good solutions now presented, I think I will leave it as comment. If someone finds it helpful, I'm happy. Commented Dec 3, 2021 at 12:17
• Babel, Thanks for asking, I have two DEM, one is from the base of SRTM and the other Sentinel 1. In DEM Sentinel I have nageative values and in SRTM positive values. In my view, negative values should only appear in ocean bathymetry data. In order to delineate watersheds and drainage patterns in high resolution, I need to have the values well adjusted. Commented Dec 4, 2021 at 12:48

For QGIS versions older than 3.22, you can create a conditional statement for what you want to do with this expression:

``````("DEM@1" < 0) * (-1) * "DEM@1" +  ABS ( ("DEM@1" < 0) - 1) * "DEM@1"
``````

Output:

`100` -> `100`
`2` -> `2`
`-5` -> `5`
`-150` -> `150`
etc.

If you use the newest QGIS version 3.22, the raster calculator has a new if-condition. Use this expression:

if ( "DEM@1" <0, "DEM@1" * -1, "DEM@1")

• Thank you so much! In QGIS 3.16.14 there is no way to do it? Commented Dec 2, 2021 at 23:26

The easiest way to turn negative to corresponding positive values in raster calculator without even using a conditional statement is simply the function `abs()`:

ABS ( "DEM@1" )

See result for the area of the Jordan valley and the Dead Sea (red ouline) that is changed to positive values, whereas the rest remains: left - original DEM, right - output of the raster calculator with only positive values:

In previous versions it simply performs an assignment, a conditional within a parenthesis followed by a *, performs an assignment to all pixels that meet that condition.

Try this:

`("DEM" < 0 ) *0 + ("DEM" >= 0 ) *"DEM"`

It will convert all values less than 0 to 0, and leave all other pixels the same.

On the other hand, if the objective is that the negative values become positive and the others remain the same we can apply this:

`("DEM" < 0 ) *-"DEM" + ("DEM" >= 0 ) *"DEM"`

• Versions previous to which version?
– PolyGeo
Commented Dec 2, 2021 at 23:39
• Thank you very much! But you see, I don't want the values to be changed, I just want the values < 0 (Negative Values) to turn to positive. From what I understand from your explanation, values less than 0 will change to 1, but thank you very much, this is not a very simple discussion as I am working in the coastal zone with DEM. Commented Dec 2, 2021 at 23:41
• @Adilson it is also possible to do it with the same expression, edit the answer so that you get the desired result. Commented Dec 3, 2021 at 12:36
• Thank you very much, It worked, but I didn't reach my goals, for example: a river has a value of -23 meters in its main channel, and its edges (riparian forest) are at an altitude of 9 meters, that is, when I transform -23 into a positive value, the main channel of the river has an elevation greater than its edges, this is not very suitable for hydrological analyses. By all means, thank you very much for all the responses received! Commented Dec 3, 2021 at 12:41