QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgsalgorithmzonalstatistics.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmzonalstatistics.cpp
3 ---------------------
4 begin : December 2019
5 copyright : (C) 2019 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
21
22const std::vector< QgsZonalStatistics::Statistic > STATS
23{
36};
37
38QString QgsZonalStatisticsAlgorithm::name() const
39{
40 return QStringLiteral( "zonalstatistics" );
41}
42
43QString QgsZonalStatisticsAlgorithm::displayName() const
44{
45 return QObject::tr( "Zonal statistics (in place)" );
46}
47
48QStringList QgsZonalStatisticsAlgorithm::tags() const
49{
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( ',' );
52}
53
54QString QgsZonalStatisticsAlgorithm::group() const
55{
56 return QObject::tr( "Raster analysis" );
57}
58
59QString QgsZonalStatisticsAlgorithm::groupId() const
60{
61 return QStringLiteral( "rasteranalysis" );
62}
63
64QString QgsZonalStatisticsAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Calculates statistics for a raster layer's values for each feature of an overlapping polygon vector layer." );
67}
68
69QString QgsZonalStatisticsAlgorithm::shortHelpString() const
70{
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." );
73}
74
75QgsProcessingAlgorithm::Flags QgsZonalStatisticsAlgorithm::flags() const
76{
78}
79
80QgsZonalStatisticsAlgorithm *QgsZonalStatisticsAlgorithm::createInstance() const
81{
82 return new QgsZonalStatisticsAlgorithm();
83}
84
85void QgsZonalStatisticsAlgorithm::initAlgorithm( const QVariantMap & )
86{
87 QStringList statChoices;
88 statChoices.reserve( STATS.size() );
89 for ( const QgsZonalStatistics::Statistic stat : STATS )
90 {
91 statChoices << QgsZonalStatistics::displayName( stat );
92 }
93
94 addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT_RASTER" ), QObject::tr( "Raster layer" ) ) );
95 addParameter( new QgsProcessingParameterBand( QStringLiteral( "RASTER_BAND" ),
96 QObject::tr( "Raster band" ), 1, QStringLiteral( "INPUT_RASTER" ) ) );
97 addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT_VECTOR" ), QObject::tr( "Vector layer containing zones" ),
98 QList< int >() << QgsProcessing::TypeVectorPolygon ) );
99 addParameter( new QgsProcessingParameterString( QStringLiteral( "COLUMN_PREFIX" ), QObject::tr( "Output column prefix" ), QStringLiteral( "_" ) ) );
100
101 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "STATISTICS" ), QObject::tr( "Statistics to calculate" ),
102 statChoices, true, QVariantList() << 0 << 1 << 2 ) );
103
104 addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "INPUT_VECTOR" ), QObject::tr( "Zonal statistics" ), QgsProcessing::TypeVectorPolygon ) );
105}
106
107bool QgsZonalStatisticsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
108{
109 QgsRasterLayer *rasterLayer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT_RASTER" ), context );
110 if ( !rasterLayer )
111 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT_RASTER" ) ) );
112
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 )
116 .arg( rasterLayer->bandCount() ) );
117
118 mInterface.reset( rasterLayer->dataProvider()->clone() );
119 mCrs = rasterLayer->crs();
120 mPixelSizeX = rasterLayer->rasterUnitsPerPixelX();
121 mPixelSizeY = rasterLayer->rasterUnitsPerPixelY();
122
123 mPrefix = parameterAsString( parameters, QStringLiteral( "COLUMN_PREFIX" ), context );
124
125 const QList< int > stats = parameterAsEnums( parameters, QStringLiteral( "STATISTICS" ), context );
126 mStats = QgsZonalStatistics::Statistics();
127 for ( const int s : stats )
128 {
129 mStats |= STATS.at( s );
130 }
131
132 return true;
133}
134
135QVariantMap QgsZonalStatisticsAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
136{
137 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT_VECTOR" ), context );
138 if ( !layer )
139 throw QgsProcessingException( QObject::tr( "Invalid zones layer" ) );
140
141 QgsZonalStatistics zs( layer,
142 mInterface.get(),
143 mCrs,
144 mPixelSizeX,
145 mPixelSizeY,
146 mPrefix,
147 mBand,
148 QgsZonalStatistics::Statistics( mStats )
149 );
150
151 zs.calculateStatistics( feedback );
152
153 QVariantMap outputs;
154 outputs.insert( QStringLiteral( "INPUT_VECTOR" ), layer->id() );
155 return outputs;
156}
157
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:80
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
@ FlagNoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
@ FlagDeprecated
Algorithm is deprecated.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A vector layer output for processing algorithms.
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.
A vector layer (with or without geometry) parameter for processing algorithms.
@ TypeVectorPolygon
Vector polygon layers.
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.
Represents a vector layer which manages a vector based data sets.
A class that calculates raster statistics (count, sum, mean) for a polygon or multipolygon layer and ...
Statistic
Enumeration of flags that specify statistics to be calculated.
@ Minority
Minority of pixel values.
@ Variety
Variety (count of distinct) pixel values.
@ Variance
Variance of pixel values.
@ Mean
Mean of pixel values.
@ Majority
Majority of pixel values.
@ Range
Range of pixel values (max - min)
@ Min
Min of pixel values.
@ Sum
Sum of pixel values.
@ StDev
Standard deviation of pixel values.
@ Max
Max of pixel values.
@ Median
Median of pixel values.
static QString displayName(QgsZonalStatistics::Statistic statistic)
Returns the friendly display name for a statistic.