29#include "moc_qgsrasterinterface.cpp"
46 int sampleSize )
const
48 QgsDebugMsgLevel( QStringLiteral(
"theBandNo = %1 sampleSize = %2" ).arg( bandNo ).arg( sampleSize ), 4 );
62 statistics.
extent = finalExtent;
68 xRes = yRes = std::sqrt( ( finalExtent.
width() * finalExtent.
height() ) / sampleSize );
75 if ( xRes < srcXRes ) xRes = srcXRes;
76 if ( yRes < srcYRes ) yRes = srcYRes;
78 QgsDebugMsgLevel( QStringLiteral(
"xRes = %1 yRes = %2" ).arg( xRes ).arg( yRes ), 4 );
80 statistics.
width =
static_cast <int>( std::ceil( finalExtent.
width() / xRes ) );
81 statistics.
height =
static_cast <int>( std::ceil( finalExtent.
height() / yRes ) );
92 statistics.
width = 1000;
96 QgsDebugMsgLevel( QStringLiteral(
"theStatistics.width = %1 statistics.height = %2" ).arg( statistics.
width ).arg( statistics.
height ), 4 );
104 QgsDebugMsgLevel( QStringLiteral(
"theBandNo = %1 stats = %2 sampleSize = %3" ).arg( bandNo ).arg( stats ).arg( sampleSize ), 4 );
113 if ( stats.contains( myRasterBandStats ) )
127 QgsDebugMsgLevel( QStringLiteral(
"theBandNo = %1 stats = %2 sampleSize = %3" ).arg( bandNo ).arg( stats ).arg( sampleSize ), 4 );
137 if ( stats.contains( myRasterBandStats ) )
145 const int myWidth = myRasterBandStats.
width;
146 const int myHeight = myRasterBandStats.
height;
152 if ( myXBlockSize == 0 )
156 if ( myYBlockSize == 0 )
161 const int myNXBlocks = ( myWidth + myXBlockSize - 1 ) / myXBlockSize;
162 const int myNYBlocks = ( myHeight + myYBlockSize - 1 ) / myYBlockSize;
164 const double myXRes = myExtent.
width() / myWidth;
165 const double myYRes = myExtent.
height() / myHeight;
170 double mySumOfSquares = 0;
172 bool myFirstIterationFlag =
true;
173 bool isNoData =
false;
174 for (
int myYBlock = 0; myYBlock < myNYBlocks; myYBlock++ )
176 for (
int myXBlock = 0; myXBlock < myNXBlocks; myXBlock++ )
179 return myRasterBandStats;
181 QgsDebugMsgLevel( QStringLiteral(
"myYBlock = %1 myXBlock = %2" ).arg( myYBlock ).arg( myXBlock ), 4 );
182 const int myBlockWidth = std::min( myXBlockSize, myWidth - myXBlock * myXBlockSize );
183 const int myBlockHeight = std::min( myYBlockSize, myHeight - myYBlock * myYBlockSize );
185 const double xmin = myExtent.
xMinimum() + myXBlock * myXBlockSize * myXRes;
186 const double xmax = xmin + myBlockWidth * myXRes;
187 const double ymin = myExtent.
yMaximum() - myYBlock * myYBlockSize * myYRes;
188 const double ymax = ymin - myBlockHeight * myYRes;
190 const QgsRectangle myPartExtent( xmin, ymin, xmax, ymax );
192 std::unique_ptr< QgsRasterBlock > blk(
block( bandNo, myPartExtent, myBlockWidth, myBlockHeight, feedback ) );
195 for (
qgssize i = 0; i < ( static_cast< qgssize >( myBlockHeight ) ) * myBlockWidth; i++ )
197 const double myValue = blk->valueAndNoData( i, isNoData );
201 myRasterBandStats.
sum += myValue;
204 if ( !std::isfinite( myValue ) )
continue;
206 if ( myFirstIterationFlag )
208 myFirstIterationFlag =
false;
225 const double myDelta = myValue - myMean;
227 mySumOfSquares += myDelta * ( myValue - myMean );
240 myRasterBandStats.
stdDev = std::sqrt( mySumOfSquares / ( myRasterBandStats.
elementCount - 1 ) );
242 QgsDebugMsgLevel( QStringLiteral(
"************ STATS **************" ), 4 );
252 return myRasterBandStats;
263 double minimum,
double maximum,
266 bool includeOutOfRange )
316 if ( sampleSize > 0 )
320 xRes = yRes = std::sqrt( (
static_cast<double>( finalExtent.
width( ) ) * finalExtent.
height() ) / sampleSize );
327 if ( xRes < srcXRes ) xRes = srcXRes;
328 if ( yRes < srcYRes ) yRes = srcYRes;
330 QgsDebugMsgLevel( QStringLiteral(
"xRes = %1 yRes = %2" ).arg( xRes ).arg( yRes ), 4 );
350 qint64 myBinCount = binCount;
351 if ( myBinCount == 0 )
394 double minimum,
double maximum,
397 bool includeOutOfRange )
399 QgsDebugMsgLevel( QStringLiteral(
"theBandNo = %1 binCount = %2 minimum = %3 maximum = %4 sampleSize = %5" ).arg( bandNo ).arg( binCount ).arg( minimum ).arg( maximum ).arg( sampleSize ), 4 );
405 initHistogram( myHistogram, bandNo, binCount, minimum, maximum,
extent, sampleSize, includeOutOfRange );
421 double minimum,
double maximum,
426 QgsDebugMsgLevel( QStringLiteral(
"theBandNo = %1 binCount = %2 minimum = %3 maximum = %4 sampleSize = %5" ).arg( bandNo ).arg( binCount ).arg( minimum ).arg( maximum ).arg( sampleSize ), 4 );
429 initHistogram( myHistogram, bandNo, binCount, minimum, maximum,
extent, sampleSize, includeOutOfRange );
442 const int myBinCount = myHistogram.
binCount;
443 const int myWidth = myHistogram.
width;
444 const int myHeight = myHistogram.
height;
450 if ( myXBlockSize == 0 )
454 if ( myYBlockSize == 0 )
459 const int myNXBlocks = ( myWidth + myXBlockSize - 1 ) / myXBlockSize;
460 const int myNYBlocks = ( myHeight + myYBlockSize - 1 ) / myYBlockSize;
462 const double myXRes = myExtent.
width() / myWidth;
463 const double myYRes = myExtent.
height() / myHeight;
465 double myMinimum = myHistogram.
minimum;
466 double myMaximum = myHistogram.
maximum;
470 const double myerval = ( myMaximum - myMinimum ) / myHistogram.
binCount;
471 myMinimum -= 0.1 * myerval;
472 myMaximum += 0.1 * myerval;
474 QgsDebugMsgLevel( QStringLiteral(
"binCount = %1 myMinimum = %2 myMaximum = %3" ).arg( myHistogram.
binCount ).arg( myMinimum ).arg( myMaximum ), 4 );
476 const double myBinSize = ( myMaximum - myMinimum ) / myBinCount;
479 bool isNoData =
false;
480 for (
int myYBlock = 0; myYBlock < myNYBlocks; myYBlock++ )
482 for (
int myXBlock = 0; myXBlock < myNXBlocks; myXBlock++ )
487 const int myBlockWidth = std::min( myXBlockSize, myWidth - myXBlock * myXBlockSize );
488 const int myBlockHeight = std::min( myYBlockSize, myHeight - myYBlock * myYBlockSize );
490 const double xmin = myExtent.
xMinimum() + myXBlock * myXBlockSize * myXRes;
491 const double xmax = xmin + myBlockWidth * myXRes;
492 const double ymin = myExtent.
yMaximum() - myYBlock * myYBlockSize * myYRes;
493 const double ymax = ymin - myBlockHeight * myYRes;
495 const QgsRectangle myPartExtent( xmin, ymin, xmax, ymax );
497 std::unique_ptr< QgsRasterBlock > blk(
block( bandNo, myPartExtent, myBlockWidth, myBlockHeight, feedback ) );
500 for (
qgssize i = 0; i < ( static_cast< qgssize >( myBlockHeight ) ) * myBlockWidth; i++ )
502 const double myValue = blk->valueAndNoData( i, isNoData );
508 int myBinIndex =
static_cast <int>( std::floor( ( myValue - myMinimum ) / myBinSize ) );
510 if ( ( myBinIndex < 0 || myBinIndex > ( myBinCount - 1 ) ) && !includeOutOfRange )
514 if ( myBinIndex < 0 ) myBinIndex = 0;
515 if ( myBinIndex > ( myBinCount - 1 ) ) myBinIndex = myBinCount - 1;
523 myHistogram.
valid =
true;
528 for ( std::size_t i = 0; i < std::min< std::size_t >( myHistogram.
histogramVector.size(), 500 ); i++ )
530 hist += QString::number( myHistogram.
histogramVector.value( i ) ) +
' ';
532 QgsDebugMsgLevel( QStringLiteral(
"Histogram (max first 500 bins): " ) + hist, 4 );
539 double lowerCount,
double upperCount,
540 double &lowerValue,
double &upperValue,
544 QgsDebugMsgLevel( QStringLiteral(
"theBandNo = %1 lowerCount = %2 upperCount = %3 sampleSize = %4" ).arg( bandNo ).arg( lowerCount ).arg( upperCount ).arg( sampleSize ), 4 );
549 lowerValue = std::numeric_limits<double>::quiet_NaN();
550 upperValue = std::numeric_limits<double>::quiet_NaN();
564 const int myMinCount =
static_cast< int >( std::round( lowerCount * myHistogram.
nonNullCount ) );
565 const int myMaxCount =
static_cast< int >( std::round( upperCount * myHistogram.
nonNullCount ) );
566 bool myLowerFound =
false;
567 QgsDebugMsgLevel( QStringLiteral(
"binCount = %1 minimum = %2 maximum = %3 myBinXStep = %4" ).arg( myHistogram.
binCount ).arg( myHistogram.
minimum ).arg( myHistogram.
maximum ).arg( myBinXStep ), 4 );
568 QgsDebugMsgLevel( QStringLiteral(
"myMinCount = %1 myMaxCount = %2" ).arg( myMinCount ).arg( myMaxCount ), 4 );
570 for (
int myBin = 0; myBin < myHistogram.
histogramVector.size(); myBin++ )
573 myCount += myBinValue;
574 if ( !myLowerFound && myCount > myMinCount )
576 lowerValue = myHistogram.
minimum + myBin * myBinXStep;
578 QgsDebugMsgLevel( QStringLiteral(
"found lowerValue %1 at bin %2" ).arg( lowerValue ).arg( myBin ), 4 );
580 if ( myCount >= myMaxCount )
582 upperValue = myHistogram.
minimum + myBin * myBinXStep;
583 QgsDebugMsgLevel( QStringLiteral(
"found upperValue %1 at bin %2" ).arg( upperValue ).arg( myBin ), 4 );
593 if ( !std::isnan( lowerValue ) )
594 lowerValue = std::floor( lowerValue );
595 if ( !std::isnan( upperValue ) )
596 upperValue = std::ceil( upperValue );
602 QStringList abilitiesList;
612 abilitiesList += tr(
"Identify" );
617 abilitiesList += tr(
"Build Pyramids" );
620 QgsDebugMsgLevel(
"Capability: " + abilitiesList.join( QLatin1String(
", " ) ), 4 );
622 return abilitiesList.join( QLatin1String(
", " ) );
631 return tr(
"Band" ) + QStringLiteral(
" %1" ) .arg( bandNumber, 1 + (
bandCount() > 0 ?
static_cast< int >( std::log10(
static_cast< double >(
bandCount() ) ) ) : 0 ), 10, QChar(
'0' ) );
646 if ( colorInterp != tr(
"Undefined" ) )
648 name.append( QStringLiteral(
" (%1)" ).arg( colorInterp ) );
660 return mRenderContext;
QFlags< RasterInterfaceCapability > RasterInterfaceCapabilities
Raster interface capabilities.
@ BuildPyramids
Supports building of pyramids (overviews) (Deprecated since QGIS 3.38 – use RasterProviderCapability:...
@ NoCapabilities
No capabilities.
@ Size
Original data source size (and thus resolution) is known, it is not always available,...
@ Identify
At least one identify format supported.
QFlags< RasterBandStatistic > RasterBandStatistics
Statistics to be calculated for raster bands.
@ All
All available statistics.
DataType
Raster data types.
@ Int16
Sixteen bit signed integer (qint16)
@ UInt16
Sixteen bit unsigned integer (quint16)
@ Byte
Eight bit unsigned integer (quint8)
@ Int32
Thirty two bit signed integer (qint32)
@ UInt32
Thirty two bit unsigned integer (quint32)
bool isCanceled() const
Tells whether the operation has been canceled already.
The RasterBandStats struct is a container for statistics about a single raster band.
qgssize elementCount
The number of not no data cells in the band.
int bandNumber
The gdal band number (starts at 1)
double sumOfSquares
The sum of the squares. Used to calculate standard deviation.
int height
Number of rows used to calc statistics.
double mean
The mean cell value for the band. NO_DATA values are excluded.
QgsRectangle extent
Extent used to calc statistics.
double stdDev
The standard deviation of the cell values.
double minimumValue
The minimum cell value in the raster band.
int width
Number of columns used to calc statistics.
double sum
The sum of all cells in the band. NO_DATA values are excluded.
Qgis::RasterBandStatistics statsGathered
Collected statistics.
double maximumValue
The maximum cell value in the raster band.
double range
The range is the distance between min & max.
Feedback object tailored for raster block reading.
QgsRenderContext renderContext() const
Returns the render context of the associated block reading.
void setRenderContext(const QgsRenderContext &renderContext)
Sets the render context of the associated block reading.
The QgsRasterHistogram is a container for histogram of a single raster band.
double minimum
The minimum histogram value.
int bandNumber
The gdal band number (starts at 1)
double maximum
The maximum histogram value.
bool includeOutOfRange
Whether histogram includes out of range values (in first and last bin)
QgsRectangle extent
Extent used to calc histogram.
int nonNullCount
The number of non NULL cells used to calculate histogram.
QgsRasterHistogram::HistogramVector histogramVector
Stores the histogram for a given layer.
int height
Number of rows used to calc histogram.
int width
Number of columns used to calc histogram.
bool valid
Histogram is valid.
int binCount
Number of bins (intervals,buckets) in histogram.
Base class for processing filters like renderers, reprojector, resampler etc.
virtual void cumulativeCut(int bandNo, double lowerCount, double upperCount, double &lowerValue, double &upperValue, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0)
Find values for cumulative pixel count cut.
virtual Qgis::RasterInterfaceCapabilities capabilities() const
Returns the capabilities supported by the interface.
virtual int yBlockSize() const
virtual QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr)=0
Read block of data using given extent and size.
Q_DECL_DEPRECATED void initStatistics(QgsRasterBandStats &statistics, int bandNo, int stats, const QgsRectangle &boundingBox=QgsRectangle(), int binCount=0) const
Fill in statistics defaults if not specified.
QList< QgsRasterBandStats > mStatistics
List of cached statistics, all bands mixed.
Q_DECL_DEPRECATED QString capabilitiesString() const
Returns the raster interface capabilities in friendly format.
void initHistogram(QgsRasterHistogram &histogram, int bandNo, int binCount, double minimum=std::numeric_limits< double >::quiet_NaN(), double maximum=std::numeric_limits< double >::quiet_NaN(), const QgsRectangle &boundingBox=QgsRectangle(), int sampleSize=0, bool includeOutOfRange=false)
Fill in histogram defaults if not specified.
virtual int xSize() const
Gets raster size.
virtual QString generateBandName(int bandNumber) const
helper function to create zero padded band names
QgsRasterInterface(QgsRasterInterface *input=nullptr)
Q_DECL_DEPRECATED QgsRasterBandStats bandStatistics(int bandNo, int stats, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, QgsRasterBlockFeedback *feedback=nullptr)
Returns the band statistics.
virtual Qgis::DataType sourceDataType(int bandNo) const
Returns source data type for the band specified by number, source data type may be shorter than dataT...
virtual int xBlockSize() const
Gets block size.
virtual int bandCount() const =0
Gets number of bands.
virtual bool hasHistogram(int bandNo, int binCount, double minimum=std::numeric_limits< double >::quiet_NaN(), double maximum=std::numeric_limits< double >::quiet_NaN(), const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, bool includeOutOfRange=false)
Returns true if histogram is available (cached, already calculated)
QString displayBandName(int bandNumber) const
Generates a friendly, descriptive name for the specified bandNumber.
Q_DECL_DEPRECATED bool hasStatistics(int bandNo, int stats, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0)
Returns true if histogram is available (cached, already calculated).
QgsRasterInterface * mInput
virtual int ySize() const
virtual QgsRectangle extent() const
Gets the extent of the interface.
QList< QgsRasterHistogram > mHistograms
List of cached histograms, all bands mixed.
virtual QString colorInterpretationName(int bandNumber) const
Returns the name of the color interpretation for the specified bandNumber.
virtual QgsRasterHistogram histogram(int bandNo, int binCount=0, double minimum=std::numeric_limits< double >::quiet_NaN(), double maximum=std::numeric_limits< double >::quiet_NaN(), const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, bool includeOutOfRange=false, QgsRasterBlockFeedback *feedback=nullptr)
Returns a band histogram.
A rectangle specified with double values.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
Contains information about the context of a rendering operation.
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
#define QgsDebugMsgLevel(str, level)