24QString QgsZonalStatisticsFeatureBasedAlgorithm::name()
const
26 return QStringLiteral(
"zonalstatisticsfb" );
29QString QgsZonalStatisticsFeatureBasedAlgorithm::displayName()
const
31 return QObject::tr(
"Zonal statistics" );
34QStringList QgsZonalStatisticsFeatureBasedAlgorithm::tags()
const
36 return QObject::tr(
"stats,statistics,zones,layer,sum,maximum,minimum,mean,count,standard,deviation,"
37 "median,range,majority,minority,variety,variance,summary,raster" )
41QString QgsZonalStatisticsFeatureBasedAlgorithm::group()
const
43 return QObject::tr(
"Raster analysis" );
46QString QgsZonalStatisticsFeatureBasedAlgorithm::groupId()
const
48 return QStringLiteral(
"rasteranalysis" );
51QString QgsZonalStatisticsFeatureBasedAlgorithm::shortHelpString()
const
53 return QObject::tr(
"This algorithm calculates statistics of a raster layer for each feature "
54 "of an overlapping polygon vector layer." );
57QList<int> QgsZonalStatisticsFeatureBasedAlgorithm::inputLayerTypes()
const
62QgsZonalStatisticsFeatureBasedAlgorithm *QgsZonalStatisticsFeatureBasedAlgorithm::createInstance()
const
64 return new QgsZonalStatisticsFeatureBasedAlgorithm();
67void QgsZonalStatisticsFeatureBasedAlgorithm::initParameters(
const QVariantMap &configuration )
69 Q_UNUSED( configuration )
70 QStringList statChoices;
71 statChoices.reserve( STATS.size() );
78 addParameter(
new QgsProcessingParameterBand( QStringLiteral(
"RASTER_BAND" ), QObject::tr(
"Raster band" ), 1, QStringLiteral(
"INPUT_RASTER" ) ) );
80 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"COLUMN_PREFIX" ), QObject::tr(
"Output column prefix" ), QStringLiteral(
"_" ) ) );
82 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"STATISTICS" ), QObject::tr(
"Statistics to calculate" ), statChoices,
true, QVariantList() << 0 << 1 << 2 ) );
85QString QgsZonalStatisticsFeatureBasedAlgorithm::outputName()
const
87 return QObject::tr(
"Zonal Statistics" );
90QgsFields QgsZonalStatisticsFeatureBasedAlgorithm::outputFields(
const QgsFields &inputFields )
const
92 Q_UNUSED( inputFields )
98 mPrefix = parameterAsString( parameters, QStringLiteral(
"COLUMN_PREFIX" ), context );
100 const QList<int> stats = parameterAsEnums( parameters, QStringLiteral(
"STATISTICS" ), context );
102 for (
const int s : stats )
104 mStats |= STATS.at( s );
107 QgsRasterLayer *rasterLayer = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT_RASTER" ), context );
111 mBand = parameterAsInt( parameters, QStringLiteral(
"RASTER_BAND" ), context );
112 if ( mBand < 1 || mBand > rasterLayer->
bandCount() )
113 throw QgsProcessingException( QObject::tr(
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( rasterLayer->
bandCount() ) );
116 throw QgsProcessingException( QObject::tr(
"Invalid raster layer. Layer %1 is invalid." ).arg( rasterLayer->
id() ) );
119 mCrs = rasterLayer->
crs();
122 std::unique_ptr<QgsFeatureSource> source( parameterAsSource( parameters, inputParameterName(), context ) );
124 mOutputFields = source->fields();
131 if ( mOutputFields.names().contains( field.
name() ) )
135 mOutputFields.append( field );
136 mStatFieldsMapping.insert( stat, mOutputFields.size() - 1 );
145 if ( !mCreatedTransform )
147 mCreatedTransform =
true;
153 attributes.resize( mOutputFields.size() );
158 geometry.
transform( mFeatureToRasterTransform );
163 feedback->
reportError( QObject::tr(
"Encountered a transform error when reprojecting feature with id %1." ).arg( feature.
id() ) );
167 for (
auto result = results.constBegin(); result != results.constEnd(); ++result )
169 attributes.replace( mStatFieldsMapping.value( result.key() ), result.value() );
178bool QgsZonalStatisticsFeatureBasedAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
@ VectorPolygon
Vector polygon layers.
ZonalStatistic
Statistics to be calculated during a zonal statistics operation.
QFlags< ZonalStatistic > ZonalStatistics
Statistics to be calculated during a zonal statistics operation.
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.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
Base class for all map layer types.
QgsCoordinateReferenceSystem crs
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.
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.
Qgis::ZonalStatisticResult calculateStatistics(QgsFeedback *feedback)
Runs the calculation.
static QString displayName(Qgis::ZonalStatistic statistic)
Returns the friendly display name for a statistic.
static QString shortName(Qgis::ZonalStatistic statistic)
Returns a short, friendly display name for a statistic, suitable for use in a field name.
QList< QgsFeature > QgsFeatureList