10

I have a raster with float number values for example: 2.544459104537964 and I want to round these up to integers. I have seen other posts on ArcGIS like this one but when I use Raster Calculator in QGIS, Int(myraster + 0.5) only makes the first value (which is 0) to 0,5 and the last one (which is 100) to 100,5. The other pixels stay float.

4 Answers 4

26

This is a bit of a hack but it works: The QGIS raster calculator does not support rounding as far as I know but you can use GDAL to perform float to int type conversions.

  1. Raster -> Conversion -> Translate
  • change the datatype from float to int by using the -ot option

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

edit: This answer was written while Qgis 2.0 was the latest major version - please see this answer where to find the command in QGis 3.0

10
  • 1
    OMG this worked perfectly! Values such as 3,391823765 are now 3 and values like 5,912735176253 are now 6! thank you so so so much
    – Stelios M
    May 8, 2015 at 18:03
  • Wait, so it doesn't allways round down? Am I missing something in the documentation here?
    – Kersten
    May 8, 2015 at 19:21
  • To me it rounds them up as it should: 0,5 and above decimal becomes 1 and 0,4 and below becomes 0! And actually, after that I wanted the image to be grayscale. At first I typed -ot Int32 as you said, but then I had issues with the image in image processing software like Gimp. It showed as a 1 band RGBA. So I searched the documentation and I tried -ot Byte. Then it worked like a charm. Thanks again for this!
    – Stelios M
    May 8, 2015 at 19:36
  • Perfectly! I have already read many answers and no one did correctly operation. Thank you. Jul 15, 2015 at 0:40
  • odd that QGIS doesnt have that option in the dialog box
    – chrismarx
    Dec 14, 2016 at 15:49
4

Use Menu Processing / Toolbox / Round Raster:

This algorithm rounds the cell values of a raster dataset according to the specified number of decimals.

enter image description here

3

The answer given by @Kersten is still good six years on. I didn't get it right first time though, so just to save anyone else similar problems, here are some more details for QGIS 3.10. The tool is currently in Toolbox > GDAL > Raster conversion > Translate (convert format). The -ot option goes in the dialogue box, but you also need to specify the data type you want to convert to. Like so:

enter image description here

Other options are available:

enter image description here Source - https://docs.qgis.org/3.16/en/docs/user_manual/processing_algs/gdal/rasterconversion.html#translate-convert-format

2

Update February 2022:

The QGIS raster calculator doesn't support rounding or data formats (which is annoying), but the gdal raster calculator does (it supports rounding, data formats and even more important in my opinion: Compression)! It is usually available in QGIS as well, but easily overlooked if you don't know it's there (Search for "calc"):

Processing Toolbox showing Raster Calculator

The gdal Calculator (gdal_calc) supports calculations in numpy style and also all gdal data format options. It just isn't as comfortable on the interface and limited to 6 different input rasters (as far as I can see). You have to manually select input files from a loaded layers drop down menu/file location, select the input Band and type your formula with placeholders A, B ... F for input files (i.e. bands of files). The configurations looks as follows and results in a gdal command as shown below:

Gdal Calculator Interface

gdal_calc.bat --calc "A-B" 
    --format GTiff --type Int16 
    -A "in_file_A.tif"--A_band 1 
    -B "in_file_B.tif" --B_band 1 
    --co COMPRESS=DEFLATE --co PREDICTOR=2 --co ZLEVEL=9 
    --outfile "outfile.tif"

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.