[QGIS-Developer] Wrong results converting vectors to Float32 rasters

Pedro VenĂ¢ncio pedrongvenancio at gmail.com
Wed Oct 28 08:06:04 PDT 2020


Hi all,

I'm seeing a weird behaviour when converting vectors to rasters.

I have a polygons layer with a "value" field that has 2 decimal places.

Converting it to raster with gdal_rasterize, the output is different if I
choose it as Float32 or Float64.

Here is a sample Project:
https://cld.pt/dl/download/61894ebd-ade0-4f12-8aa3-1405ab2690cc/Float_Issue_Project.zip

These are the commands I use (inside QGIS or in CLI):

gdal_rasterize -l polygons -a value -tr 5.0 5.0 -a_nodata 0.0 -te
61738.6841 107228.3171 63464.9242 108429.7802 -ot Float32 -of GTiff
D:\Testes\Float_Issue\polygons.gpkg
D:/Testes/Float_Issue/polygons_rasterize_float32.tif

Info:
Name polygons_rasterize_float32
Path D:\Testes\Float_Issue\polygons_rasterize_float32.tif
CRS EPSG:3763 - ETRS89 / Portugal TM06 - Projected
Extent 61738.6840999999985797,107229.7801999999937834 :
63463.6840999999985797,108429.7801999999937834
Unit meters
Width 345
Height 240
Data type Float32 - Thirty two bit floating point
GDAL Driver Description GTiff
GDAL Driver Metadata GeoTIFF
Dataset Description D:\Testes\Float_Issue\polygons_rasterize_float32.tif
Compression
Band 1
STATISTICS_MAXIMUM=18537
STATISTICS_MEAN=3782.009546874
STATISTICS_MINIMUM=0.12999999523163
STATISTICS_STDDEV=7001.5216220675
STATISTICS_VALID_PERCENT=100
More information
AREA_OR_POINT=Area
Dimensions X: 345 Y: 240 Bands: 1
Origin 61738.7,108430
Pixel Size 5,-5


gdal_rasterize -l polygons -a value -tr 5.0 5.0 -a_nodata 0.0 -te
61738.6841 107228.3171 63464.9242 108429.7802 -ot Float64 -of GTiff
D:\Testes\Float_Issue\polygons.gpkg
D:/Testes/Float_Issue/polygons_rasterize_float64.tif

Info:
Name polygons_rasterize_float64
Path D:\Testes\Float_Issue\polygons_rasterize_float64.tif
CRS EPSG:3763 - ETRS89 / Portugal TM06 - Projected
Extent 61738.6840999999985797,107229.7801999999937834 :
63463.6840999999985797,108429.7801999999937834
Unit meters
Width 345
Height 240
Data type Float64 - Sixty four bit floating point
GDAL Driver Description GTiff
GDAL Driver Metadata GeoTIFF
Dataset Description D:/Testes/Float_Issue/polygons_rasterize_float64.tif
Compression
Band 1
STATISTICS_MAXIMUM=18537
STATISTICS_MEAN=3782.0095468599
STATISTICS_MINIMUM=0.13
STATISTICS_STDDEV=7001.521622075
STATISTICS_VALID_PERCENT=100
More information
AREA_OR_POINT=Area
Dimensions X: 345 Y: 240 Bands: 1
Origin 61738.7,108430
Pixel Size 5,-5

Looking at output in Value Tool:

[image: image.jpeg]


So, if the conversion is to a Float64 raster, pixel values have 2 decimal
places, as the original data.
If the conversion is to a Float32 raster, several pixel values assume 16
decimal places.

Float32: 0.2199999988079071
Float64: 0.22
Original value: 0.22


Where this becomes a serious problem is when the values are used, for
instance, for a reclassification process (with Reclassify by Table).
Imagine these rules:

[0 - 0.22[ = 1
[0.22 - 0.5[ = 2
>= 0.5 = 3

Pixels 0.22 are reclassified as 2;
Pixels 0.2199999988079071 are reclassified as 1.

[image: image.jpeg]


So, Float64 gives the right results and Float32 gives wrong results.

But how can this be explained, if all input values can fall within both a
Float32 and Float64 range of values?

  Float32       -3.4E38           3.4E38
  Float64       -1.79E308         1.79E308


Even more strange is, if instead of gdal_rasterize I use OTB Rasterization,
no matter whether I use float (Float32) or double (Float64) dtype, I always
get wrong results, just like gdal_rasterize with Float32.

Should we change the default gdal output to Float64? Or is this a deeper
issue?

Thanks.

Best regards,
Pedro VenĂ¢ncio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20201028/35fc9e72/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.jpeg
Type: image/jpeg
Size: 6752 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20201028/35fc9e72/attachment-0002.jpeg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.jpeg
Type: image/jpeg
Size: 12029 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20201028/35fc9e72/attachment-0003.jpeg>


More information about the QGIS-Developer mailing list