Skip to content

Commit 3e4a915

Browse files
committed
fix raster cumulative cut for byte bands (#9793) and make sure values are rounded down/up for integer bands
1 parent 2d73751 commit 3e4a915

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/core/raster/qgsrasterinterface.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,14 @@ void QgsRasterInterface::cumulativeCut( int theBandNo,
515515
{
516516
QgsDebugMsg( QString( "theBandNo = %1 theLowerCount = %2 theUpperCount = %3 theSampleSize = %4" ).arg( theBandNo ).arg( theLowerCount ).arg( theUpperCount ).arg( theSampleSize ) );
517517

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 );
519526

520527
// Init to NaN is better than histogram min/max to catch errors
521528
theLowerValue = std::numeric_limits<double>::quiet_NaN();
@@ -537,13 +544,26 @@ void QgsRasterInterface::cumulativeCut( int theBandNo,
537544
{
538545
theLowerValue = myHistogram.minimum + myBin * myBinXStep;
539546
myLowerFound = true;
547+
QgsDebugMsg( QString( "found lowerValue %1 at bin %2" ).arg( theLowerValue ).arg( myBin ) );
540548
}
541549
if ( myCount >= myMaxCount )
542550
{
543551
theUpperValue = myHistogram.minimum + myBin * myBinXStep;
552+
QgsDebugMsg( QString( "found upperValue %1 at bin %2" ).arg( theUpperValue ).arg( myBin ) );
544553
break;
545554
}
546555
}
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+
}
547567
}
548568

549569
QString QgsRasterInterface::capabilitiesString() const

0 commit comments

Comments
 (0)