@@ -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
476476bool 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
497499QgsRasterBandStats 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
727732bool 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 )
0 commit comments