48 QgsDebugMsg( QString(
"theBandNo = %1 theSampleSize = %2" ).arg( theBandNo ).arg( theSampleSize ) );
62 theStatistics.
extent = myExtent;
64 if ( theSampleSize > 0 )
68 xRes = yRes = sqrt(( myExtent.
width() * myExtent.
height() ) / theSampleSize );
75 if ( xRes < srcXRes ) xRes = srcXRes;
76 if ( yRes < srcYRes ) yRes = srcYRes;
78 QgsDebugMsg( QString(
"xRes = %1 yRes = %2" ).arg( xRes ).arg( yRes ) );
80 theStatistics.
width = static_cast <
int>( myExtent.
width() / xRes );
81 theStatistics.
height = static_cast <
int>( myExtent.
height() / yRes );
92 theStatistics.
width = 1000;
93 theStatistics.
height = 1000;
96 QgsDebugMsg( QString(
"theStatistics.width = %1 theStatistics.height = %2" ).arg( theStatistics.
width ).arg( theStatistics.
height ) );
104 QgsDebugMsg( QString(
"theBandNo = %1 theStats = %2 theSampleSize = %3" ).arg( theBandNo ).arg( theStats ).arg( theSampleSize ) );
108 initStatistics( myRasterBandStats, theBandNo, theStats, theExtent, theSampleSize );
112 if ( stats.
contains( myRasterBandStats ) )
126 QgsDebugMsg( QString(
"theBandNo = %1 theStats = %2 theSampleSize = %3" ).arg( theBandNo ).arg( theStats ).arg( theSampleSize ) );
131 initStatistics( myRasterBandStats, theBandNo, theStats, theExtent, theSampleSize );
135 if ( stats.
contains( myRasterBandStats ) )
143 int myWidth = myRasterBandStats.
width;
144 int myHeight = myRasterBandStats.
height;
150 if ( myXBlockSize == 0 )
154 if ( myYBlockSize == 0 )
159 int myNXBlocks = ( myWidth + myXBlockSize - 1 ) / myXBlockSize;
160 int myNYBlocks = ( myHeight + myYBlockSize - 1 ) / myYBlockSize;
162 double myXRes = myExtent.
width() / myWidth;
163 double myYRes = myExtent.
height() / myHeight;
168 double mySumOfSquares = 0;
170 bool myFirstIterationFlag =
true;
171 for (
int myYBlock = 0; myYBlock < myNYBlocks; myYBlock++ )
173 for (
int myXBlock = 0; myXBlock < myNXBlocks; myXBlock++ )
175 QgsDebugMsg( QString(
"myYBlock = %1 myXBlock = %2" ).arg( myYBlock ).arg( myXBlock ) );
176 int myBlockWidth = qMin( myXBlockSize, myWidth - myXBlock * myXBlockSize );
177 int myBlockHeight = qMin( myYBlockSize, myHeight - myYBlock * myYBlockSize );
179 double xmin = myExtent.
xMinimum() + myXBlock * myXBlockSize * myXRes;
180 double xmax = xmin + myBlockWidth * myXRes;
181 double ymin = myExtent.
yMaximum() - myYBlock * myYBlockSize * myYRes;
182 double ymax = ymin - myBlockHeight * myYRes;
189 for (
qgssize i = 0; i < ((
qgssize ) myBlockHeight ) * myBlockWidth; i++ )
193 double myValue = blk->
value( i );
195 myRasterBandStats.
sum += myValue;
198 if ( myFirstIterationFlag )
200 myFirstIterationFlag =
false;
217 double myDelta = myValue - myMean;
219 mySumOfSquares += myDelta * ( myValue - myMean );
233 myRasterBandStats.
stdDev = sqrt( mySumOfSquares / ( myRasterBandStats.
elementCount - 1 ) );
235 QgsDebugMsg(
"************ STATS **************" );
245 return myRasterBandStats;
251 double theMinimum,
double theMaximum,
254 bool theIncludeOutOfRange )
257 theHistogram.
minimum = theMinimum;
258 theHistogram.
maximum = theMaximum;
263 if ( qIsNaN( theHistogram.
minimum ) )
280 if ( qIsNaN( theHistogram.
maximum ) )
302 theHistogram.
extent = myExtent;
304 if ( theSampleSize > 0 )
308 xRes = yRes = sqrt(( myExtent.
width() * myExtent.
height() ) / theSampleSize );
315 if ( xRes < srcXRes ) xRes = srcXRes;
316 if ( yRes < srcYRes ) yRes = srcYRes;
318 QgsDebugMsg( QString(
"xRes = %1 yRes = %2" ).arg( xRes ).arg( yRes ) );
320 theHistogram.
width = static_cast <
int>( myExtent.
width() / xRes );
321 theHistogram.
height = static_cast <
int>( myExtent.
height() / yRes );
332 theHistogram.
width = 1000;
333 theHistogram.
height = 1000;
336 QgsDebugMsg( QString(
"theHistogram.width = %1 theHistogram.height = %2" ).arg( theHistogram.
width ).arg( theHistogram.
height ) );
338 int myBinCount = theBinCount;
339 if ( myBinCount == 0 )
350 myBinCount = theHistogram.
width * theHistogram.
height;
351 if ( myBinCount > 1000 ) myBinCount = 1000;
359 if ( myBinCount > theHistogram.
maximum - theHistogram.
minimum + 1 )
360 myBinCount = int( ceil( theHistogram.
maximum - theHistogram.
minimum + 1 ) );
371 double theMinimum,
double theMaximum,
374 bool theIncludeOutOfRange )
376 QgsDebugMsg( QString(
"theBandNo = %1 theBinCount = %2 theMinimum = %3 theMaximum = %4 theSampleSize = %5" ).arg( theBandNo ).arg( theBinCount ).arg( theMinimum ).arg( theMaximum ).arg( theSampleSize ) );
382 initHistogram( myHistogram, theBandNo, theBinCount, theMinimum, theMaximum, theExtent, theSampleSize, theIncludeOutOfRange );
386 if ( histogram == myHistogram )
397 double theMinimum,
double theMaximum,
400 bool theIncludeOutOfRange )
402 QgsDebugMsg( QString(
"theBandNo = %1 theBinCount = %2 theMinimum = %3 theMaximum = %4 theSampleSize = %5" ).arg( theBandNo ).arg( theBinCount ).arg( theMinimum ).arg( theMaximum ).arg( theSampleSize ) );
405 initHistogram( myHistogram, theBandNo, theBinCount, theMinimum, theMaximum, theExtent, theSampleSize, theIncludeOutOfRange );
410 if ( histogram == myHistogram )
417 int myBinCount = myHistogram.
binCount;
418 int myWidth = myHistogram.
width;
419 int myHeight = myHistogram.
height;
425 if ( myXBlockSize == 0 )
429 if ( myYBlockSize == 0 )
434 int myNXBlocks = ( myWidth + myXBlockSize - 1 ) / myXBlockSize;
435 int myNYBlocks = ( myHeight + myYBlockSize - 1 ) / myYBlockSize;
437 double myXRes = myExtent.
width() / myWidth;
438 double myYRes = myExtent.
height() / myHeight;
440 double myMinimum = myHistogram.
minimum;
441 double myMaximum = myHistogram.
maximum;
445 double myerval = ( myMaximum - myMinimum ) / myHistogram.
binCount;
446 myMinimum -= 0.1 * myerval;
447 myMaximum += 0.1 * myerval;
449 QgsDebugMsg( QString(
"binCount = %1 myMinimum = %2 myMaximum = %3" ).arg( myHistogram.
binCount ).arg( myMinimum ).arg( myMaximum ) );
451 double myBinSize = ( myMaximum - myMinimum ) / myBinCount;
454 for (
int myYBlock = 0; myYBlock < myNYBlocks; myYBlock++ )
456 for (
int myXBlock = 0; myXBlock < myNXBlocks; myXBlock++ )
458 int myBlockWidth = qMin( myXBlockSize, myWidth - myXBlock * myXBlockSize );
459 int myBlockHeight = qMin( myYBlockSize, myHeight - myYBlock * myYBlockSize );
461 double xmin = myExtent.
xMinimum() + myXBlock * myXBlockSize * myXRes;
462 double xmax = xmin + myBlockWidth * myXRes;
463 double ymin = myExtent.
yMaximum() - myYBlock * myYBlockSize * myYRes;
464 double ymax = ymin - myBlockHeight * myYRes;
471 for (
qgssize i = 0; i < ((
qgssize ) myBlockHeight ) * myBlockWidth; i++ )
477 double myValue = blk->
value( i );
479 int myBinIndex = static_cast <
int>( qFloor(( myValue - myMinimum ) / myBinSize ) ) ;
481 if (( myBinIndex < 0 || myBinIndex > ( myBinCount - 1 ) ) && !theIncludeOutOfRange )
485 if ( myBinIndex < 0 ) myBinIndex = 0;
486 if ( myBinIndex > ( myBinCount - 1 ) ) myBinIndex = myBinCount - 1;
495 myHistogram.
valid =
true;
500 for (
int i = 0; i < qMin( myHistogram.
histogramVector.size(), 500 ); i++ )
502 hist += QString::number( myHistogram.
histogramVector.value( i ) ) +
" ";
504 QgsDebugMsg(
"Histogram (max first 500 bins): " + hist );
511 double theLowerCount,
double theUpperCount,
512 double &theLowerValue,
double &theUpperValue,
516 QgsDebugMsg( QString(
"theBandNo = %1 theLowerCount = %2 theUpperCount = %3 theSampleSize = %4" ).arg( theBandNo ).arg( theLowerCount ).arg( theUpperCount ).arg( theSampleSize ) );
528 theLowerValue = std::numeric_limits<double>::quiet_NaN();
529 theUpperValue = std::numeric_limits<double>::quiet_NaN();
533 int myMinCount = (
int ) qRound( theLowerCount * myHistogram.
nonNullCount );
534 int myMaxCount = ( int ) qRound( theUpperCount * myHistogram.
nonNullCount );
535 bool myLowerFound =
false;
536 QgsDebugMsg( QString(
"binCount = %1 minimum = %2 maximum = %3 myBinXStep = %4" ).arg( myHistogram.
binCount ).arg( myHistogram.
minimum ).arg( myHistogram.
maximum ).arg( myBinXStep ) );
537 QgsDebugMsg( QString(
"myMinCount = %1 myMaxCount = %2" ).arg( myMinCount ).arg( myMaxCount ) );
539 for (
int myBin = 0; myBin < myHistogram.
histogramVector.size(); myBin++ )
542 myCount += myBinValue;
543 if ( !myLowerFound && myCount > myMinCount )
545 theLowerValue = myHistogram.
minimum + myBin * myBinXStep;
547 QgsDebugMsg( QString(
"found lowerValue %1 at bin %2" ).arg( theLowerValue ).arg( myBin ) );
549 if ( myCount >= myMaxCount )
551 theUpperValue = myHistogram.
minimum + myBin * myBinXStep;
552 QgsDebugMsg( QString(
"found upperValue %1 at bin %2" ).arg( theUpperValue ).arg( myBin ) );
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 );
571 QStringList abilitiesList;
581 abilitiesList +=
tr(
"Identify" );
586 abilitiesList +=
tr(
"Create Datasources" );
591 abilitiesList +=
tr(
"Remove Datasources" );
596 abilitiesList +=
tr(
"Build Pyramids" );
599 QgsDebugMsg(
"Capability: " + abilitiesList.join(
", " ) );
601 return abilitiesList.join(
", " );
virtual ~QgsRasterInterface()
A rectangle specified with double values.
bool isEmpty() const
test if rectangle is empty.
double sum
The sum of all cells in the band.
void initHistogram(QgsRasterHistogram &theHistogram, int theBandNo, int theBinCount=0, double theMinimum=std::numeric_limits< double >::quiet_NaN(), double theMaximum=std::numeric_limits< double >::quiet_NaN(), const QgsRectangle &theExtent=QgsRectangle(), int theSampleSize=0, bool theIncludeOutOfRange=false)
Fill in histogram defaults if not specified.
int bandNumber
The gdal band number (starts at 1)
int width
Number of columns used to calc histogram.
int height
Number of rows used to calc statistics.
double yMaximum() const
Get the y maximum value (top side of rectangle)
int bandNumber
The gdal band number (starts at 1)
virtual int xBlockSize() const
Get block size.
double minimum
The minimum histogram value.
virtual int yBlockSize() const
double maximumValue
The maximum cell value in the raster band.
QString capabilitiesString() const
Returns the above in friendly format.
int height
Number of rows used to calc histogram.
bool isNoData(int row, int column)
Check if value at position is no data.
virtual int ySize() const
void initStatistics(QgsRasterBandStats &theStatistics, int theBandNo, int theStats=QgsRasterBandStats::All, const QgsRectangle &theExtent=QgsRectangle(), int theBinCount=0)
Fill in statistics defaults if not specified.
bool contains(const QgsRasterBandStats &s) const
QgsRectangle extent
Extent used to calc statistics.
virtual QgsRasterBandStats bandStatistics(int theBandNo, int theStats=QgsRasterBandStats::All, const QgsRectangle &theExtent=QgsRectangle(), int theSampleSize=0)
Get band statistics.
double stdDev
The standard deviation of the cell values.
The RasterBandStats struct is a container for statistics about a single raster band.
double mean
The mean cell value for the band.
virtual bool hasHistogram(int theBandNo, int theBinCount, double theMinimum=std::numeric_limits< double >::quiet_NaN(), double theMaximum=std::numeric_limits< double >::quiet_NaN(), const QgsRectangle &theExtent=QgsRectangle(), int theSampleSize=0, bool theIncludeOutOfRange=false)
Returns true if histogram is available (cached, already calculated), the parameters are the same as i...
double value(int row, int column) const
Read a single value if type of block is numeric.
QList< QgsRasterHistogram > mHistograms
List of cached histograms, all bands mixed.
qgssize elementCount
The number of not no data cells in the band.
int statsGathered
Collected statistics.
Base class for processing filters like renderers, reprojector, resampler etc.
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...
virtual QgsRasterHistogram histogram(int theBandNo, int theBinCount=0, double theMinimum=std::numeric_limits< double >::quiet_NaN(), double theMaximum=std::numeric_limits< double >::quiet_NaN(), const QgsRectangle &theExtent=QgsRectangle(), int theSampleSize=0, bool theIncludeOutOfRange=false)
Get histogram.
virtual int capabilities() const
Returns a bitmask containing the supported capabilities.
double maximum
The maximum histogram value.
QgsRasterInterface(QgsRasterInterface *input=0)
virtual QgsRectangle extent()
Get the extent of the interface.
virtual bool hasStatistics(int theBandNo, int theStats=QgsRasterBandStats::All, const QgsRectangle &theExtent=QgsRectangle(), int theSampleSize=0)
Returns true if histogram is available (cached, already calculated).
virtual int xSize() const
Get raster size.
QgsRectangle intersect(const QgsRectangle *rect) const
return the intersection with the given rectangle
virtual QGis::DataType srcDataType(int bandNo) const
Returns source data type for the band specified by number, source data type may be shorter than dataT...
double range
The range is the distance between min & max.
virtual QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height)=0
Read block of data using given extent and size.
double minimumValue
The minimum cell value in the raster band.
The QgsRasterHistogram is a container for histogram of a single raster band.
int binCount
Number of bins (intervals,buckets) in histogram.
QList< QgsRasterBandStats > mStatistics
List of cached statistics, all bands mixed.
HistogramVector histogramVector
Store the histogram for a given layer.
bool valid
Histogram is valid.
double width() const
Width of the rectangle.
virtual void cumulativeCut(int theBandNo, double theLowerCount, double theUpperCount, double &theLowerValue, double &theUpperValue, const QgsRectangle &theExtent=QgsRectangle(), int theSampleSize=0)
Find values for cumulative pixel count cut.
QgsRasterInterface * mInput
QgsRectangle extent
Extent used to calc histogram.
double xMinimum() const
Get the x minimum value (left side of rectangle)
bool includeOutOfRange
Whether histogram includes out of range values (in first and last bin)
double height() const
Height of the rectangle.
double sumOfSquares
The sum of the squares.
int width
Number of columns used to calc statistics.
int nonNullCount
The number of non NULL cells used to calculate histogram.