Skip to content

Commit

Permalink
Better constructor, dox
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 8, 2018
1 parent e97f0d3 commit 6bf7c0e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
23 changes: 20 additions & 3 deletions python/analysis/auto_generated/vector/qgszonalstatistics.sip.in
Expand Up @@ -15,7 +15,7 @@
class QgsZonalStatistics
{
%Docstring
A class that calculates raster statistics (count, sum, mean) for a polygon or multipolygon layer and appends the results as attributes*
A class that calculates raster statistics (count, sum, mean) for a polygon or multipolygon layer and appends the results as attributes.
%End

%TypeHeaderCode
Expand Down Expand Up @@ -69,6 +69,24 @@ The raster layer must exist for the lifetime of the zonal statistics calculation
%Docstring
Constructor for QgsZonalStatistics, using a QgsRasterInterface.

The ``polygonLayer`` gives the vector layer containing the (multi)polygon features corresponding to the
different zones. This layer will be modified, adding extra attributes for each of the zonal statistics
calculated.

Pixel values for each zone are taken from the raster ``rasterInterface``. The constructor must also
be given various properties relating to the input raster, such as the raster CRS (``rasterCrs``),
and the size (X and Y) in map units for each raster pixel. The source raster band is specified
via ``rasterBand``, where a value of 1 corresponds to the first band.

If the CRS of the ``polygonLayer`` and ``rasterCrs`` differ, the calculation will automatically
reproject the zones to ensure valid results are calculated.

The ``attributePrefix`` argument specifies an optional prefix to use when creating the
new fields for each calculated statistic.

Finally, the calculated statistics can be set via the ``stats`` argument. A new field will be
added to ``polygonLayer`` for each statistic calculated.

.. warning::

The raster interface must exist for the lifetime of the zonal statistics calculation. For thread
Expand All @@ -77,12 +95,11 @@ Constructor for QgsZonalStatistics, using a QgsRasterInterface.
.. versionadded:: 3.2
%End


int calculateStatistics( QgsFeedback *feedback );
%Docstring
Starts the calculation

:return: 0 in case of success*
:return: 0 in case of success
%End

public:
Expand Down
12 changes: 2 additions & 10 deletions src/analysis/vector/qgszonalstatistics.cpp
Expand Up @@ -46,21 +46,13 @@ QgsZonalStatistics::QgsZonalStatistics( QgsVectorLayer *polygonLayer, QgsRasterI
const QgsCoordinateReferenceSystem &rasterCrs, double rasterUnitsPerPixelX, double rasterUnitsPerPixelY, const QString &attributePrefix, int rasterBand, QgsZonalStatistics::Statistics stats )
: mRasterInterface( rasterInterface )
, mRasterCrs( rasterCrs )
, mCellSizeX( rasterUnitsPerPixelX )
, mCellSizeY( rasterUnitsPerPixelY )
, mCellSizeX( std::fabs( rasterUnitsPerPixelX ) )
, mCellSizeY( std::fabs( rasterUnitsPerPixelY ) )
, mRasterBand( rasterBand )
, mPolygonLayer( polygonLayer )
, mAttributePrefix( attributePrefix )
, mStatistics( stats )
{
if ( mCellSizeX < 0 )
{
mCellSizeX = -mCellSizeX;
}
if ( mCellSizeY < 0 )
{
mCellSizeY = -mCellSizeY;
}
}

int QgsZonalStatistics::calculateStatistics( QgsFeedback *feedback )
Expand Down
25 changes: 22 additions & 3 deletions src/analysis/vector/qgszonalstatistics.h
Expand Up @@ -38,7 +38,8 @@ class QgsField;

/**
* \ingroup analysis
* A class that calculates raster statistics (count, sum, mean) for a polygon or multipolygon layer and appends the results as attributes*/
* A class that calculates raster statistics (count, sum, mean) for a polygon or multipolygon layer and appends the results as attributes.
*/
class ANALYSIS_EXPORT QgsZonalStatistics
{
public:
Expand Down Expand Up @@ -79,6 +80,24 @@ class ANALYSIS_EXPORT QgsZonalStatistics
/**
* Constructor for QgsZonalStatistics, using a QgsRasterInterface.
*
* The \a polygonLayer gives the vector layer containing the (multi)polygon features corresponding to the
* different zones. This layer will be modified, adding extra attributes for each of the zonal statistics
* calculated.
*
* Pixel values for each zone are taken from the raster \a rasterInterface. The constructor must also
* be given various properties relating to the input raster, such as the raster CRS (\a rasterCrs),
* and the size (X and Y) in map units for each raster pixel. The source raster band is specified
* via \a rasterBand, where a value of 1 corresponds to the first band.
*
* If the CRS of the \a polygonLayer and \a rasterCrs differ, the calculation will automatically
* reproject the zones to ensure valid results are calculated.
*
* The \a attributePrefix argument specifies an optional prefix to use when creating the
* new fields for each calculated statistic.
*
* Finally, the calculated statistics can be set via the \a stats argument. A new field will be
* added to \a polygonLayer for each statistic calculated.
*
* \warning The raster interface must exist for the lifetime of the zonal statistics calculation. For thread
* safe use, always use a cloned raster interface.
*
Expand All @@ -93,10 +112,10 @@ class ANALYSIS_EXPORT QgsZonalStatistics
int rasterBand = 1,
QgsZonalStatistics::Statistics stats = QgsZonalStatistics::Statistics( QgsZonalStatistics::Count | QgsZonalStatistics::Sum | QgsZonalStatistics::Mean ) );


/**
* Starts the calculation
\returns 0 in case of success*/
* \returns 0 in case of success
*/
int calculateStatistics( QgsFeedback *feedback );

private:
Expand Down

0 comments on commit 6bf7c0e

Please sign in to comment.