[QGIS Commit] r11189 - trunk/qgis/src/core/raster

svn_qgis at osgeo.org svn_qgis at osgeo.org
Mon Jul 27 22:51:09 EDT 2009


Author: ersts
Date: 2009-07-27 22:51:08 -0400 (Mon, 27 Jul 2009)
New Revision: 11189

Modified:
   trunk/qgis/src/core/raster/qgsrasterlayer.cpp
Log:
-Fix no data value from being included in histogram and stats
-Closes ticket #1380

Modified: trunk/qgis/src/core/raster/qgsrasterlayer.cpp
===================================================================
--- trunk/qgis/src/core/raster/qgsrasterlayer.cpp	2009-07-28 02:04:46 UTC (rev 11188)
+++ trunk/qgis/src/core/raster/qgsrasterlayer.cpp	2009-07-28 02:51:08 UTC (rev 11189)
@@ -70,6 +70,13 @@
 # endif
 #endif
 
+// Comparison value for equality; i.e., we shouldn't directly compare two
+// floats so it's better to take their difference and see if they're within
+// a certain range -- in this case twenty times the smallest value that
+// doubles can take for the current system.  (Yes, 20 was arbitrary.)
+#define TINY_VALUE  std::numeric_limits<double>::epsilon() * 20
+
+
 QgsRasterLayer::QgsRasterLayer(
   QString const & path,
   QString const & baseName,
@@ -106,7 +113,7 @@
 
   mBandCount = 0;
   mHasPyramids = false;
-  mNoDataValue = -9999;
+  mNoDataValue = -9999.0;
   mValidNoDataValue = false;
 
   mGdalBaseDataset = 0;
@@ -759,18 +766,11 @@
   myNXBlocks = ( GDALGetRasterXSize( myGdalBand ) + myXBlockSize - 1 ) / myXBlockSize;
   myNYBlocks = ( GDALGetRasterYSize( myGdalBand ) + myYBlockSize - 1 ) / myYBlockSize;
 
-  void *myData = CPLMalloc( myXBlockSize * myYBlockSize * GDALGetDataTypeSize( myDataType ) / 8 );
+  void *myData = CPLMalloc( myXBlockSize * myYBlockSize * ( GDALGetDataTypeSize( myDataType ) / 8 ) );
 
   // unfortunately we need to make two passes through the data to calculate stddev
   bool myFirstIterationFlag = true;
 
-  // Comparison value for equality; i.e., we shouldn't directly compare two
-  // floats so it's better to take their difference and see if they're within
-  // a certain range -- in this case twenty times the smallest value that
-  // doubles can take for the current system.  (Yes, 20 was arbitrary.)
-  double myPrecision = std::numeric_limits<double>::epsilon() * 20;
-  Q_UNUSED( myPrecision );
-
   //ifdefs below to remove compiler warning about unused vars
 #ifdef QGISDEBUG
   int success;
@@ -841,10 +841,9 @@
       {
         for ( int iX = 0; iX < nXValid; iX++ )
         {
-          double my = readValue( myData, myDataType, iX + iY * myXBlockSize );
+          double myValue = readValue( myData, myDataType, iX + ( iY * myXBlockSize ) );
 
-          //if ( mValidNoDataValue && (fabs(my - mNoDataValue) < myPrecision || my == mNoDataValue || my != my))
-          if ( mValidNoDataValue && ( my == mNoDataValue || my != my ) )
+          if ( mValidNoDataValue && ( fabs( myValue - mNoDataValue ) <= TINY_VALUE || myValue != myValue ) )
           {
             continue; // NULL
           }
@@ -854,23 +853,23 @@
           {
             //this is the first iteration so initialise vars
             myFirstIterationFlag = false;
-            myRasterBandStats.minimumValue = my;
-            myRasterBandStats.maximumValue = my;
+            myRasterBandStats.minimumValue = myValue;
+            myRasterBandStats.maximumValue = myValue;
             ++myRasterBandStats.elementCount;
           }               //end of true part for first iteration check
           else
           {
             //this is done for all subsequent iterations
-            if ( my < myRasterBandStats.minimumValue )
+            if ( myValue < myRasterBandStats.minimumValue )
             {
-              myRasterBandStats.minimumValue = my;
+              myRasterBandStats.minimumValue = myValue;
             }
-            if ( my > myRasterBandStats.maximumValue )
+            if ( myValue > myRasterBandStats.maximumValue )
             {
-              myRasterBandStats.maximumValue = my;
+              myRasterBandStats.maximumValue = myValue;
             }
 
-            myRasterBandStats.sum += my;
+            myRasterBandStats.sum += myValue;
             ++myRasterBandStats.elementCount;
           }               //end of false part for first iteration check
         }
@@ -912,16 +911,15 @@
       {
         for ( int iX = 0; iX < nXValid; iX++ )
         {
-          double my = readValue( myData, myDataType, iX + iY * myXBlockSize );
+          double myValue = readValue( myData, myDataType, iX + ( iY * myXBlockSize ) );
 
-          //if ( mValidNoDataValue && (fabs(my - mNoDataValue) < myPrecision || my == mNoDataValue || my != my))
-          if ( mValidNoDataValue && ( my == mNoDataValue || my != my ) )
+          if ( mValidNoDataValue && ( fabs( myValue - mNoDataValue ) <= TINY_VALUE || myValue != myValue ) )
           {
             continue; // NULL
           }
 
           myRasterBandStats.sumOfSquares += static_cast < double >
-                                            ( pow( my - myRasterBandStats.mean, 2 ) );
+                                            ( pow( myValue - myRasterBandStats.mean, 2 ) );
         }
       }
     }                       //end of column wise loop
@@ -1893,7 +1891,7 @@
 #endif
       QString v;
 
-      if ( mValidNoDataValue && ( mNoDataValue == value || value != value ) )
+      if ( mValidNoDataValue && ( fabs( value - mNoDataValue ) <= TINY_VALUE || value != value ) )
       {
         v = tr( "null (no data)" );
       }
@@ -3071,7 +3069,8 @@
 
 void QgsRasterLayer::resetNoDataValue()
 {
-  mNoDataValue = -9999;
+  mNoDataValue = std::numeric_limits<int>::max();
+  mValidNoDataValue = false;
   if ( mGdalDataset != NULL && GDALGetRasterCount( mGdalDataset ) > 0 )
   {
     int myRequestValid;
@@ -3084,7 +3083,7 @@
     }
     else
     {
-      setNoDataValue( myValue );
+      setNoDataValue( -9999.0 );
       mValidNoDataValue = false;
     }
   }
@@ -4340,7 +4339,7 @@
       myBlueValue  = readValue( myGdalBlueData, ( GDALDataType )myBlueType,
                                 myRow * theRasterViewPort->drawableAreaXDim + myColumn );
 
-      if ( mValidNoDataValue && (( myRedValue == mNoDataValue || myRedValue != myRedValue ) || ( myGreenValue == mNoDataValue || myGreenValue != myGreenValue ) || ( myBlueValue == mNoDataValue || myBlueValue != myBlueValue ) ) )
+      if ( mValidNoDataValue && (( fabs( myRedValue - mNoDataValue ) <= TINY_VALUE || myRedValue != myRedValue ) || ( fabs( myGreenValue - mNoDataValue ) <= TINY_VALUE || myGreenValue != myGreenValue ) || ( fabs( myBlueValue - mNoDataValue ) <= TINY_VALUE || myBlueValue != myBlueValue ) ) )
       {
         myLineBuffer[ myColumn ] = myDefaultColor;
         continue;
@@ -4466,7 +4465,7 @@
       myPixelValue = readValue( myGdalScanData, ( GDALDataType )myDataType,
                                 myRow * theRasterViewPort->drawableAreaXDim + myColumn );
 
-      if ( mValidNoDataValue && ( myPixelValue == mNoDataValue || myPixelValue != myPixelValue ) )
+      if ( mValidNoDataValue && ( fabs( myPixelValue - mNoDataValue ) <= TINY_VALUE || myPixelValue != myPixelValue ) )
       {
         myLineBuffer[ myColumn ] = myDefaultColor;
         continue;
@@ -4554,7 +4553,7 @@
       myPixelValue = readValue( myGdalScanData, ( GDALDataType )myDataType,
                                 myRow * theRasterViewPort->drawableAreaXDim + myColumn );
 
-      if ( mValidNoDataValue && ( myPixelValue == mNoDataValue || myPixelValue != myPixelValue ) )
+      if ( mValidNoDataValue && ( fabs( myPixelValue - mNoDataValue ) <= TINY_VALUE || myPixelValue != myPixelValue ) )
       {
         myLineBuffer[ myColumn ] = myDefaultColor;
         continue;
@@ -4664,7 +4663,7 @@
       myPixelValue = readValue( myGdalScanData, ( GDALDataType )myDataType,
                                 myRow * theRasterViewPort->drawableAreaXDim + myColumn );
 
-      if ( mValidNoDataValue && ( myPixelValue == mNoDataValue || myPixelValue != myPixelValue ) )
+      if ( mValidNoDataValue && ( fabs( myPixelValue - mNoDataValue ) <= TINY_VALUE || myPixelValue != myPixelValue ) )
       {
         myLineBuffer[ myColumn ] = myDefaultColor;
         continue;
@@ -4773,7 +4772,8 @@
       // against myGrayVal will always fail ( nan==nan always
       // returns false, by design), hence the slightly odd comparison
       // of myGrayVal against itself.
-      if ( mValidNoDataValue && ( myGrayValue == mNoDataValue || myGrayValue != myGrayValue ) )
+
+      if ( mValidNoDataValue && ( fabs( myGrayValue - mNoDataValue ) <= TINY_VALUE || myGrayValue != myGrayValue ) )
       {
         myLineBuffer[ myColumn ] = myDefaultColor;
         continue;
@@ -4876,7 +4876,7 @@
       myPixelValue = readValue( myGdalScanData, myDataType,
                                 myRow * theRasterViewPort->drawableAreaXDim + myColumn );
 
-      if ( mValidNoDataValue && ( myPixelValue == mNoDataValue || myPixelValue != myPixelValue ) )
+      if ( mValidNoDataValue && ( fabs( myPixelValue - mNoDataValue ) <= TINY_VALUE || myPixelValue != myPixelValue ) )
       {
         myLineBuffer[ myColumn ] = myDefaultColor;
         continue;
@@ -5198,7 +5198,7 @@
   //
   // Determin the nodatavalue
   //
-  mNoDataValue = -9999; //Standard default?
+  mNoDataValue = -9999.0; //Standard default?
   mValidNoDataValue = false;
   int isValid = false;
   double myNoDataValue = GDALGetRasterNoDataValue( GDALGetRasterBand( mGdalDataset, 1 ), &isValid );



More information about the QGIS-commit mailing list