Fixed a stupid bug causing a segfault in hasBand() method. Added a short circuit to calculate stats loop to prevent repeating stats gathering when it has already been done before.

git-svn-id: http://svn.osgeo.org/qgis/trunk@546 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
timlinux 2004-01-21 08:33:53 +00:00
parent ad78d37c9e
commit 5965b8b7e3
2 changed files with 143 additions and 140 deletions

View File

@ -187,12 +187,14 @@ bool QgsRasterLayer::hasBand(QString theBandName)
{
GDALRasterBand *myGdalBand = gdalDataset->GetRasterBand( i );
QString myColorQString = GDALGetColorInterpretationName(myGdalBand->GetColorInterpretation());
std::cout << "band : " << i << std::endl;
if (myColorQString==theBandName)
{
std::cout << "band : " << i << std::endl;
std::cout << "Found band : " << theBandName << std::endl;
return true;
}
std::cout << "Found unmatched band : " << myIterator.key().latin1() << std::endl;
std::cout << "Found unmatched band : " << i << " " << myColorQString << std::endl;
}
return false;
}
@ -993,21 +995,26 @@ void QgsRasterLayer::calculateStats(QString theBandNameQString)
RasterBandStats myRasterBandStats;
GDALRasterBand *myGdalBand = gdalDataset->GetRasterBand( i );
QString myColorInterpretation = GDALGetColorInterpretationName(myGdalBand->GetColorInterpretation());
//see if we are calculating all bands or just a particular band
if (theBandNameQString=="" ||
myColorInterpretation==theBandNameQString ||
(myColorInterpretation=="Palette" &&
( theBandNameQString=="Red" ||
//Decide if we want to process this band...
if (theBandNameQString=="" || //process if no band name was passed to this fn
myColorInterpretation==theBandNameQString || //process if theband name passed to fn matches this band
(myColorInterpretation=="Palette" && //process if the current band is palette and the band name
( theBandNameQString=="Red" || //passed to this fn is red, green or blue
theBandNameQString=="Green" ||
theBandNameQString=="Blue"
)
)
)
{
//check if we have previously gathered stats for this band...
rasterStatsMap[myColorInterpretation];
//declare a colorTable to hold a palette - will only be used of the layer color interp is palette
GDALColorTable *colorTable;
if ( myColorInterpretation=="Palette" )
{
//check if we have previously gathered stats for this band...
RasterBandStats myTempRasterBandrStats = rasterStatsMap[theBandNameQString];
if (myTempRasterBandrStats.statsGatheredFlag) continue; //should revert to outer loop if true
colorTable = myGdalBand->GetColorTable();
myRasterBandStats.bandName=theBandNameQString;
}
@ -1023,17 +1030,7 @@ void QgsRasterLayer::calculateStats(QString theBandNameQString)
myRasterBandStats.elementCountInt=myColsInt*myRowsInt;
myRasterBandStats.noDataDouble=myGdalBand->GetNoDataValue();
//avoid collecting stats for rgb images for now
if (myColorInterpretation=="Gray" ||
myColorInterpretation=="Undefined" ||
(myColorInterpretation=="Palette" &&
( theBandNameQString=="Red" ||
theBandNameQString=="Green" ||
theBandNameQString=="Blue"
)
)
)
{
//allocate a buffer to hold one row of ints
int myAllocationSizeInt = sizeof(uint)*myColsInt;
uint * myScanlineAllocInt = (uint*) CPLMalloc(myAllocationSizeInt);
@ -1159,7 +1156,6 @@ void QgsRasterLayer::calculateStats(QString theBandNameQString)
myRasterBandStats.stdDevDouble = static_cast<double>(sqrt(myRasterBandStats.sumSqrDevDouble /
(myRasterBandStats.elementCountInt - 1)));
CPLFree(myScanlineAllocInt);
}//end of gray / undefined raster color interp check
//add this band to the class stats map
rasterStatsMap[myColorInterpretation]=myRasterBandStats;
}//end of "" / bandNameQString check

View File

@ -66,6 +66,7 @@ class GDALRasterBand;
struct RasterBandStats
{
QString bandName;
bool statsGatheredFlag; //use so we can only gather stats once for a layer
int bandNo;
double minValDouble;
double maxValDouble;
@ -299,6 +300,12 @@ public:
void setRasterLayerType( RASTER_LAYER_TYPE theRasterLayerType ) { rasterLayerType=theRasterLayerType; };
//get a legend image for this layer
QPixmap getLegendQPixmap();
//similar to above but returns a pointer. Implemented for qgsmaplayer interface
QPixmap * legendPixmap()
{
QPixmap myQPixmap = getLegendQPixmap();
return new QPixmap(myQPixmap);
};
// emit a signal asking for a repaint
void triggerRepaint();