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" );
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::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 QgsProcessingAlgorithm::Flags QgsZonalStatisticsAlgorithm::flags()
const
75 QgsZonalStatisticsAlgorithm *QgsZonalStatisticsAlgorithm::createInstance()
const
77 return new QgsZonalStatisticsAlgorithm();
80 void QgsZonalStatisticsAlgorithm::initAlgorithm(
const QVariantMap & )
82 QStringList statChoices;
83 statChoices.reserve( STATS.size() );
91 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 ) );
104 QgsRasterLayer *rasterLayer = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT_RASTER" ), context );
108 mBand = parameterAsInt( parameters, QStringLiteral(
"RASTER_BAND" ), context );
109 if ( mBand < 1 || mBand > rasterLayer->
bandCount() )
110 throw QgsProcessingException( QObject::tr(
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand )
114 mCrs = rasterLayer->
crs();
118 mPrefix = parameterAsString( parameters, QStringLiteral(
"COLUMN_PREFIX" ), context );
120 const QList< int > stats = parameterAsEnums( parameters, QStringLiteral(
"STATISTICS" ), context );
122 for (
int s : stats )
124 mStats |= STATS.at( s );
132 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral(
"INPUT_VECTOR" ), context );
143 QgsZonalStatistics::Statistics( mStats )
146 zs.calculateStatistics( feedback );
149 outputs.insert( QStringLiteral(
"INPUT_VECTOR" ), layer->
id() );