OSGeo / gdal

GDAL is an open source MIT licensed translator library for raster and vector geospatial data formats.
https://gdal.org
Other
4.95k stars 2.57k forks source link

Why OSGeo.GDAL.Band returns nothing for GetMaximum method? #1548

Closed WindingWinter closed 5 years ago

WindingWinter commented 5 years ago

I have the following code:

    string tiffFile=%anytifffile%;
      using (var ds = Gdal.Open(tiffFile, Access.GA_ReadOnly))
      {
        var nCol = ds.RasterXSize;
       var nRow = ds.RasterYSize;
       var allValues = new int[nCol * nRow];
       Band band = ds.GetRasterBand(1);
       band.ReadRaster(0, 0, nCol, nRow, allValues, nCol, nRow, 0, 0);   
       double[] minMax = new double[2];
        band.ComputeRasterMinMax(minMax, 0);
       double maxVal = double.MinValue ;
       int intMaxVal=-1;
       band.GetMaximum(out maxVal, out intMaxVal);  
       }

It seems that minMax value is correct ( because correct values are returned), but intMaxVal and maxVal are wrong ( ie: no value).

Why is it so?

I'm using GDAL.Net binding, version 2.3. I suspect that the problem is there also in other Python or C++ bindings.

rouault commented 5 years ago

From the C++ docs

/**
 * \brief Fetch the maximum value for this band.
 *
 * For file formats that don't know this intrinsically, the maximum supported
 * value for the data type will generally be returned.
 *
 * This method is the same as the C function GDALGetRasterMaximum().
 *
 * @param pbSuccess pointer to a boolean to use to indicate if the
 * returned value is a tight maximum or not.  May be NULL (default).
 *
 * @return the maximum raster value (excluding no data pixels)
 */

double GDALRasterBand::GetMaximum( int *pbSuccess )

In Python, when *pbSuccess = FALSE, nothing is returned, which is an acceptable behaviour I think

>>> from osgeo import gdal
>>> ds = gdal.Open('byte.tif')
>>> print ds.GetRasterBand(1).GetMaximum()
None

So similar behaviour in C# seems OK to me

mloskot commented 5 years ago

intMaxVal and maxVal are wrong ( ie: no value)

What do you mean by wrong or no value?

What does the intMaxVal say? Notice, this is boolean flag:

https://github.com/OSGeo/gdal/blob/f070adf64950cae1c6cc86b104ba835c29df06b1/gdal/swig/csharp/apps/GDALInfo.cs#L233-L238

WindingWinter commented 5 years ago

Try this one example.

beforetransform.zip

band.ComputeRasterMinMax(minMax, 0);

Returns proper value for minMax, but

band.GetMaximum(out val, out hasval);

hasval returns 0 andval a very big number

Obviously ComputeRasterMinMax and GetMaximum not returning the consistent value as per indicated in the doc.

rouault commented 5 years ago

Obviously ComputeRasterMinMax and GetMaximum not returning the consistent value as per indicated in the doc.

What you get is expected. The TIFF file has no metadata indicating the maximum value, hence GetMaximum(), which doesn''t do any computation, returns the maximum possible value for a Float64 and hasval = 0 to indicate that this value cannot be trusted. ComputeRasterMinMax() on the contrary inspects the raster content.