22 const std::vector< QgsZonalStatistics::Statistic > STATS
38 QString QgsZonalStatisticsFeatureBasedAlgorithm::name()
const
40 return QStringLiteral(
"zonalstatisticsfb" );
43 QString QgsZonalStatisticsFeatureBasedAlgorithm::displayName()
const
45 return QObject::tr(
"Zonal statistics" );
48 QStringList QgsZonalStatisticsFeatureBasedAlgorithm::tags()
const
50 return QObject::tr(
"stats,statistics,zones,layer,sum,maximum,minimum,mean,count,standard,deviation,"
51 "median,range,majority,minority,variety,variance,summary,raster" ).split(
',' );
54 QString QgsZonalStatisticsFeatureBasedAlgorithm::group()
const
56 return QObject::tr(
"Raster analysis" );
59 QString QgsZonalStatisticsFeatureBasedAlgorithm::groupId()
const
61 return QStringLiteral(
"rasteranalysis" );
64 QString QgsZonalStatisticsFeatureBasedAlgorithm::shortHelpString()
const
66 return QObject::tr(
"This algorithm calculates statistics of a raster layer for each feature "
67 "of an overlapping polygon vector layer." );
70 QList<int> QgsZonalStatisticsFeatureBasedAlgorithm::inputLayerTypes()
const
75 QgsZonalStatisticsFeatureBasedAlgorithm *QgsZonalStatisticsFeatureBasedAlgorithm::createInstance()
const
77 return new QgsZonalStatisticsFeatureBasedAlgorithm();
80 void QgsZonalStatisticsFeatureBasedAlgorithm::initParameters(
const QVariantMap &configuration )
82 Q_UNUSED( configuration )
83 QStringList statChoices;
84 statChoices.reserve( STATS.size() );
92 QObject::tr(
"Raster band" ), 1, QStringLiteral(
"INPUT_RASTER" ) ) );
94 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"COLUMN_PREFIX" ), QObject::tr(
"Output column prefix" ), QStringLiteral(
"_" ) ) );
97 statChoices,
true, QVariantList() << 0 << 1 << 2 ) );
100 QString QgsZonalStatisticsFeatureBasedAlgorithm::outputName()
const
102 return QObject::tr(
"Zonal Statistics" );
105 QgsFields QgsZonalStatisticsFeatureBasedAlgorithm::outputFields(
const QgsFields &inputFields )
const
107 Q_UNUSED( inputFields )
108 return mOutputFields;
113 mPrefix = parameterAsString( parameters, QStringLiteral(
"COLUMN_PREFIX" ), context );
115 const QList< int > stats = parameterAsEnums( parameters, QStringLiteral(
"STATISTICS" ), context );
116 mStats = QgsZonalStatistics::Statistics();
117 for (
int s : stats )
119 mStats |= STATS.at( s );
122 QgsRasterLayer *rasterLayer = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT_RASTER" ), context );
126 mBand = parameterAsInt( parameters, QStringLiteral(
"RASTER_BAND" ), context );
127 if ( mBand < 1 || mBand > rasterLayer->
bandCount() )
128 throw QgsProcessingException( QObject::tr(
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand )
132 throw QgsProcessingException( QObject::tr(
"Invalid raster layer. Layer %1 is invalid." ).arg( rasterLayer->
id() ) );
135 mCrs = rasterLayer->
crs();
138 std::unique_ptr<QgsFeatureSource> source( parameterAsSource( parameters, inputParameterName(), context ) );
140 mOutputFields = source->fields();
147 if ( mOutputFields.names().contains(
field.
name() ) )
151 mOutputFields.append(
field );
152 mStatFieldsMapping.insert( stat, mOutputFields.size() - 1 );
161 if ( !mCreatedTransform )
163 mCreatedTransform =
true;
169 attributes.resize( mOutputFields.size() );
174 geometry.
transform( mFeatureToRasterTransform );
179 feedback->
reportError( QObject::tr(
"Encountered a transform error when reprojecting feature with id %1." ).arg( feature.
id() ) );
183 for (
auto result = results.constBegin(); result != results.constEnd(); ++result )
185 attributes.replace( mStatFieldsMapping.value( result.key() ), result.value() );
194 bool QgsZonalStatisticsFeatureBasedAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
Custom exception class for Coordinate Reference System related exceptions.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
Base class for all map layer types.
QgsCoordinateReferenceSystem crs
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A raster band parameter for Processing algorithms.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A raster layer parameter for processing algorithms.
A string parameter for processing algorithms.
@ TypeVectorPolygon
Vector polygon layers.
QgsRasterDataProvider * clone() const override=0
Clone itself, create deep copy.
Represents a raster layer.
int bandCount() const
Returns the number of bands in this layer.
double rasterUnitsPerPixelX() const
Returns the number of raster units per each raster pixel in X axis.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
double rasterUnitsPerPixelY() const
Returns the number of raster units per each raster pixel in Y axis.
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.
QgsZonalStatistics::Result calculateStatistics(QgsFeedback *feedback)
Runs the calculation.
static QString displayName(QgsZonalStatistics::Statistic statistic)
Returns the friendly display name for a statistic.
QList< QgsFeature > QgsFeatureList