Skip to content

Commit 23f865a

Browse files
author
ersts
committed
-Padded band numbers with leading zeros
-Added patch to keep older project files from breaking -Closes ticket #1567 git-svn-id: http://svn.osgeo.org/qgis/trunk@10278 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent c620953 commit 23f865a

File tree

3 files changed

+74
-13
lines changed

3 files changed

+74
-13
lines changed

src/core/raster/qgscolorrampshader.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ originally part of the larger QgsRasterLayer class
1717
* (at your option) any later version. *
1818
* *
1919
***************************************************************************/
20-
#define REALLY_SMALL 0.0000001
20+
#define DOUBLE_DIFF_THRESHOLD 0.0000001
2121

2222
#include "qgslogger.h"
2323

@@ -69,7 +69,7 @@ bool QgsColorRampShader::discreteColor( double theValue, int* theReturnRedValue,
6969
{
7070
mCurrentColorRampItemIndex--;
7171
}
72-
else if ( theValue <= myColorRampItem.value || myTinyDiff <= REALLY_SMALL )
72+
else if ( theValue <= myColorRampItem.value || myTinyDiff <= DOUBLE_DIFF_THRESHOLD )
7373
{
7474
*theReturnRedValue = myColorRampItem.color.red();
7575
*theReturnGreenValue = myColorRampItem.color.green();
@@ -106,7 +106,7 @@ bool QgsColorRampShader::exactColor( double theValue, int* theReturnRedValue, in
106106
//Start searching from the last index - assumtion is that neighboring pixels tend to be similar values
107107
myColorRampItem = mColorRampItemList.value( mCurrentColorRampItemIndex );
108108
myTinyDiff = fabs( theValue - myColorRampItem.value );
109-
if ( theValue == myColorRampItem.value || myTinyDiff <= REALLY_SMALL )
109+
if ( theValue == myColorRampItem.value || myTinyDiff <= DOUBLE_DIFF_THRESHOLD )
110110
{
111111
*theReturnRedValue = myColorRampItem.color.red();
112112
*theReturnGreenValue = myColorRampItem.color.green();
@@ -160,7 +160,7 @@ bool QgsColorRampShader::interpolatedColor( double theValue, int* theReturnRedVa
160160
{
161161
mCurrentColorRampItemIndex--;
162162
}
163-
else if ( mCurrentColorRampItemIndex != 0 && ( theValue <= myColorRampItem.value || myTinyDiff <= REALLY_SMALL ) )
163+
else if ( mCurrentColorRampItemIndex != 0 && ( theValue <= myColorRampItem.value || myTinyDiff <= DOUBLE_DIFF_THRESHOLD ) )
164164
{
165165
QgsColorRampShader::ColorRampItem myPreviousColorRampItem = mColorRampItemList.value( mCurrentColorRampItemIndex - 1 );
166166
myCurrentRampRange = myColorRampItem.value - myPreviousColorRampItem.value;

src/core/raster/qgsrasterlayer.cpp

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ QgsRasterLayer::QgsRasterLayer(
104104
mColorShadingAlgorithm = QgsRasterLayer::UndefinedShader;
105105
mRasterShader = new QgsRasterShader();
106106

107+
mBandCount = 0;
107108
mHasPyramids = false;
108109
mNoDataValue = -9999;
109110
mValidNoDataValue = false;
@@ -638,7 +639,7 @@ int CPL_STDCALL progressCallback( double dfComplete,
638639

639640
unsigned int QgsRasterLayer::bandCount()
640641
{
641-
return mRasterStatsList.size();
642+
return mBandCount;
642643
}
643644

644645
const QString QgsRasterLayer::bandName( int theBandNo )
@@ -1838,7 +1839,7 @@ bool QgsRasterLayer::identify( const QgsPoint& thePoint, QMap<QString, QString>&
18381839
// Outside the raster
18391840
for ( int i = 1; i <= GDALGetRasterCount( mGdalDataset ); i++ )
18401841
{
1841-
theResults[ tr( "Band%1" ).arg( i )] = tr( "out of extent" );
1842+
theResults[ generateBandName( i ) ] = tr( "out of extent" );
18421843
}
18431844
}
18441845
else
@@ -1882,7 +1883,8 @@ bool QgsRasterLayer::identify( const QgsPoint& thePoint, QMap<QString, QString>&
18821883
{
18831884
v.setNum( value );
18841885
}
1885-
theResults[tr( "Band%1" ).arg( i )] = v;
1886+
1887+
theResults[ generateBandName( i ) ] = v;
18861888

18871889
CPLFree( data );
18881890
}
@@ -4888,6 +4890,37 @@ void QgsRasterLayer::closeDataset()
48884890
mRasterStatsList.clear();
48894891
}
48904892

4893+
QString QgsRasterLayer::generateBandName( int theBandNumber )
4894+
{
4895+
//Calculate magnitude of band count for padding
4896+
QString myBandName = tr( "Band" ) + " ";
4897+
int myBandCount = bandCount();
4898+
int myLeadingZeros = 0;
4899+
int myWholeNumber = myBandCount / 10;
4900+
while( myWholeNumber > 0 )
4901+
{
4902+
myLeadingZeros++;
4903+
myWholeNumber = myBandCount / pow( 10, myLeadingZeros + 1 );
4904+
}
4905+
4906+
//Pad the band number of needed
4907+
int myMagnitude = 0;
4908+
myWholeNumber = theBandNumber / 10;
4909+
while( myWholeNumber > 0 )
4910+
{
4911+
myMagnitude++;
4912+
myWholeNumber = theBandNumber / pow( 10, myMagnitude + 1 );
4913+
}
4914+
4915+
for( int myPadder = 0; myPadder < myLeadingZeros - myMagnitude; myPadder++ )
4916+
{
4917+
myBandName += "0";
4918+
}
4919+
myBandName += QString::number( theBandNumber );
4920+
4921+
return myBandName;
4922+
}
4923+
48914924
/**
48924925
* This method looks to see if a given band name exists.
48934926
*@note This function is no longer really needed and about to be removed
@@ -5166,13 +5199,12 @@ bool QgsRasterLayer::readFile( QString const &theFilename )
51665199
mRasterTransparency.initializeTransparentPixelList( mNoDataValue );
51675200
}
51685201

5169-
//initialise the raster band stats and contrast enhancement vector
5170-
for ( int i = 1; i <= GDALGetRasterCount( mGdalDataset ); i++ )
5202+
mBandCount = GDALGetRasterCount( mGdalDataset );
5203+
for ( int i = 1; i <= mBandCount; i++ )
51715204
{
51725205
GDALRasterBandH myGdalBand = GDALGetRasterBand( mGdalDataset, i );
51735206
QgsRasterBandStats myRasterBandStats;
5174-
//myRasterBandStats.bandName = myColorQString ;
5175-
myRasterBandStats.bandName = "Band " + QString::number( i );
5207+
myRasterBandStats.bandName = generateBandName( i );
51765208
myRasterBandStats.bandNumber = i;
51775209
myRasterBandStats.statsGathered = false;
51785210
myRasterBandStats.histogramVector = new QgsRasterBandStats::HistogramVector();
@@ -5347,12 +5379,34 @@ QString QgsRasterLayer::validateBandName( QString const & theBandName )
53475379
}
53485380
QgsDebugMsg( "No matching band name found in raster band stats" );
53495381

5382+
QgsDebugMsg( "Testing for non zero-buffered names" );
5383+
//TODO Remove test in v2.0 or earlier
5384+
QStringList myBandNameComponents = theBandName.split( " " );
5385+
if ( myBandNameComponents.size() == 2 )
5386+
{
5387+
int myBandNumber = myBandNameComponents.at( 1 ).toInt();
5388+
if ( myBandNumber > 0 )
5389+
{
5390+
QString myBandName = generateBandName( myBandNumber );
5391+
for ( int myIterator = 0; myIterator < mRasterStatsList.size(); ++myIterator )
5392+
{
5393+
//find out the name of this band
5394+
if ( mRasterStatsList[myIterator].bandName == myBandName )
5395+
{
5396+
QgsDebugMsg( "Matching band name found" );
5397+
return myBandName;
5398+
}
5399+
}
5400+
}
5401+
}
5402+
53505403
QgsDebugMsg( "Testing older naming format" );
53515404
//See of the band in an older format #:something.
5352-
//TODO Remove test in v2.0
5405+
//TODO Remove test in v2.0 or earlier
5406+
myBandNameComponents.clear();
53535407
if ( theBandName.contains( ':' ) )
53545408
{
5355-
QStringList myBandNameComponents = theBandName.split( ":" );
5409+
myBandNameComponents = theBandName.split( ":" );
53565410
if ( myBandNameComponents.size() == 2 )
53575411
{
53585412
int myBandNumber = myBandNameComponents.at( 0 ).toInt();

src/core/raster/qgsrasterlayer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
668668
//
669669
// Private methods
670670
//
671+
671672
/** \brief Drawing routine for multiband image */
672673
void drawMultiBandColor( QPainter * theQPainter,
673674
QgsRasterViewPort * theRasterViewPort,
@@ -724,6 +725,9 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
724725
/** \brief Close data set and release related data */
725726
void closeDataset();
726727

728+
/** \brief helper function to create zero padded band names */
729+
QString generateBandName( int );
730+
727731
/** \brief Find out whether a given band exists. */
728732
bool hasBand( const QString & theBandName );
729733

@@ -759,6 +763,9 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
759763
const QString QSTRING_NOT_SET;
760764
const QString TRSTRING_NOT_SET;
761765

766+
/** \brief The number of bands in the dataset */
767+
int mBandCount;
768+
762769
/** \brief The band to be associated with the color blue - usually 3 */
763770
QString mBlueBandName;
764771

0 commit comments

Comments
 (0)