Skip to content

Commit 540df9c

Browse files
committedJul 28, 2012
continues removing of stats from raster layer
1 parent 42dc71f commit 540df9c

12 files changed

+211
-132
lines changed
 

‎python/core/qgsrasterbandstats.sip

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ class QgsRasterBandStats
1313
QString bandName;
1414
/** \brief The gdal band number (starts at 1)*/
1515
int bandNumber;
16-
/** \brief A flag to indicate whether this RasterBandStats struct
17-
* is completely populated */
18-
bool statsGathered;
16+
/** \brief Collected statistics */
17+
int statsGathered;
1918
/** \brief The minimum cell value in the raster band. NO_DATA values
2019
* are ignored. This does not use the gdal GetMinimum function. */
2120
double minimumValue;

‎src/core/qgsrasterdataprovider.cpp

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -423,18 +423,20 @@ QgsRasterBandStats QgsRasterDataProvider::bandStatistics( int theBandNo )
423423
}
424424
#endif
425425

426-
QgsRasterBandStats QgsRasterDataProvider::statisticsDefaults( int theBandNo,
426+
void QgsRasterDataProvider::initStatistics( QgsRasterBandStats &theStatistics,
427+
int theBandNo,
428+
int theStats,
427429
const QgsRectangle & theExtent,
428430
int theSampleSize )
429431
{
430432
QgsDebugMsg( QString( "theBandNo = %1 theSampleSize = %2" ).arg( theBandNo ).arg( theSampleSize ) );
431433

432-
QgsRasterBandStats myRasterBandStats;
433-
myRasterBandStats.bandName = generateBandName( theBandNo );
434-
myRasterBandStats.bandNumber = theBandNo;
434+
theStatistics.bandName = generateBandName( theBandNo );
435+
theStatistics.bandNumber = theBandNo;
436+
theStatistics.statsGathered = theStats;
435437

436438
QgsRectangle myExtent = theExtent.isEmpty() ? extent() : theExtent;
437-
myRasterBandStats.extent = myExtent;
439+
theStatistics.extent = myExtent;
438440

439441
if ( theSampleSize > 0 )
440442
{
@@ -452,39 +454,39 @@ QgsRasterBandStats QgsRasterDataProvider::statisticsDefaults( int theBandNo,
452454
}
453455
QgsDebugMsg( QString( "xRes = %1 yRes = %2" ).arg( xRes ).arg( yRes ) );
454456

455-
myRasterBandStats.width = static_cast <int>( myExtent.width() / xRes );
456-
myRasterBandStats.height = static_cast <int>( myExtent.height() / yRes );
457+
theStatistics.width = static_cast <int>( myExtent.width() / xRes );
458+
theStatistics.height = static_cast <int>( myExtent.height() / yRes );
457459
}
458460
else
459461
{
460462
if ( capabilities() & Size )
461463
{
462-
myRasterBandStats.width = xSize();
463-
myRasterBandStats.height = ySize();
464+
theStatistics.width = xSize();
465+
theStatistics.height = ySize();
464466
}
465467
else
466468
{
467-
myRasterBandStats.width = 1000;
468-
myRasterBandStats.height = 1000;
469+
theStatistics.width = 1000;
470+
theStatistics.height = 1000;
469471
}
470472
}
471-
QgsDebugMsg( QString( "myRasterBandStats.width = %1 myRasterBandStats.height = %2" ).arg( myRasterBandStats.width ).arg( myRasterBandStats.height ) );
472-
473-
return myRasterBandStats;
473+
QgsDebugMsg( QString( "theStatistics.width = %1 theStatistics.height = %2" ).arg( theStatistics.width ).arg( theStatistics.height ) );
474474
}
475475

476476
bool QgsRasterDataProvider::hasStatistics( int theBandNo,
477+
int theStats,
477478
const QgsRectangle & theExtent,
478479
int theSampleSize )
479480
{
480-
QgsDebugMsg( QString( "theBandNo = %1 theSampleSize = %2" ).arg( theBandNo ).arg( theSampleSize ) );
481+
QgsDebugMsg( QString( "theBandNo = %1 theStats = %2 theSampleSize = %3" ).arg( theBandNo ).arg( theStats ).arg( theSampleSize ) );
481482
if ( mStatistics.size() == 0 ) return false;
482483

483-
QgsRasterBandStats myRasterBandStats = statisticsDefaults( theBandNo, theExtent, theSampleSize );
484+
QgsRasterBandStats myRasterBandStats;
485+
initStatistics( myRasterBandStats, theBandNo, theStats, theExtent, theSampleSize );
484486

485487
foreach( QgsRasterBandStats stats, mStatistics )
486488
{
487-
if ( stats == myRasterBandStats )
489+
if ( stats.contains( myRasterBandStats ) )
488490
{
489491
QgsDebugMsg( "Has cached statistics." );
490492
return true;
@@ -495,15 +497,20 @@ bool QgsRasterDataProvider::hasStatistics( int theBandNo,
495497

496498
// Find cached
497499
QgsRasterBandStats QgsRasterDataProvider::bandStatistics( int theBandNo,
500+
int theStats,
498501
const QgsRectangle & theExtent,
499502
int theSampleSize )
500503
{
501-
QgsDebugMsg( QString( "theBandNo = %1 theSampleSize = %2" ).arg( theBandNo ).arg( theSampleSize ) );
502-
QgsRasterBandStats myRasterBandStats = statisticsDefaults( theBandNo, theExtent, theSampleSize );
504+
QgsDebugMsg( QString( "theBandNo = %1 theStats = %2 theSampleSize = %3" ).arg( theBandNo ).arg( theStats ).arg( theSampleSize ) );
505+
506+
// TODO: null values set on raster layer!!!
507+
508+
QgsRasterBandStats myRasterBandStats;
509+
initStatistics( myRasterBandStats, theBandNo, theStats, theExtent, theSampleSize );
503510

504511
foreach( QgsRasterBandStats stats, mStatistics )
505512
{
506-
if ( stats == myRasterBandStats )
513+
if ( stats.contains( myRasterBandStats ) )
507514
{
508515
QgsDebugMsg( "Using cached statistics." );
509516
return stats;
@@ -618,57 +625,57 @@ QgsRasterBandStats QgsRasterDataProvider::bandStatistics( int theBandNo,
618625

619626
CPLFree( myData );
620627

621-
myRasterBandStats.statsGathered = true;
628+
myRasterBandStats.statsGathered = QgsRasterBandStats::All;
622629
mStatistics.append( myRasterBandStats );
623630

624631
return myRasterBandStats;
625632
}
626633

627-
QgsRasterHistogram QgsRasterDataProvider::histogramDefaults( int theBandNo,
634+
void QgsRasterDataProvider::initHistogram( QgsRasterHistogram &theHistogram,
635+
int theBandNo,
628636
int theBinCount,
629637
double theMinimum, double theMaximum,
630638
const QgsRectangle & theExtent,
631639
int theSampleSize,
632640
bool theIncludeOutOfRange )
633641
{
634-
QgsRasterHistogram myHistogram;
635-
myHistogram.bandNumber = theBandNo;
636-
myHistogram.minimum = theMinimum;
637-
myHistogram.maximum = theMaximum;
638-
myHistogram.includeOutOfRange = theIncludeOutOfRange;
642+
theHistogram.bandNumber = theBandNo;
643+
theHistogram.minimum = theMinimum;
644+
theHistogram.maximum = theMaximum;
645+
theHistogram.includeOutOfRange = theIncludeOutOfRange;
639646

640647
int mySrcDataType = srcDataType( theBandNo );
641648

642-
if ( qIsNaN( myHistogram.minimum ) )
649+
if ( qIsNaN( theHistogram.minimum ) )
643650
{
644651
if ( mySrcDataType == QgsRasterDataProvider::Byte )
645652
{
646-
myHistogram.minimum = 0; // see histogram() for shift for rounding
653+
theHistogram.minimum = 0; // see histogram() for shift for rounding
647654
}
648655
else
649656
{
650657
// We need statistcs -> avoid histogramDefaults in hasHistogram if possible
651658
// TODO: use approximated statistics if aproximated histogram is requested
652659
// (theSampleSize > 0)
653-
QgsRasterBandStats stats = bandStatistics( theBandNo, theExtent, theSampleSize );
654-
myHistogram.minimum = stats.minimumValue;
660+
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Min, theExtent, theSampleSize );
661+
theHistogram.minimum = stats.minimumValue;
655662
}
656663
}
657-
if ( qIsNaN( myHistogram.maximum ) )
664+
if ( qIsNaN( theHistogram.maximum ) )
658665
{
659666
if ( mySrcDataType == QgsRasterDataProvider::Byte )
660667
{
661-
myHistogram.maximum = 255;
668+
theHistogram.maximum = 255;
662669
}
663670
else
664671
{
665-
QgsRasterBandStats stats = bandStatistics( theBandNo, theExtent, theSampleSize );
666-
myHistogram.maximum = stats.maximumValue;
672+
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Max, theExtent, theSampleSize );
673+
theHistogram.maximum = stats.maximumValue;
667674
}
668675
}
669676

670677
QgsRectangle myExtent = theExtent.isEmpty() ? extent() : theExtent;
671-
myHistogram.extent = myExtent;
678+
theHistogram.extent = myExtent;
672679

673680
if ( theSampleSize > 0 )
674681
{
@@ -686,23 +693,23 @@ QgsRasterHistogram QgsRasterDataProvider::histogramDefaults( int theBandNo,
686693
}
687694
QgsDebugMsg( QString( "xRes = %1 yRes = %2" ).arg( xRes ).arg( yRes ) );
688695

689-
myHistogram.width = static_cast <int>( myExtent.width() / xRes );
690-
myHistogram.height = static_cast <int>( myExtent.height() / yRes );
696+
theHistogram.width = static_cast <int>( myExtent.width() / xRes );
697+
theHistogram.height = static_cast <int>( myExtent.height() / yRes );
691698
}
692699
else
693700
{
694701
if ( capabilities() & Size )
695702
{
696-
myHistogram.width = xSize();
697-
myHistogram.height = ySize();
703+
theHistogram.width = xSize();
704+
theHistogram.height = ySize();
698705
}
699706
else
700707
{
701-
myHistogram.width = 1000;
702-
myHistogram.height = 1000;
708+
theHistogram.width = 1000;
709+
theHistogram.height = 1000;
703710
}
704711
}
705-
QgsDebugMsg( QString( "myHistogram.width = %1 myHistogram.height = %2" ).arg( myHistogram.width ).arg( myHistogram.height ) );
712+
QgsDebugMsg( QString( "theHistogram.width = %1 theHistogram.height = %2" ).arg( theHistogram.width ).arg( theHistogram.height ) );
706713

707714
int myBinCount = theBinCount;
708715
if ( myBinCount == 0 )
@@ -714,14 +721,12 @@ QgsRasterHistogram QgsRasterDataProvider::histogramDefaults( int theBandNo,
714721
else
715722
{
716723
// There is no best default value, to display something reasonable in histogram chart, binCount should be small, OTOH, to get precise data for cumulative cut, the number should be big. Because it is easier to define fixed lower value for the chart, we calc optimum binCount for higher resolution (to avoid calculating that where histogram() is used. In any any case, it does not make sense to use more than width*height;
717-
myBinCount = myHistogram.width * myHistogram.height;
724+
myBinCount = theHistogram.width * theHistogram.height;
718725
if ( myBinCount > 1000 ) myBinCount = 1000;
719726
}
720727
}
721-
myHistogram.binCount = myBinCount;
722-
QgsDebugMsg( QString( "myHistogram.binCount = %1" ).arg( myHistogram.binCount ) );
723-
724-
return myHistogram;
728+
theHistogram.binCount = myBinCount;
729+
QgsDebugMsg( QString( "theHistogram.binCount = %1" ).arg( theHistogram.binCount ) );
725730
}
726731

727732
bool QgsRasterDataProvider::hasHistogram( int theBandNo,
@@ -736,7 +741,8 @@ bool QgsRasterDataProvider::hasHistogram( int theBandNo,
736741
// do other checks which dont need statistics before histogramDefaults()
737742
if ( mHistograms.size() == 0 ) return false;
738743

739-
QgsRasterHistogram myHistogram = histogramDefaults( theBandNo, theBinCount, theMinimum, theMaximum, theExtent, theSampleSize, theIncludeOutOfRange );
744+
QgsRasterHistogram myHistogram;
745+
initHistogram( myHistogram, theBandNo, theBinCount, theMinimum, theMaximum, theExtent, theSampleSize, theIncludeOutOfRange );
740746

741747
foreach( QgsRasterHistogram histogram, mHistograms )
742748
{
@@ -758,7 +764,8 @@ QgsRasterHistogram QgsRasterDataProvider::histogram( int theBandNo,
758764
{
759765
QgsDebugMsg( QString( "theBandNo = %1 theBinCount = %2 theMinimum = %3 theMaximum = %4 theSampleSize = %5" ).arg( theBandNo ).arg( theBinCount ).arg( theMinimum ).arg( theMaximum ).arg( theSampleSize ) );
760766

761-
QgsRasterHistogram myHistogram = histogramDefaults( theBandNo, theBinCount, theMinimum, theMaximum, theExtent, theSampleSize, theIncludeOutOfRange );
767+
QgsRasterHistogram myHistogram;
768+
initHistogram( myHistogram, theBandNo, theBinCount, theMinimum, theMaximum, theExtent, theSampleSize, theIncludeOutOfRange );
762769

763770
// Find cached
764771
foreach( QgsRasterHistogram histogram, mHistograms )

‎src/core/qgsrasterdataprovider.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -359,16 +359,19 @@ class CORE_EXPORT QgsRasterDataProvider : public QgsDataProvider, public QgsRast
359359

360360
/** \brief Get band statistics.
361361
* @param theBandNo The band (number).
362+
* @param theStats Requested statistics
362363
* @param theExtent Extent used to calc histogram, if empty, whole raster extent is used.
363364
* @param theSampleSize Approximate number of cells in sample. If 0, all cells (whole raster will be used). If raster does not have exact size (WCS without exact size for example), provider decides size of sample.
364365
* @return Band statistics.
365366
*/
366367
virtual QgsRasterBandStats bandStatistics( int theBandNo,
368+
int theStats = QgsRasterBandStats::All,
367369
const QgsRectangle & theExtent = QgsRectangle(),
368370
int theSampleSize = 0 );
369371

370372
/** \brief Returns true if histogram is available (cached, already calculated), the parameters are the same as in histogram() */
371373
virtual bool hasStatistics( int theBandNo,
374+
int theStats = QgsRasterBandStats::All,
372375
const QgsRectangle & theExtent = QgsRectangle(),
373376
int theSampleSize = 0 );
374377

@@ -551,18 +554,19 @@ class CORE_EXPORT QgsRasterDataProvider : public QgsDataProvider, public QgsRast
551554
QList <QgsRasterHistogram> mHistograms;
552555

553556
/** Fill in histogram defaults if not specified */
554-
virtual QgsRasterHistogram histogramDefaults( int theBandNo,
555-
int theBinCount = 0,
556-
double theMinimum = std::numeric_limits<double>::quiet_NaN(),
557-
double theMaximum = std::numeric_limits<double>::quiet_NaN(),
558-
const QgsRectangle & theExtent = QgsRectangle(),
559-
int theSampleSize = 0,
560-
bool theIncludeOutOfRange = false );
557+
void initHistogram( QgsRasterHistogram &theHistogram, int theBandNo,
558+
int theBinCount = 0,
559+
double theMinimum = std::numeric_limits<double>::quiet_NaN(),
560+
double theMaximum = std::numeric_limits<double>::quiet_NaN(),
561+
const QgsRectangle & theExtent = QgsRectangle(),
562+
int theSampleSize = 0,
563+
bool theIncludeOutOfRange = false );
561564

562565
/** Fill in statistics defaults if not specified */
563-
virtual QgsRasterBandStats statisticsDefaults( int theBandNo,
564-
const QgsRectangle & theExtent = QgsRectangle(),
565-
int theBinCount = 0 );
566+
void initStatistics( QgsRasterBandStats &theStatistics, int theBandNo,
567+
int theStats = QgsRasterBandStats::All,
568+
const QgsRectangle & theExtent = QgsRectangle(),
569+
int theBinCount = 0 );
566570

567571
};
568572
#endif

‎src/core/raster/qgsrasterbandstats.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,23 @@
3333
class CORE_EXPORT QgsRasterBandStats
3434
{
3535
public:
36+
enum Stats
37+
{
38+
None = 0,
39+
Min = 1,
40+
Max = 1 << 1,
41+
Range = 1 << 2,
42+
Sum = 1 << 3,
43+
Mean = 1 << 4,
44+
StdDev = 1 << 5,
45+
SumOfSquares = 1 << 6,
46+
All = Min | Max | Range | Sum | Mean | StdDev | SumOfSquares
47+
};
48+
3649
QgsRasterBandStats()
3750
{
3851
bandName = "";
39-
statsGathered = false;
52+
statsGathered = None;
4053
minimumValue = std::numeric_limits<double>::max();
4154
maximumValue = std::numeric_limits<double>::min();
4255
range = 0.0;
@@ -50,12 +63,13 @@ class CORE_EXPORT QgsRasterBandStats
5063
}
5164

5265
/*! Compares region, size etc. not collected statistics */
53-
bool operator==( const QgsRasterBandStats &s ) const
66+
bool contains( const QgsRasterBandStats &s ) const
5467
{
5568
return ( s.bandNumber == bandNumber &&
5669
s.extent == extent &&
5770
s.width == width &&
58-
s.height == height );
71+
s.height == height &&
72+
s.statsGathered == ( statsGathered & s.statsGathered ) );
5973
}
6074

6175
/** \brief The name of the band that these stats belong to. */
@@ -88,9 +102,8 @@ class CORE_EXPORT QgsRasterBandStats
88102
/** \brief The standard deviation of the cell values. */
89103
double stdDev;
90104

91-
/** \brief A flag to indicate whether this RasterBandStats struct
92-
* is completely populated */
93-
bool statsGathered;
105+
/** \brief Collected statistics */
106+
int statsGathered;
94107

95108
/** \brief The sum of all cells in the band. NO_DATA values are excluded. */
96109
double sum;

0 commit comments

Comments
 (0)
Please sign in to comment.