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;
361 double theMinimum,
double theMaximum,
364 bool theIncludeOutOfRange )
366 QgsDebugMsg( QString(
"theBandNo = %1 theBinCount = %2 theMinimum = %3 theMaximum = %4 theSampleSize = %5" ).arg( theBandNo ).arg( theBinCount ).arg( theMinimum ).arg( theMaximum ).arg( theSampleSize ) );
372 initHistogram( myHistogram, theBandNo, theBinCount, theMinimum, theMaximum, theExtent, theSampleSize, theIncludeOutOfRange );
376 if ( histogram == myHistogram )
387 double theMinimum,
double theMaximum,
390 bool theIncludeOutOfRange )
392 QgsDebugMsg( QString(
"theBandNo = %1 theBinCount = %2 theMinimum = %3 theMaximum = %4 theSampleSize = %5" ).arg( theBandNo ).arg( theBinCount ).arg( theMinimum ).arg( theMaximum ).arg( theSampleSize ) );
395 initHistogram( myHistogram, theBandNo, theBinCount, theMinimum, theMaximum, theExtent, theSampleSize, theIncludeOutOfRange );
400 if ( histogram == myHistogram )
407 int myBinCount = myHistogram.
binCount;
408 int myWidth = myHistogram.
width;
409 int myHeight = myHistogram.
height;
415 if ( myXBlockSize == 0 )
419 if ( myYBlockSize == 0 )
424 int myNXBlocks = ( myWidth + myXBlockSize - 1 ) / myXBlockSize;
425 int myNYBlocks = ( myHeight + myYBlockSize - 1 ) / myYBlockSize;
427 double myXRes = myExtent.
width() / myWidth;
428 double myYRes = myExtent.
height() / myHeight;
430 double myMinimum = myHistogram.
minimum;
431 double myMaximum = myHistogram.
maximum;
435 double myerval = ( myMaximum - myMinimum ) / myHistogram.
binCount;
436 myMinimum -= 0.1 * myerval;
437 myMaximum += 0.1 * myerval;
439 QgsDebugMsg( QString(
"binCount = %1 myMinimum = %2 myMaximum = %3" ).arg( myHistogram.
binCount ).arg( myMinimum ).arg( myMaximum ) );
441 double myBinSize = ( myMaximum - myMinimum ) / myBinCount;
444 for (
int myYBlock = 0; myYBlock < myNYBlocks; myYBlock++ )
446 for (
int myXBlock = 0; myXBlock < myNXBlocks; myXBlock++ )
448 int myBlockWidth = qMin( myXBlockSize, myWidth - myXBlock * myXBlockSize );
449 int myBlockHeight = qMin( myYBlockSize, myHeight - myYBlock * myYBlockSize );
451 double xmin = myExtent.
xMinimum() + myXBlock * myXBlockSize * myXRes;
452 double xmax = xmin + myBlockWidth * myXRes;
453 double ymin = myExtent.
yMaximum() - myYBlock * myYBlockSize * myYRes;
454 double ymax = ymin - myBlockHeight * myYRes;
461 for (
qgssize i = 0; i < ((
qgssize ) myBlockHeight ) * myBlockWidth; i++ )
467 double myValue = blk->
value( i );
469 int myBinIndex = static_cast <
int>( qFloor(( myValue - myMinimum ) / myBinSize ) ) ;
471 if (( myBinIndex < 0 || myBinIndex > ( myBinCount - 1 ) ) && !theIncludeOutOfRange )
475 if ( myBinIndex < 0 ) myBinIndex = 0;
476 if ( myBinIndex > ( myBinCount - 1 ) ) myBinIndex = myBinCount - 1;
485 myHistogram.
valid =
true;
490 for (
int i = 0; i < qMin( myHistogram.
histogramVector.size(), 500 ); i++ )
492 hist += QString::number( myHistogram.
histogramVector.value( i ) ) +
" ";
494 QgsDebugMsg(
"Histogram (max first 500 bins): " + hist );
501 double theLowerCount,
double theUpperCount,
502 double &theLowerValue,
double &theUpperValue,
506 QgsDebugMsg( QString(
"theBandNo = %1 theLowerCount = %2 theUpperCount = %3 theSampleSize = %4" ).arg( theBandNo ).arg( theLowerCount ).arg( theUpperCount ).arg( theSampleSize ) );
508 QgsRasterHistogram myHistogram =
histogram( theBandNo, 0, std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), theExtent, theSampleSize );
511 theLowerValue = std::numeric_limits<double>::quiet_NaN();
512 theUpperValue = std::numeric_limits<double>::quiet_NaN();
516 int myMinCount = (
int ) qRound( theLowerCount * myHistogram.
nonNullCount );
517 int myMaxCount = ( int ) qRound( theUpperCount * myHistogram.
nonNullCount );
518 bool myLowerFound =
false;
519 QgsDebugMsg( QString(
"binCount = %1 minimum = %2 maximum = %3 myBinXStep = %4" ).arg( myHistogram.
binCount ).arg( myHistogram.
minimum ).arg( myHistogram.
maximum ).arg( myBinXStep ) );
520 QgsDebugMsg( QString(
"myMinCount = %1 myMaxCount = %2" ).arg( myMinCount ).arg( myMaxCount ) );
522 for (
int myBin = 0; myBin < myHistogram.
histogramVector.size(); myBin++ )
525 myCount += myBinValue;
526 if ( !myLowerFound && myCount > myMinCount )
528 theLowerValue = myHistogram.
minimum + myBin * myBinXStep;
531 if ( myCount >= myMaxCount )
533 theUpperValue = myHistogram.
minimum + myBin * myBinXStep;
541 QStringList abilitiesList;
551 abilitiesList +=
tr(
"Identify" );
556 abilitiesList +=
tr(
"Create Datasources" );
561 abilitiesList +=
tr(
"Remove Datasources" );
566 abilitiesList +=
tr(
"Build Pyramids" );
569 QgsDebugMsg(
"Capability: " + abilitiesList.join(
", " ) );
571 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.