35                         rasterLayer ? rasterLayer->dataProvider() : nullptr,
 
   37                         rasterLayer ? rasterLayer->rasterUnitsPerPixelX() : 0,
 
   38                         rasterLayer ? rasterLayer->rasterUnitsPerPixelY() : 0,
 
   46                                         const QgsCoordinateReferenceSystem &rasterCrs, 
double rasterUnitsPerPixelX, 
double rasterUnitsPerPixelY, 
const QString &attributePrefix, 
int rasterBand, QgsZonalStatistics::Statistics stats )
 
   47   : mRasterInterface( rasterInterface )
 
   48   , mRasterCrs( rasterCrs )
 
   49   , mCellSizeX( std::fabs( rasterUnitsPerPixelX ) )
 
   50   , mCellSizeY( std::fabs( rasterUnitsPerPixelY ) )
 
   51   , mRasterBand( rasterBand )
 
   52   , mPolygonLayer( polygonLayer )
 
   53   , mAttributePrefix( attributePrefix )
 
   54   , mStatistics( stats )
 
   60   if ( !mRasterInterface )
 
   65   if ( mRasterInterface->
bandCount() < mRasterBand )
 
   76   if ( !vectorProvider )
 
   81   QMap<QgsZonalStatistics::Statistic, int> statFieldIndexes;
 
   84   int oldFieldCount = vectorProvider->
fields().
count();
 
   85   QList<QgsField> newFieldList;
 
  102     if ( mStatistics & stat )
 
  105       QgsField field( fieldName, QVariant::Double, QStringLiteral( 
"double precision" ) );
 
  106       newFieldList.push_back( 
field );
 
  107       statFieldIndexes.insert( stat, oldFieldCount + newFieldList.count() - 1 );
 
  122   int featureCounter = 0;
 
  135       feedback->
setProgress( 100.0 * 
static_cast< double >( featureCounter ) / featureCount );
 
  140     QMap<QgsZonalStatistics::Statistic, QVariant> results = 
calculateStatistics( mRasterInterface, featureGeometry, mCellSizeX, mCellSizeY, mRasterBand, mStatistics );
 
  142     if ( results.empty() )
 
  146     for ( 
const auto &result : results.toStdMap() )
 
  148       changeAttributeMap.insert( statFieldIndexes.value( result.first ), result.second );
 
  151     changeMap.insert( feature.
id(), changeAttributeMap );
 
  168 QString QgsZonalStatistics::getUniqueFieldName( 
const QString &fieldName, 
const QList<QgsField> &newFields )
 
  172   if ( !dp->
storageType().contains( QLatin1String( 
"ESRI Shapefile" ) ) )
 
  178   allFields.append( newFields );
 
  179   QString 
shortName = fieldName.mid( 0, 10 );
 
  197   shortName = QStringLiteral( 
"%1_%2" ).arg( fieldName.mid( 0, 8 ) ).arg( n );
 
  209           shortName = QStringLiteral( 
"%1_%2" ).arg( fieldName.mid( 0, 8 ) ).arg( n );
 
  213           shortName = QStringLiteral( 
"%1_%2" ).arg( fieldName.mid( 0, 7 ) ).arg( n );
 
  227       return QObject::tr( 
"Count" );
 
  229       return QObject::tr( 
"Sum" );
 
  231       return QObject::tr( 
"Mean" );
 
  233       return QObject::tr( 
"Median" );
 
  235       return QObject::tr( 
"St dev" );
 
  237       return QObject::tr( 
"Minimum" );
 
  239       return QObject::tr( 
"Maximum" );
 
  241       return QObject::tr( 
"Range" );
 
  243       return QObject::tr( 
"Minority" );
 
  245       return QObject::tr( 
"Majority" );
 
  247       return QObject::tr( 
"Variety" );
 
  249       return QObject::tr( 
"Variance" );
 
  261       return QStringLiteral( 
"count" );
 
  263       return QStringLiteral( 
"sum" );
 
  265       return QStringLiteral( 
"mean" );
 
  267       return QStringLiteral( 
"median" );
 
  269       return QStringLiteral( 
"stdev" );
 
  271       return QStringLiteral( 
"min" );
 
  273       return QStringLiteral( 
"max" );
 
  275       return QStringLiteral( 
"range" );
 
  277       return QStringLiteral( 
"minority" );
 
  279       return QStringLiteral( 
"majority" );
 
  281       return QStringLiteral( 
"variety" );
 
  283       return QStringLiteral( 
"variance" );
 
  291 QMap<int, QVariant> QgsZonalStatistics::calculateStatisticsInt( 
QgsRasterInterface *rasterInterface, 
const QgsGeometry &geometry, 
double cellSizeX, 
double cellSizeY, 
int rasterBand, QgsZonalStatistics::Statistics statistics )
 
  294   QMap<int, QVariant> pyResult;
 
  295   for ( 
auto it = result.constBegin(); it != result.constEnd(); ++it )
 
  297     pyResult.insert( it.key(), it.value() );
 
  305   QMap<QgsZonalStatistics::Statistic, QVariant> results;
 
  323   FeatureStats featureStats( statsStoreValues, statsStoreValueCount );
 
  325   int nCellsXProvider = rasterInterface->
xSize();
 
  326   int nCellsYProvider = rasterInterface->
