[gdal-dev] java Dataset.GetRasterBand(i).ComputeRasterMinMax not working for float data
Even Rouault
even.rouault at spatialys.com
Fri Jan 27 10:37:44 PST 2017
On vendredi 27 janvier 2017 11:15:59 CET mccorb wrote:
> I have a geotiff image with float data (GDT_Float32). When I read a tile I
> have to case the nodata value to float to make it compare otherwise it
> doesn't...here is a reduced snippet of my code
>
> Double[] d = new Double[1];
> rb.GetNoDataValue(d);
> double noDataVal = d[0];
> ...
> if(rasterBandDataType == gdalconstConstants.GDT_Float32)
> {
> float[] array = new float[(rect.height * rect.width)];
> int retVal = rb.ReadRaster(rect.x, rect.y, rect.width, rect.height,
> rasterBandDataType, array);
> * float noDataValCast = (float) noDataVal;*
> for(int i = 0; i < array.length; i++)
> {
> if(noDataValCast == buff[i])
> array[i] = Double.NaN;
> else
> array[i] = (scale * buff[i]) + offset;
> }
> }
>
> If I don't cast the noDataVal to a float then the value in the returned
> array doesn't equal the noData value
> returned from the raster band.
>
> It seems as though the ComputeRasterMinMax call has the same issue as I was
> having before I did the casting. I definitely want to use this method it
> because it is much faster than pulling the tiles and processing them myself.
All of that is more or less expected given that GetNoDataValue() and
ComputeRasterMinMax() return double values. So if you compare to float directly, the float
get promoted to double (and the double isn't strictly equal to the initial pixel value), and
tiny precision issues appear. Downcasting to float as you did is the way to go.
Even
--
Spatialys - Geospatial professional services
http://www.spatialys.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20170127/680e2d72/attachment.html>
More information about the gdal-dev
mailing list