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

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sat Mar 14 15:20:50 EDT 2009


Author: ersts
Date: 2009-03-14 15:20:50 -0400 (Sat, 14 Mar 2009)
New Revision: 10278

Modified:
   trunk/qgis/src/core/raster/qgscolorrampshader.cpp
   trunk/qgis/src/core/raster/qgsrasterlayer.cpp
   trunk/qgis/src/core/raster/qgsrasterlayer.h
Log:
-Padded band numbers with leading zeros
-Added patch to keep older project files from breaking
-Closes ticket #1567

Modified: trunk/qgis/src/core/raster/qgscolorrampshader.cpp
===================================================================
--- trunk/qgis/src/core/raster/qgscolorrampshader.cpp	2009-03-14 14:40:16 UTC (rev 10277)
+++ trunk/qgis/src/core/raster/qgscolorrampshader.cpp	2009-03-14 19:20:50 UTC (rev 10278)
@@ -17,7 +17,7 @@
  *   (at your option) any later version.                                   *
  *                                                                         *
  ***************************************************************************/
-#define REALLY_SMALL 0.0000001
+#define DOUBLE_DIFF_THRESHOLD 0.0000001
 
 #include "qgslogger.h"
 
@@ -69,7 +69,7 @@
     {
       mCurrentColorRampItemIndex--;
     }
-    else if ( theValue <= myColorRampItem.value || myTinyDiff <= REALLY_SMALL )
+    else if ( theValue <= myColorRampItem.value || myTinyDiff <= DOUBLE_DIFF_THRESHOLD )
     {
       *theReturnRedValue = myColorRampItem.color.red();
       *theReturnGreenValue = myColorRampItem.color.green();
@@ -106,7 +106,7 @@
     //Start searching from the last index - assumtion is that neighboring pixels tend to be similar values
     myColorRampItem = mColorRampItemList.value( mCurrentColorRampItemIndex );
     myTinyDiff = fabs( theValue - myColorRampItem.value );
-    if ( theValue == myColorRampItem.value || myTinyDiff <= REALLY_SMALL )
+    if ( theValue == myColorRampItem.value || myTinyDiff <= DOUBLE_DIFF_THRESHOLD )
     {
       *theReturnRedValue = myColorRampItem.color.red();
       *theReturnGreenValue = myColorRampItem.color.green();
@@ -160,7 +160,7 @@
     {
       mCurrentColorRampItemIndex--;
     }
-    else if ( mCurrentColorRampItemIndex != 0 && ( theValue <= myColorRampItem.value || myTinyDiff <= REALLY_SMALL ) )
+    else if ( mCurrentColorRampItemIndex != 0 && ( theValue <= myColorRampItem.value || myTinyDiff <= DOUBLE_DIFF_THRESHOLD ) )
     {
       QgsColorRampShader::ColorRampItem myPreviousColorRampItem = mColorRampItemList.value( mCurrentColorRampItemIndex - 1 );
       myCurrentRampRange = myColorRampItem.value - myPreviousColorRampItem.value;

Modified: trunk/qgis/src/core/raster/qgsrasterlayer.cpp
===================================================================
--- trunk/qgis/src/core/raster/qgsrasterlayer.cpp	2009-03-14 14:40:16 UTC (rev 10277)
+++ trunk/qgis/src/core/raster/qgsrasterlayer.cpp	2009-03-14 19:20:50 UTC (rev 10278)
@@ -104,6 +104,7 @@
   mColorShadingAlgorithm = QgsRasterLayer::UndefinedShader;
   mRasterShader = new QgsRasterShader();
 
+  mBandCount = 0;
   mHasPyramids = false;
   mNoDataValue = -9999;
   mValidNoDataValue = false;
@@ -638,7 +639,7 @@
 
 unsigned int QgsRasterLayer::bandCount()
 {
-  return mRasterStatsList.size();
+  return mBandCount;
 }
 
 const QString QgsRasterLayer::bandName( int theBandNo )
@@ -1838,7 +1839,7 @@
     // Outside the raster
     for ( int i = 1; i <= GDALGetRasterCount( mGdalDataset ); i++ )
     {
-      theResults[ tr( "Band%1" ).arg( i )] = tr( "out of extent" );
+      theResults[ generateBandName( i ) ] = tr( "out of extent" );
     }
   }
   else
@@ -1882,8 +1883,9 @@
       {
         v.setNum( value );
       }
-      theResults[tr( "Band%1" ).arg( i )] = v;
 
+     theResults[ generateBandName( i ) ] = v;
+
       CPLFree( data );
     }
   }
@@ -4888,6 +4890,37 @@
   mRasterStatsList.clear();
 }
 
