5

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
6
  • 1
    Which Version of QGIS are you using? Perhaps add that to your question.
    – Cushen
    Dec 3, 2021 at 0:32
  • 1
    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.
    – Ben W
    Dec 3, 2021 at 2:36
  • 1
    @BenW: How about you turn this into an answer and grab some internet points?
    – Erik
    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.
    – Ben W
    Dec 3, 2021 at 12:17
  • 1
    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.
    – Adilson
    Dec 4, 2021 at 12:48

4 Answers 4

6

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.

4

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

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

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: enter image description here

3

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"

4
  • Versions previous to which version?
    – PolyGeo
    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.
    – Adilson
    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.
    – Luis Perez
    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!
    – Adilson
    Dec 3, 2021 at 12:41

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.