22 const std::vector< QgsZonalStatistics::Statistic > STATS
38 QString QgsZonalStatisticsAlgorithm::name()
const
40 return QStringLiteral(
"zonalstatistics" );
43 QString QgsZonalStatisticsAlgorithm::displayName()
const
45 return QObject::tr(
"Zonal statistics (in place)" );
48 QStringList QgsZonalStatisticsAlgorithm::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 QgsZonalStatisticsAlgorithm::group()
const
56 return QObject::tr(
"Raster analysis" );
59 QString QgsZonalStatisticsAlgorithm::groupId()
const
61 return QStringLiteral(
"rasteranalysis" );
64 QString QgsZonalStatisticsAlgorithm::shortDescription()
const
66 return QObject::tr(
"Calculates statistics for a raster layer's values for each feature of an overlapping polygon vector layer." );
69 QString QgsZonalStatisticsAlgorithm::shortHelpString()
const
71 return QObject::tr(
"This algorithm calculates statistics of a raster layer for each feature "
72 "of an overlapping polygon vector layer. The results will be written in place." );
75 QgsProcessingAlgorithm::Flags QgsZonalStatisticsAlgorithm::flags()
const
80 QgsZonalStatisticsAlgorithm *QgsZonalStatisticsAlgorithm::createInstance()
const
82 return new QgsZonalStatisticsAlgorithm();
85 void QgsZonalStatisticsAlgorithm::initAlgorithm(
const QVariantMap & )
87 QStringList statChoices;
88 statChoices.reserve( STATS.size() );
96 QObject::tr(
"Raster band" ), 1, QStringLiteral(
"INPUT_RASTER" ) ) );
99 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"COLUMN_PREFIX" ), QObject::tr(
"Output column prefix" ), QStringLiteral(
"_" ) ) );
102 statChoices,
true, QVariantList() << 0 << 1 << 2 ) );
109 QgsRasterLayer *rasterLayer = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT_RASTER" ), context );
113 mBand = parameterAsInt( parameters, QStringLiteral(
"RASTER_BAND" ), context );
114 if ( mBand < 1 || mBand > rasterLayer->
bandCount() )
115 throw QgsProcessingException( QObject::tr(
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand )
119 mCrs = rasterLayer->
crs();
123 mPrefix = parameterAsString( parameters, QStringLiteral(
"COLUMN_PREFIX" ), context );
125 const QList< int > stats = parameterAsEnums( parameters, QStringLiteral(
"STATISTICS" ), context );
126 mStats = QgsZonalStatistics::Statistics();
127 for (
const int s : stats )
129 mStats |= STATS.at( s );
137 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral(
"INPUT_VECTOR" ), context );
148 QgsZonalStatistics::Statistics( mStats )
151 zs.calculateStatistics( feedback );
154 outputs.insert( QStringLiteral(
"INPUT_VECTOR" ), layer->
id() );