ySize();
 
  328   int nCellsX, nCellsY;
 
  330   QgsRasterAnalysisUtils::cellInfoForBBox( rasterBBox, featureRect, cellSizeX, cellSizeY, nCellsX, nCellsY, nCellsXProvider, nCellsYProvider, rasterBlockExtent );
 
  332   featureStats.reset();
 
  333   QgsRasterAnalysisUtils::statisticsFromMiddlePointTest( rasterInterface, rasterBand, geometry, nCellsX, nCellsY, cellSizeX, cellSizeY, rasterBlockExtent, [ &featureStats ]( 
double value ) { featureStats.addValue( value ); } );
 
  335   if ( featureStats.count <= 1 )
 
  338     featureStats.reset();
 
  339     QgsRasterAnalysisUtils::statisticsFromPreciseIntersection( rasterInterface, rasterBand, geometry, nCellsX, nCellsY, cellSizeX, cellSizeY, rasterBlockExtent, [ &featureStats ]( 
double value, 
double weight ) { featureStats.addValue( value, weight ); } );
 
  348   if ( featureStats.count > 0 )
 
  350     double mean = featureStats.sum / featureStats.count;
 
  355       std::sort( featureStats.values.begin(), featureStats.values.end() );
 
  356       int size = featureStats.values.count();
 
  357       bool even = ( size % 2 ) < 1;
 
  361         medianValue = ( featureStats.values.at( size / 2 - 1 ) + featureStats.values.at( size / 2 ) ) / 2;
 
  365         medianValue = featureStats.values.at( ( size + 1 ) / 2 - 1 );
 
  371       double sumSquared = 0;
 
  372       for ( 
int i = 0; i < featureStats.values.count(); ++i )
 
  374         double diff = featureStats.values.at( i ) - mean;
 
  375         sumSquared += diff * diff;
 
  377       double variance = sumSquared / featureStats.values.count();
 
  380         double stdev = std::pow( variance, 0.5 );
 
  394       QList<int> vals = featureStats.valueCount.values();
 
  395       std::sort( vals.begin(), vals.end() );
 
  398         double minorityKey = featureStats.valueCount.key( vals.first() );
 
  403         double majKey = featureStats.valueCount.key( vals.last() );
 
This class represents a coordinate reference system (CRS).
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature's geometries.
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Base class for feedback objects to be used for cancellation of something running in a worker thread.
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Encapsulate a field in an attribute table or data source.
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
int count() const
Returns number of items.
A geometry is the spatial representation of a feature.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Base class for processing filters like renderers, reprojector, resampler etc.
virtual int xSize() const
Gets raster size.
virtual int bandCount() const =0
Gets number of bands.
virtual int ySize() const
virtual QgsRectangle extent() const
Gets the extent of the interface.
Represents a raster layer.
A rectangle specified with double values.
bool isEmpty() const
Returns true if the rectangle is empty.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
This is the base class for vector data providers.
long long featureCount() const override=0
Number of features in the layer.
virtual QString storageType() const
Returns the permanent storage type for this layer as a friendly name.
virtual bool changeAttributeValues(const QgsChangedAttributesMap &attr_map)
Changes attribute values of existing features.
QgsFields fields() const override=0
Returns the fields associated with this data provider.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override=0
Query the provider for features specified in request.
virtual bool addAttributes(const QList< QgsField > &attributes)
Adds new attributes to the provider.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
void updateFields()
Will regenerate the fields property of this layer by obtaining all fields from the dataProvider,...
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
A class that calculates raster statistics (count, sum, mean) for a polygon or multipolygon layer and ...
static QString shortName(QgsZonalStatistics::Statistic statistic)
Returns a short, friendly display name for a statistic, suitable for use in a field name.
Statistic
Enumeration of flags that specify statistics to be calculated.
@ Minority
Minority of pixel values.
@ Variety
Variety (count of distinct) pixel values.
@ Variance
Variance of pixel values.
@ Mean
Mean of pixel values.
@ Majority
Majority of pixel values.
@ Range
Range of pixel values (max - min)
@ Min
Min of pixel values.
@ Sum
Sum of pixel values.
@ StDev
Standard deviation of pixel values.
@ Max
Max of pixel values.
@ Median
Median of pixel values.
Result
Error codes for calculation.
@ RasterInvalid
Raster layer is invalid.
@ RasterBandInvalid
The raster band does not exist on the raster layer.
@ LayerTypeWrong
Layer is not a polygon layer.
@ Canceled
Algorithm was canceled.
@ LayerInvalid
Layer is invalid.
QgsZonalStatistics::Result calculateStatistics(QgsFeedback *feedback)
Runs the calculation.
static QString displayName(QgsZonalStatistics::Statistic statistic)
Returns the friendly display name for a statistic.
QgsZonalStatistics(QgsVectorLayer *polygonLayer, QgsRasterLayer *rasterLayer, const QString &attributePrefix=QString(), int rasterBand=1, QgsZonalStatistics::Statistics stats=QgsZonalStatistics::Statistics(QgsZonalStatistics::Count|QgsZonalStatistics::Sum|QgsZonalStatistics::Mean))
Convenience constructor for QgsZonalStatistics, using an input raster layer.
QMap< int, QVariant > QgsAttributeMap
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
const QgsCoordinateReferenceSystem & crs