[gdal-dev] Compute statistis and ignoring nodata value

Even Rouault even.rouault at mines-paris.org
Wed Apr 14 16:31:01 EDT 2010


Michael,

ComputeStatistics() is supposed to ignore pixels whose value is the nodata 
value. I've tried the following test code to try to reproduce your issue :

#include <gdal.h>

int main(int argc, char* argv[])
{
    GDALAllRegister();
    GDALDatasetH hDS = GDALCreate(GDALGetDriverByName("GTiff"), "test.tif", 3, 
1, 1, GDT_Byte, NULL);
    GDALRasterBandH hBand = GDALGetRasterBand(hDS, 1);
    char chVal;
    chVal = 0;
    GDALRasterIO(hBand, GF_Write, 0, 0, 1, 1, &chVal, 1, 1, GDT_Byte, 0, 0);
    chVal = 100;
    GDALRasterIO(hBand, GF_Write, 1, 0, 1, 1, &chVal, 1, 1, GDT_Byte, 0, 0);
    chVal = 2;
    GDALRasterIO(hBand, GF_Write, 2, 0, 1, 1, &chVal, 1, 1, GDT_Byte, 0, 0);
    GDALSetRasterNoDataValue(hBand, 100.0);
    double min, max, mean, stddev;
    GDALComputeRasterStatistics(hBand, FALSE, &min, &max, &mean, &stddev, 
NULL, NULL);
    printf("min=%f max=%f mean=%f stddev=%f\n", min, max, mean, stddev);
    return 0;
}

(a 3x1 dataset with pixels values being 0, 100 and 2 and 100 set as the nodata 
value)

The results I get are : min=0.000000 max=2.000000 mean=1.000000 
stddev=1.000000. Which is the expected values.

I'd advise you to check that the nodata value that you set at the band level 
matches exactly the value used by the nodata pixels in the imagery. 
ComputeStatistics() uses strict equality comparison, so even a very slight 
difference might result in not recognizing the pixels as being nodata. 

Best regards,

Even

Le Wednesday 14 April 2010 19:35:47 Michael ODonnell, vous avez écrit :
> I am trying to compute and set the statistics for a data set that I have
> created using GDAL, but the no data values are being included. Is there a
> way to ignore the no data value when computing the statistics. The
> documentation does not say anything about this, to the best of my
> knowledge, and it appears to be included in my results. Everything is
> working except that no data is included during the use of
> ComputeStatistics.
>
> Here are the steps I am using:
>
> g_MBand->SetNoDataValue(g_nulldata2);
> g_MBand->ComputeStatistics(FALSE, &pdfMin2, &pdfMax2, &pdfMean2,
> &pdfStdDev2, NULL, NULL); g_InBand->SetStatistics(pdfMin2, pdfMax2,
> pdfMean2, pdfStdDev2);
>
> Thank your for your help!
> Mike
>
>
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev




More information about the gdal-dev mailing list