7

Is it possible to round raster value in QGIS to a given number of digits? I have raster data with temperatures as values but the authors have made the values Celsius * 10. That is, a temperature of 2.5 is stored as 25. When I use the following formula in the Raster Calculator:

"july_merged@1" * .1

...the result is 2.599999904632568. I just want the 2.5.

3 Answers 3

8

As an alternative, you can use in QGIS the gdal.calc module located in : Processing toolbox > GDAL/OGR > GDAL - Miscellaneous > Raster calculator

  • Select your raster (A)

Raster Calculator - raster select

  • Type in the formula : round (A,2)

Raster Calculator - formula

The details of the function used comes from numpy array functions :

  • round_(a[, decimals, out]) Round an array to the given number of decimals.

This way, you can master the way the values are rounded. See below the result.

Raster Calculator - result

In a commandline way :

gdal_calc --calc "Round(A,2)" --format GTiff --type Float32 -A \\my\path\input.tif --A_band 1 --outfile \\my\path\output.tif
2
  • 2
    gdal_calc --calc "Round(A,2)" - correct syntax may depend on Numpy version. For me round_(A,2) works with numpy 1.11.
    – jurajb
    Commented Mar 19, 2018 at 11:29
  • Hum...good point. Maybe i made a mistake in the final formula. I should check it out... From what I read in my own message (round_(a[, decimals, out]) Round an array to the given number of decimals.), i would say the right syntax is yours..
    – gisnside
    Commented Mar 19, 2018 at 14:22
3

There's no built-in round function (which, ugh why), but this question provides a workaround - How to round up/down an pixel value of a raster to a pixel value with 2 decimal places?

In your case, replace multiply by 100 with multiply by 10 since you only want 1 decimal place.

1
  • No, it still adds all of those decimal places. I tried all of the calculations on that page.
    – Jim
    Commented Aug 14, 2016 at 21:18
0

I used floor instead of round, floor function "cut" the pixel value, if you have 2.5 and want to get 2, you have to use the floor function, in that case if you use round you will get 3.

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.