23const std::vector< Qgis::ZonalStatistic > STATS
39QString QgsZonalStatisticsFeatureBasedAlgorithm::name()
const
41 return QStringLiteral(
"zonalstatisticsfb" );
44QString QgsZonalStatisticsFeatureBasedAlgorithm::displayName()
const
46 return QObject::tr(
"Zonal statistics" );
49QStringList QgsZonalStatisticsFeatureBasedAlgorithm::tags()
const
51 return QObject::tr(
"stats,statistics,zones,layer,sum,maximum,minimum,mean,count,standard,deviation,"
52 "median,range,majority,minority,variety,variance,summary,raster" ).split(
',' );
55QString QgsZonalStatisticsFeatureBasedAlgorithm::group()
const
57 return QObject::tr(
"Raster analysis" );
60QString QgsZonalStatisticsFeatureBasedAlgorithm::groupId()
const
62 return QStringLiteral(
"rasteranalysis" );
65QString QgsZonalStatisticsFeatureBasedAlgorithm::shortHelpString()
const
67 return QObject::tr(
"This algorithm calculates statistics of a raster layer for each feature "
68 "of an overlapping polygon vector layer." );
71QList<int> QgsZonalStatisticsFeatureBasedAlgorithm::inputLayerTypes()
const
76QgsZonalStatisticsFeatureBasedAlgorithm *QgsZonalStatisticsFeatureBasedAlgorithm::createInstance()
const
78 return new QgsZonalStatisticsFeatureBasedAlgorithm();
81void QgsZonalStatisticsFeatureBasedAlgorithm::initParameters(
const QVariantMap &configuration )
83 Q_UNUSED( configuration )
84 QStringList statChoices;
85 statChoices.reserve( STATS.size() );
93 QObject::tr(
"Raster band" ), 1, QStringLiteral(
"INPUT_RASTER" ) ) );
95 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"COLUMN_PREFIX" ), QObject::tr(
"Output column prefix" ), QStringLiteral(
"_" ) ) );
98 statChoices,
true, QVariantList() << 0 << 1 << 2 ) );
101QString QgsZonalStatisticsFeatureBasedAlgorithm::outputName()
const
103 return QObject::tr(
"Zonal Statistics" );
106QgsFields QgsZonalStatisticsFeatureBasedAlgorithm::outputFields(
const QgsFields &inputFields )
const
108 Q_UNUSED( inputFields )
109 return mOutputFields;
114 mPrefix = parameterAsString( parameters, QStringLiteral(
"COLUMN_PREFIX" ), context );
116 const QList< int > stats = parameterAsEnums( parameters, QStringLiteral(
"STATISTICS" ), context );
118 for (
const int s : stats )
120 mStats |= STATS.at( s );
123 QgsRasterLayer *rasterLayer = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT_RASTER" ), context );
127 mBand = parameterAsInt( parameters, QStringLiteral(
"RASTER_BAND" ), context );
128 if ( mBand < 1 || mBand > rasterLayer->
bandCount() )
129 throw QgsProcessingException( QObject::tr(
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand )
133 throw QgsProcessingException( QObject::tr(
"Invalid raster layer. Layer %1 is invalid." ).arg( rasterLayer->
id() ) );
136 mCrs = rasterLayer->
crs();
139 std::unique_ptr<QgsFeatureSource> source( parameterAsSource( parameters, inputParameterName(), context ) );
141 mOutputFields = source->fields();
148 if ( mOutputFields.names().contains( field.
name() ) )
152 mOutputFields.append( field );
153 mStatFieldsMapping.insert( stat, mOutputFields.size() - 1 );
162 if ( !mCreatedTransform )
164 mCreatedTransform =
true;
170 attributes.resize( mOutputFields.size() );
175 geometry.
transform( mFeatureToRasterTransform );
180 feedback->
reportError( QObject::tr(
"Encountered a transform error when reprojecting feature with id %1." ).arg( feature.
id() ) );
184 for (
auto result = results.constBegin(); result != results.constEnd(); ++result )
186 attributes.replace( mStatFieldsMapping.value( result.key() ), result.value() );
195bool QgsZonalStatisticsFeatureBasedAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
@ VectorPolygon
Vector polygon layers.
ZonalStatistic
Statistics to be calculated during a zonal statistics operation.
@ StDev
Standard deviation of pixel values.
@ Mean
Mean of pixel values.
@ Median
Median of pixel values.
@ Max
Max of pixel values.
@ Variance
Variance of pixel values.
@ Min
Min of pixel values.
@ Range
Range of pixel values (max - min)
@ Sum
Sum of pixel values.
@ Minority
Minority of pixel values.
@ Majority
Majority of pixel values.
@ Variety
Variety (count of distinct) pixel values.
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