@@ -515,7 +515,14 @@ void QgsRasterInterface::cumulativeCut( int theBandNo,
515
515
{
516
516
QgsDebugMsg ( QString ( " theBandNo = %1 theLowerCount = %2 theUpperCount = %3 theSampleSize = %4" ).arg ( theBandNo ).arg ( theLowerCount ).arg ( theUpperCount ).arg ( theSampleSize ) );
517
517
518
- QgsRasterHistogram myHistogram = histogram ( theBandNo, 0 , std::numeric_limits<double >::quiet_NaN (), std::numeric_limits<double >::quiet_NaN (), theExtent, theSampleSize );
518
+ int mySrcDataType = srcDataType ( theBandNo );
519
+
520
+ // get band stats to specify real histogram min/max (fix #9793 Byte bands)
521
+ QgsRasterBandStats stats = bandStatistics ( theBandNo, QgsRasterBandStats::Min, theExtent, theSampleSize );
522
+ // for byte bands make sure bin count == actual range
523
+ int myBinCount = ( mySrcDataType == QGis::Byte ) ? int ( ceil ( stats.maximumValue - stats.minimumValue + 1 ) ) : 0 ;
524
+ QgsRasterHistogram myHistogram = histogram ( theBandNo, myBinCount, stats.minimumValue , stats.maximumValue , theExtent, theSampleSize );
525
+ // QgsRasterHistogram myHistogram = histogram( theBandNo, 0, std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), theExtent, theSampleSize );
519
526
520
527
// Init to NaN is better than histogram min/max to catch errors
521
528
theLowerValue = std::numeric_limits<double >::quiet_NaN ();
@@ -537,13 +544,26 @@ void QgsRasterInterface::cumulativeCut( int theBandNo,
537
544
{
538
545
theLowerValue = myHistogram.minimum + myBin * myBinXStep;
539
546
myLowerFound = true ;
547
+ QgsDebugMsg ( QString ( " found lowerValue %1 at bin %2" ).arg ( theLowerValue ).arg ( myBin ) );
540
548
}
541
549
if ( myCount >= myMaxCount )
542
550
{
543
551
theUpperValue = myHistogram.minimum + myBin * myBinXStep;
552
+ QgsDebugMsg ( QString ( " found upperValue %1 at bin %2" ).arg ( theUpperValue ).arg ( myBin ) );
544
553
break ;
545
554
}
546
555
}
556
+
557
+ // fix integer data - round down/up
558
+ if ( mySrcDataType == QGis::Byte ||
559
+ mySrcDataType == QGis::Int16 || mySrcDataType == QGis::Int32 ||
560
+ mySrcDataType == QGis::UInt16 || mySrcDataType == QGis::UInt32 )
561
+ {
562
+ if ( theLowerValue != std::numeric_limits<double >::quiet_NaN () )
563
+ theLowerValue = floor ( theLowerValue );
564
+ if ( theUpperValue != std::numeric_limits<double >::quiet_NaN () )
565
+ theUpperValue = ceil ( theUpperValue );
566
+ }
547
567
}
548
568
549
569
QString QgsRasterInterface::capabilitiesString () const
0 commit comments