Skip to content

Commit

Permalink
Raster statistics and histogram moved from QgsRasterDataProvider to Q…
Browse files Browse the repository at this point in the history
…gsRasterInterface
  • Loading branch information
blazek committed Dec 17, 2012
1 parent b662077 commit 76115ec
Show file tree
Hide file tree
Showing 9 changed files with 865 additions and 796 deletions.
106 changes: 3 additions & 103 deletions python/core/raster/qgsrasterdataprovider.sip
Expand Up @@ -15,25 +15,6 @@ class QgsRasterDataProvider : QgsDataProvider, QgsRasterInterface

public:

//! If you add to this, please also add to capabilitiesString()
enum Capability
{
NoCapabilities,
Identify,
ExactMinimumMaximum,
ExactResolution,
EstimatedMinimumMaximum,
BuildPyramids,
Histogram,
Size,
Create,
Remove,
IdentifyValue,
IdentifyText,
IdentifyHtml,
IdentifyFeature
};

// This is modified copy of GDALColorInterp
enum ColorInterpretation
{
Expand Down Expand Up @@ -104,23 +85,12 @@ class QgsRasterDataProvider : QgsDataProvider, QgsRasterInterface
*/
virtual QImage* draw( const QgsRectangle & viewExtent, int pixelWidth, int pixelHeight ) = 0;

/** Returns a bitmask containing the supported capabilities
Note, some capabilities may change depending on whether
a spatial filter is active on this provider, so it may
be prudent to check this value per intended operation.
*/
virtual int capabilities() const;

/**
* Returns the above in friendly format.
*/
QString capabilitiesString() const;


// TODO: Get the supported formats by this provider

// TODO: Get the file masks supported by this provider, suitable for feeding into the file open dialog box

virtual QgsRectangle extent() = 0;

/** Returns data type for the band specified by number */
virtual QGis::DataType dataType( int bandNo ) const = 0;

Expand Down Expand Up @@ -162,7 +132,7 @@ class QgsRasterDataProvider : QgsDataProvider, QgsRasterInterface
virtual QgsRasterBlock *block( int bandNo, const QgsRectangle &extent, int width, int height ) / Factory /;

/* Read a value from a data block at a given index. */
virtual double readValue( void *data, int type, int index );
//virtual double readValue( void *data, int type, int index );

/* Return true if source band has no data value */
virtual bool srcHasNoDataValue( int bandNo ) const;
Expand Down Expand Up @@ -198,50 +168,6 @@ class QgsRasterDataProvider : QgsDataProvider, QgsRasterInterface
/** \brief Returns the sublayers of this layer - Useful for providers that manage their own layers, such as WMS */
virtual QStringList subLayers() const;

/** \brief Get histogram. Histograms are cached in providers.
* @param theBandNo The band (number).
* @param theBinCount Number of bins (intervals,buckets). If 0, the number of bins is decided automaticaly according to data type, raster size etc.
* @param theMinimum Minimum value, if NaN, raster minimum value will be used.
* @param theMaximum Maximum value, if NaN, raster minimum value will be used.
* @param theExtent Extent used to calc histogram, if empty, whole raster extent is used.
* @param theSampleSize Approximate number of cells in sample. If 0, all cells (whole raster will be used). If raster does not have exact size (WCS without exact size for example), provider decides size of sample.
* @param theIncludeOutOfRange include out of range values
* @return Vector of non NULL cell counts for each bin.
*/
virtual QgsRasterHistogram histogram( int theBandNo,
int theBinCount,
double theMinimum,
double theMaximum,
const QgsRectangle & theExtent = QgsRectangle(),
int theSampleSize = 0,
bool theIncludeOutOfRange = false );

/** \brief Returns true if histogram is available (cached, already calculated), the parameters are the same as in histogram() */
virtual bool hasHistogram( int theBandNo,
int theBinCount,
double theMinimum,
double theMaximum,
const QgsRectangle & theExtent = QgsRectangle(),
int theSampleSize = 0,
bool theIncludeOutOfRange = false );

/** \brief Find values for cumulative pixel count cut.
* @param theBandNo The band (number).
* @param theLowerCount The lower count as fraction of 1, e.g. 0.02 = 2%
* @param theUpperCount The upper count as fraction of 1, e.g. 0.98 = 98%
* @param theLowerValue Location into which the lower value will be set.
* @param theUpperValue Location into which the upper value will be set.
* @param theExtent Extent used to calc histogram, if empty, whole raster extent is used.
* @param theSampleSize Approximate number of cells in sample. If 0, all cells (whole raster will be used). If raster does not have exact size (WCS without exact size for example), provider decides size of sample.
*/
virtual void cumulativeCut( int theBandNo,
double theLowerCount,
double theUpperCount,
double &theLowerValue,
double &theUpperValue,
const QgsRectangle & theExtent = QgsRectangle(),
int theSampleSize = 0 );

/** \brief Create pyramid overviews */
virtual QString buildPyramids( const QList<QgsRasterPyramid> & thePyramidList,
const QString & theResamplingMethod = "NEAREST",
Expand All @@ -259,32 +185,6 @@ class QgsRasterDataProvider : QgsDataProvider, QgsRasterInterface
/** \brief Returns true if raster has at least one populated histogram. */
bool hasPyramids();

/** If the provider supports it, return band stats for the
given band. Default behaviour is to blockwise read the data
and generate the stats unless the provider overloads this function. */
//virtual QgsRasterBandStats bandStatistics( int theBandNo );

/** \brief Get band statistics.
* @param theBandNo The band (number).
* @param theStats Requested statistics
* @param theExtent Extent used to calc histogram, if empty, whole raster extent is used.
* @param theSampleSize Approximate number of cells in sample. If 0, all cells (whole raster will be used). If raster does not have exact size (WCS without exact size for example), provider decides size of sample.
* @return Band statistics.
*/
virtual QgsRasterBandStats bandStatistics( int theBandNo,
int theStats = QgsRasterBandStats::All,
const QgsRectangle & theExtent = QgsRectangle(),
int theSampleSize = 0 );

/** \brief Returns true if histogram is available (cached, already calculated), the parameters are the same as in histogram() */
virtual bool hasStatistics( int theBandNo,
int theStats = QgsRasterBandStats::All,
const QgsRectangle & theExtent = QgsRectangle(),
int theSampleSize = 0 );

/** \brief helper function to create zero padded band names */
QString generateBandName( int theBandNumber ) const;

/**
* Get metadata in a format suitable for feeding directly
* into a subset of the GUI raster properties "Metadata" tab.
Expand Down
102 changes: 102 additions & 0 deletions python/core/raster/qgsrasterinterface.sip
Expand Up @@ -41,6 +41,23 @@ class QgsRasterInterface
%End

public:
enum Capability
{
NoCapabilities,
Identify,
ExactMinimumMaximum,
ExactResolution,
EstimatedMinimumMaximum,
BuildPyramids,
Histogram,
Size,
Create,
Remove,
IdentifyValue,
IdentifyText,
IdentifyHtml,
IdentifyFeature
};

QgsRasterInterface( QgsRasterInterface * input = 0 );

Expand All @@ -49,6 +66,14 @@ class QgsRasterInterface
/** Clone itself, create deep copy */
virtual QgsRasterInterface *clone() const = 0 /Factory/;

/** Returns a bitmask containing the supported capabilities */
virtual int capabilities() const;

/**
* Returns the above in friendly format.
*/
QString capabilitiesString() const;

/** Returns data type for the band specified by number */
virtual QGis::DataType dataType( int bandNo ) const = 0;

Expand All @@ -57,6 +82,14 @@ class QgsRasterInterface
/** Get number of bands */
virtual int bandCount() const = 0;

/** Get block size */
virtual int xBlockSize() const;
virtual int yBlockSize() const;

/** Get raster size */
virtual int xSize() const;
virtual int ySize() const;

/** Return no data value for specific band. Each band/provider must have
* no data value, if there is no one set in original data, provider decides one
* possibly using wider data type.
Expand Down Expand Up @@ -90,5 +123,74 @@ class QgsRasterInterface
* resampling etc.
*/
virtual QgsRasterInterface *srcInput();

/** \brief Get band statistics.
* @param theBandNo The band (number).
* @param theStats Requested statistics
* @param theExtent Extent used to calc statistics, if empty, whole raster extent is used.
* @param theSampleSize Approximate number of cells in sample. If 0, all cells (whole raster will be used). If raster does not have exact size (WCS without exact size for example), provider decides size of sample.
* @return Band statistics.
*/
virtual QgsRasterBandStats bandStatistics( int theBandNo,
int theStats = QgsRasterBandStats::All,
const QgsRectangle & theExtent = QgsRectangle(),
int theSampleSize = 0 );

/** \brief Returns true if histogram is available (cached, already calculated). * The parameters are the same as in bandStatistics()
* @return true if statistics are available (ready to use)
*/
virtual bool hasStatistics( int theBandNo,
int theStats = QgsRasterBandStats::All,
const QgsRectangle & theExtent = QgsRectangle(),
int theSampleSize = 0 );


/** \brief Get histogram. Histograms are cached in providers.
* @param theBandNo The band (number).
* @param theBinCount Number of bins (intervals,buckets). If 0, the number of bins is decided automaticaly according to data type, raster size etc.
* @param theMinimum Minimum value, if NaN, raster minimum value will be used.
* @param theMaximum Maximum value, if NaN, raster minimum value will be used.
* @param theExtent Extent used to calc histogram, if empty, whole raster extent is used.
* @param theSampleSize Approximate number of cells in sample. If 0, all cells (whole raster will be used). If raster does not have exact size (WCS without exact size for example), provider decides size of sample.
* @param theIncludeOutOfRange include out of range values
* @return Vector of non NULL cell counts for each bin.
* @note theBinCount, theMinimun and theMaximum not optional in python bindings
*/
virtual QgsRasterHistogram histogram( int theBandNo,
int theBinCount,
double theMinimum,
double theMaximum,
const QgsRectangle & theExtent,
int theSampleSize,
bool theIncludeOutOfRange );

/** \brief Returns true if histogram is available (cached, already calculated), the parameters are the same as in histogram()
* @note theBinCount, theMinimun and theMaximum not optional in python bindings
*/
virtual bool hasHistogram( int theBandNo,
int theBinCount,
double theMinimum,
double theMaximum,
const QgsRectangle & theExtent,
int theSampleSize,
bool theIncludeOutOfRange );

/** \brief Find values for cumulative pixel count cut.
* @param theBandNo The band (number).
* @param theLowerCount The lower count as fraction of 1, e.g. 0.02 = 2%
* @param theUpperCount The upper count as fraction of 1, e.g. 0.98 = 98%
* @param theLowerValue Location into which the lower value will be set.
* @param theUpperValue Location into which the upper value will be set.
* @param theExtent Extent used to calc histogram, if empty, whole raster extent is used.
* @param theSampleSize Approximate number of cells in sample. If 0, all cells (whole raster will be used). If raster does not have exact size (WCS without exact size for example), provider decides size of sample.
*/
virtual void cumulativeCut( int theBandNo,
double theLowerCount,
double theUpperCount,
double &theLowerValue,
double &theUpperValue,
const QgsRectangle & theExtent = QgsRectangle(),
int theSampleSize = 0 );

};

4 changes: 2 additions & 2 deletions src/core/raster/qgsrasterchecker.cpp
Expand Up @@ -159,8 +159,8 @@ bool QgsRasterChecker::runTest( QString theVerifiedKey, QString theVerifiedUri,
for ( int col = 0; col < width; col ++ )
{
bool cellOk = true;
double verifiedVal = verifiedProvider->readValue( verifiedData, verifiedProvider->dataType( band ), row * width + col );
double expectedVal = expectedProvider->readValue( expectedData, expectedProvider->dataType( band ), row * width + col );
double verifiedVal = QgsRasterBlock::readValue( verifiedData, verifiedProvider->dataType( band ), row * width + col );
double expectedVal = QgsRasterBlock::readValue( expectedData, expectedProvider->dataType( band ), row * width + col );

QString valStr;
if ( compare( verifiedVal, expectedVal, 0 ) )
Expand Down

0 comments on commit 76115ec

Please sign in to comment.