1

I have single band raster whose values somehow were set to something like 1.399999999 instead of 1.4, 1.29999999999 instead of 1.3 (I'm not sure about the number of 9's there.), 1.2000047683 instead of 1.2. While some pixels do have a value 0.5.
I actually need all values to one decimal place like0.5, 1.2, 1.3, 1.4. For this I searched around and bumped into one answer in stackexchange. I did not find gdal.calc as suggested but found r.mapcalculator (dialog box looked similar). I then used the function round(A,1). But it did not work.

How can I round the pixel values to one decimal place ?

When I used raster calculator raster@1 * 100 the new raster formed has values like 50, 120, 130, 140 and then again used newraster@1 / 100 to obtain 0.5, 1.20000047683, 1.299999999, 1.399999999.

7
  • 1
    You can multiply by 10, then convert to integer: gis.stackexchange.com/questions/146543/… and then back to float and divide by 10
    – BERA
    Apr 20, 2018 at 5:27
  • which raster should I multiply by 10 ? The one with values 1.3999999 ? @BERA
    – anup
    Apr 20, 2018 at 5:30
  • @BERA Yes, Sorry about that. From the link you posted, this line gdal_translate -of GTiff -ot Int32 E:/float.tif E:/int.tif converts to int. Now what do I use to convert it back to int ? Is it FLT ?
    – anup
    Apr 20, 2018 at 5:39
  • 1
    @BERA It was Float32, but It didnot work. It gave me back 1.399999 and 1.299999
    – anup
    Apr 20, 2018 at 5:46
  • 1
    It worked with Float 64 just fine :D Thank you! @BERA
    – anup
    Apr 20, 2018 at 5:50

1 Answer 1

2

You can multiply by 10, then convert to integer using gdal_translate, see: How to round pixel values of a raster in QGIS? and then back to float and divide by 10.

To convert to Int:

gdal_translate -of GTiff -ot Int32 E:/float.tif E:/int.tif

To convert back to Float:

gdal_translate -of GTiff -ot Float64 E:/int.tif E:/float64.tif

Not the answer you're looking for? Browse other questions tagged or ask your own question.