@@ -423,18 +423,20 @@ QgsRasterBandStats QgsRasterDataProvider::bandStatistics( int theBandNo )
423
423
}
424
424
#endif
425
425
426
- QgsRasterBandStats QgsRasterDataProvider::statisticsDefaults ( int theBandNo,
426
+ void QgsRasterDataProvider::initStatistics ( QgsRasterBandStats &theStatistics,
427
+ int theBandNo,
428
+ int theStats,
427
429
const QgsRectangle & theExtent,
428
430
int theSampleSize )
429
431
{
430
432
QgsDebugMsg ( QString ( " theBandNo = %1 theSampleSize = %2" ).arg ( theBandNo ).arg ( theSampleSize ) );
431
433
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 ;
435
437
436
438
QgsRectangle myExtent = theExtent.isEmpty () ? extent () : theExtent;
437
- myRasterBandStats .extent = myExtent;
439
+ theStatistics .extent = myExtent;
438
440
439
441
if ( theSampleSize > 0 )
440
442
{
@@ -452,39 +454,39 @@ QgsRasterBandStats QgsRasterDataProvider::statisticsDefaults( int theBandNo,
452
454
}
453
455
QgsDebugMsg ( QString ( " xRes = %1 yRes = %2" ).arg ( xRes ).arg ( yRes ) );
454
456
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 );
457
459
}
458
460
else
459
461
{
460
462
if ( capabilities () & Size )
461
463
{
462
- myRasterBandStats .width = xSize ();
463
- myRasterBandStats .height = ySize ();
464
+ theStatistics .width = xSize ();
465
+ theStatistics .height = ySize ();
464
466
}
465
467
else
466
468
{
467
- myRasterBandStats .width = 1000 ;
468
- myRasterBandStats .height = 1000 ;
469
+ theStatistics .width = 1000 ;
470
+ theStatistics .height = 1000 ;
469
471
}
470
472
}
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 ) );
474
474
}
475
475
476
476
bool QgsRasterDataProvider::hasStatistics ( int theBandNo,
477
+ int theStats,
477
478
const QgsRectangle & theExtent,
478
479
int theSampleSize )
479
480
{
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 ) );
481
482
if ( mStatistics .size () == 0 ) return false ;
482
483
483
- QgsRasterBandStats myRasterBandStats = statisticsDefaults ( theBandNo, theExtent, theSampleSize );
484
+ QgsRasterBandStats myRasterBandStats;
485
+ initStatistics ( myRasterBandStats, theBandNo, theStats, theExtent, theSampleSize );
484
486
485
487
foreach ( QgsRasterBandStats stats, mStatistics )
486
488
{
487
- if ( stats == myRasterBandStats )
489
+ if ( stats. contains ( myRasterBandStats ) )
488
490
{
489
491
QgsDebugMsg ( " Has cached statistics." );
490
492
return true ;
@@ -495,15 +497,20 @@ bool QgsRasterDataProvider::hasStatistics( int theBandNo,
495
497
496
498
// Find cached
497
499
QgsRasterBandStats QgsRasterDataProvider::bandStatistics ( int theBandNo,
500
+ int theStats,
498
501
const QgsRectangle & theExtent,
499
502
int theSampleSize )
500
503
{
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 );
503
510
504
511
foreach ( QgsRasterBandStats stats, mStatistics )
505
512
{
506
- if ( stats == myRasterBandStats )
513
+ if ( stats. contains ( myRasterBandStats ) )
507
514
{
508
515
QgsDebugMsg ( " Using cached statistics." );
509
516
return stats;
@@ -618,57 +625,57 @@ QgsRasterBandStats QgsRasterDataProvider::bandStatistics( int theBandNo,
618
625
619
626
CPLFree ( myData );
620
627
621
- myRasterBandStats.statsGathered = true ;
628
+ myRasterBandStats.statsGathered = QgsRasterBandStats::All ;
622
629
mStatistics .append ( myRasterBandStats );
623
630
624
631
return myRasterBandStats;
625
632
}
626
633
627
- QgsRasterHistogram QgsRasterDataProvider::histogramDefaults ( int theBandNo,
634
+ void QgsRasterDataProvider::initHistogram ( QgsRasterHistogram &theHistogram,
635
+ int theBandNo,
628
636
int theBinCount,
629
637
double theMinimum, double theMaximum,
630
638
const QgsRectangle & theExtent,
631
639
int theSampleSize,
632
640
bool theIncludeOutOfRange )
633
641
{
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;
639
646
640
647
int mySrcDataType = srcDataType ( theBandNo );
641
648
642
- if ( qIsNaN ( myHistogram .minimum ) )
649
+ if ( qIsNaN ( theHistogram .minimum ) )
643
650
{
644
651
if ( mySrcDataType == QgsRasterDataProvider::Byte )
645
652
{
646
- myHistogram .minimum = 0 ; // see histogram() for shift for rounding
653
+ theHistogram .minimum = 0 ; // see histogram() for shift for rounding
647
654
}
648
655
else
649
656
{
650
657
// We need statistcs -> avoid histogramDefaults in hasHistogram if possible
651
658
// TODO: use approximated statistics if aproximated histogram is requested
652
659
// (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 ;
655
662
}
656
663
}
657
- if ( qIsNaN ( myHistogram .maximum ) )
664
+ if ( qIsNaN ( theHistogram .maximum ) )
658
665
{
659
666
if ( mySrcDataType == QgsRasterDataProvider::Byte )
660
667
{
661
- myHistogram .maximum = 255 ;
668
+ theHistogram .maximum = 255 ;
662
669
}
663
670
else
664
671
{
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 ;
667
674
}
668
675
}
669
676
670
677
QgsRectangle myExtent = theExtent.isEmpty () ? extent () : theExtent;
671
- myHistogram .extent = myExtent;
678
+ theHistogram .extent = myExtent;
672
679
673
680
if ( theSampleSize > 0 )
674
681
{
@@ -686,23 +693,23 @@ QgsRasterHistogram QgsRasterDataProvider::histogramDefaults( int theBandNo,
686
693
}
687
694
QgsDebugMsg ( QString ( " xRes = %1 yRes = %2" ).arg ( xRes ).arg ( yRes ) );
688
695
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 );
691
698
}
692
699
else
693
700
{
694
701
if ( capabilities () & Size )
695
702
{
696
- myHistogram .width = xSize ();
697
- myHistogram .height = ySize ();
703
+ theHistogram .width = xSize ();
704
+ theHistogram .height = ySize ();
698
705
}
699
706
else
700
707
{
701
- myHistogram .width = 1000 ;
702
- myHistogram .height = 1000 ;
708
+ theHistogram .width = 1000 ;
709
+ theHistogram .height = 1000 ;
703
710
}
704
711
}
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 ) );
706
713
707
714
int myBinCount = theBinCount;
708
715
if ( myBinCount == 0 )
@@ -714,14 +721,12 @@ QgsRasterHistogram QgsRasterDataProvider::histogramDefaults( int theBandNo,
714
721
else
715
722
{
716
723
// 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 ;
718
725
if ( myBinCount > 1000 ) myBinCount = 1000 ;
719
726
}
720
727
}
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 ) );
725
730
}
726
731
727
732
bool QgsRasterDataProvider::hasHistogram ( int theBandNo,
@@ -736,7 +741,8 @@ bool QgsRasterDataProvider::hasHistogram( int theBandNo,
736
741
// do other checks which dont need statistics before histogramDefaults()
737
742
if ( mHistograms .size () == 0 ) return false ;
738
743
739
- QgsRasterHistogram myHistogram = histogramDefaults ( theBandNo, theBinCount, theMinimum, theMaximum, theExtent, theSampleSize, theIncludeOutOfRange );
744
+ QgsRasterHistogram myHistogram;
745
+ initHistogram ( myHistogram, theBandNo, theBinCount, theMinimum, theMaximum, theExtent, theSampleSize, theIncludeOutOfRange );
740
746
741
747
foreach ( QgsRasterHistogram histogram, mHistograms )
742
748
{
@@ -758,7 +764,8 @@ QgsRasterHistogram QgsRasterDataProvider::histogram( int theBandNo,
758
764
{
759
765
QgsDebugMsg ( QString ( " theBandNo = %1 theBinCount = %2 theMinimum = %3 theMaximum = %4 theSampleSize = %5" ).arg ( theBandNo ).arg ( theBinCount ).arg ( theMinimum ).arg ( theMaximum ).arg ( theSampleSize ) );
760
766
761
- QgsRasterHistogram myHistogram = histogramDefaults ( theBandNo, theBinCount, theMinimum, theMaximum, theExtent, theSampleSize, theIncludeOutOfRange );
767
+ QgsRasterHistogram myHistogram;
768
+ initHistogram ( myHistogram, theBandNo, theBinCount, theMinimum, theMaximum, theExtent, theSampleSize, theIncludeOutOfRange );
762
769
763
770
// Find cached
764
771
foreach ( QgsRasterHistogram histogram, mHistograms )
0 commit comments