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
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")
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"
("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.