[GIS] Rounding raster values in QGIS raster calculator

qgisrasterraster-calculator

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.

Best Answer

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
Related Question