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 (
const 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