+QString QgsRasterLayer::generateBandName( int theBandNumber )
+{
+  //Calculate magnitude of band count for padding
+  QString myBandName = tr( "Band" ) + " ";
+  int myBandCount = bandCount();
+  int myLeadingZeros = 0;
+  int myWholeNumber = myBandCount / 10;
+  while( myWholeNumber > 0 )
+  {
+    myLeadingZeros++;
+    myWholeNumber = myBandCount / pow( 10, myLeadingZeros + 1 );
+  }
+
+  //Pad the band number of needed
+  int myMagnitude = 0;
+  myWholeNumber = theBandNumber / 10;
+  while( myWholeNumber > 0 )
+  {
+    myMagnitude++;
+    myWholeNumber = theBandNumber / pow( 10, myMagnitude + 1 );
+  }
+
+  for( int myPadder = 0; myPadder < myLeadingZeros - myMagnitude; myPadder++ )
+  {
+    myBandName += "0";
+  }
+  myBandName += QString::number( theBandNumber );
+
+  return myBandName;
+}
+
 /**
  * This method looks to see if a given band name exists.
  *@note This function is no longer really needed and about to be removed
@@ -5166,13 +5199,12 @@
     mRasterTransparency.initializeTransparentPixelList( mNoDataValue );
   }
 
-  //initialise the raster band stats and contrast enhancement vector
-  for ( int i = 1; i <= GDALGetRasterCount( mGdalDataset ); i++ )
+  mBandCount = GDALGetRasterCount( mGdalDataset );
+  for ( int i = 1; i <= mBandCount; i++ )
   {
     GDALRasterBandH myGdalBand = GDALGetRasterBand( mGdalDataset, i );
     QgsRasterBandStats myRasterBandStats;
-    //myRasterBandStats.bandName = myColorQString ;
-    myRasterBandStats.bandName = "Band " + QString::number( i );
+    myRasterBandStats.bandName = generateBandName( i );
     myRasterBandStats.bandNumber = i;
     myRasterBandStats.statsGathered = false;
     myRasterBandStats.histogramVector = new QgsRasterBandStats::HistogramVector();
@@ -5347,12 +5379,34 @@
   }
   QgsDebugMsg( "No matching band name found in raster band stats" );
 
+  QgsDebugMsg( "Testing for non zero-buffered names" );
+  //TODO Remove test in v2.0 or earlier
+  QStringList myBandNameComponents = theBandName.split( " " );
+  if ( myBandNameComponents.size() == 2 )
+  {
+    int myBandNumber = myBandNameComponents.at( 1 ).toInt();
+    if ( myBandNumber > 0 )
+    {
+      QString myBandName = generateBandName( myBandNumber );
+      for ( int myIterator = 0; myIterator < mRasterStatsList.size(); ++myIterator )
+      {
+        //find out the name of this band
+        if ( mRasterStatsList[myIterator].bandName == myBandName )
+        {
+          QgsDebugMsg( "Matching band name found" );
+          return myBandName;
+        }
+      }
+    }
+  }
+
   QgsDebugMsg( "Testing older naming format" );
   //See of the band in an older format #:something.
-  //TODO Remove test in v2.0
+  //TODO Remove test in v2.0 or earlier
+  myBandNameComponents.clear();
   if ( theBandName.contains( ':' ) )
   {
-    QStringList myBandNameComponents = theBandName.split( ":" );
+    myBandNameComponents = theBandName.split( ":" );
     if ( myBandNameComponents.size() == 2 )
     {
       int myBandNumber = myBandNameComponents.at( 0 ).toInt();

Modified: trunk/qgis/src/core/raster/qgsrasterlayer.h
===================================================================
--- trunk/qgis/src/core/raster/qgsrasterlayer.h	2009-03-14 14:40:16 UTC (rev 10277)
+++ trunk/qgis/src/core/raster/qgsrasterlayer.h	2009-03-14 19:20:50 UTC (rev 10278)
@@ -668,6 +668,7 @@
     //
     // Private methods
     //
+
     /** \brief Drawing routine for multiband image  */
     void drawMultiBandColor( QPainter * theQPainter,
                              QgsRasterViewPort * theRasterViewPort,
@@ -724,6 +725,9 @@
     /** \brief Close data set and release related data */
     void closeDataset();
 
+    /** \brief helper function to create zero padded band names */
+    QString  generateBandName( int );
+
     /** \brief Find out whether a given band exists.    */
     bool hasBand( const QString &  theBandName );
 
@@ -759,6 +763,9 @@
     const QString QSTRING_NOT_SET;
     const QString TRSTRING_NOT_SET;
 
+    /** \brief The number of bands in the dataset */
+    int mBandCount;
+
     /** \brief The band to be associated with the color blue - usually 3 */
     QString mBlueBandName;
 



More information about the QGIS-commit mailing list