有点坑爹的GDALComputeRasterMinMax函数

作者:朱金灿

来源:http://blog.csdn.net/clever101

    

        GDALComputeRasterMinMax函数是gdal库为了求取指定波段的极值而提供的接口。最近看了这个接口的源码,发现这个接口有点坑爹。GDALComputeRasterMinMax实际上是调用GDALRasterBand类的virtual double GetMinimum( int *pbSuccess = NULL )virtual double GetMaximum(int *pbSuccess = NULL );两个接口。我们看看GDALRasterBand::GetMinimum函数的实现:


double GDALRasterBand::GetMinimum( int *pbSuccess )

{
    const char *pszValue = NULL;
    
    if( (pszValue = GetMetadataItem("STATISTICS_MINIMUM")) != NULL )
    {
        if( pbSuccess != NULL )
            *pbSuccess = TRUE;
        
        return CPLAtofM(pszValue);
    }

    if( pbSuccess != NULL )
        *pbSuccess = FALSE;

    switch( eDataType )
    {
      case GDT_Byte:
      {
        const char* pszPixelType = GetMetadataItem("PIXELTYPE", "IMAGE_STRUCTURE");
        if (pszPixelType != NULL && EQUAL(pszPixelType, "SIGNEDBYTE"))
            return -128;
        else
            return 0;
      }

      case GDT_UInt16:
        return 0;

      case GDT_Int16:
        return -32768;

      case GDT_Int32:
        return -2147483648.0;

      case GDT_UInt32:
        return 0;

      case GDT_Float32:
        return -4294967295.0; /* not actually accurate */

      case GDT_Float64:
        return -4294967295.0; /* not actually accurate */

      default:
        return -4294967295.0; /* not actually accurate */
    }
}

    这段函数的意义是什么呢?就是说首先从元数据文件(一般是xml文件)中查找是否有最小值记录,如果有就取出来返回;如果没有就把波段类型的值域的最小值返回。这样做就有点坑爹了,因为求取的极值并非来自统计图像而来,就是说除非派生自GDALRasterBand类的波段类重写了GetMinimumGetMaximum两个接口,否则求取的极值基本上是不准确的。我查了一下,geotiff的波段类都没重写这两个接口。因此GDALComputeRasterMinMax这个接口应该慎用。

posted on 2014-11-06 18:25  岚之山  阅读(721)  评论(0编辑  收藏  举报

导航