4

I am trying to change negative values (raster layer) into "0" but I cannot find a way to do that.

Does anyone know how to accomplish it in QGIS 3.2?

I found an answer somewhere on the Internet to use the Python console entering '0 if !field! < 0 else !field!' but I have no experience using Python.

1 Answer 1

11

I recommend Raster calculator.

If your raster layername is RasterA, then try (RasterA@1 >= 0) * RasterA@1

  • If the cell value is 0 or greater than 0, (RasterA@1 >= 0) returns 1 (i.e. True)
  • If the cell value is less than 0, (RasterA@1 >= 0) returns 0 (i.e. False).

In short, (RasterA@1 >= 0) acts as a Boolean mask.

3
  • Thanks, but I would like the values >0 to remain the original values and the values <=0 changed into 0. I used rastercalculator to create a raster where cells represent deviation from optimum pH. For negatives & 0 the pH is okay.
    – ClaKie
    Commented Nov 1, 2018 at 16:53
  • 1
    That's what this calculation does. The equation returns the original cell value when the original cell value is greater than or equal to zero. When the original cell value is less than zero, it returns 0. So all values less than zero are converted to zero, while all other values remain unchanged..
    – csk
    Commented Nov 1, 2018 at 17:23
  • Thanks @csk ! Please excuse my lack of explanation, ClaKie.
    – Kazuhito
    Commented Nov 1, 2018 at 19:02

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.