QGIS API Documentation 3.39.0-Master (67e056379ed)
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#include "qgszonalstatistics.h"
20#include "qgsvectorlayer.h"
21
23
24const std::vector< Qgis::ZonalStatistic > STATS
25{
38};
39
40QString QgsZonalStatisticsAlgorithm::name() const
41{
42 return QStringLiteral( "zonalstatistics" );
43}
44
45QString QgsZonalStatisticsAlgorithm::displayName() const
46{
47 return QObject::tr( "Zonal statistics (in place)" );
48}
49
50QStringList QgsZonalStatisticsAlgorithm::tags() const
51{
52 return QObject::tr( "stats,statistics,zones,layer,sum,maximum,minimum,mean,count,standard,deviation,"
53 "median,range,majority,minority,variety,variance,summary,raster" ).split( ',' );
54}
55
56QString QgsZonalStatisticsAlgorithm::group() const
57{
58 return QObject::tr( "Raster analysis" );
59}
60
61QString QgsZonalStatisticsAlgorithm::groupId() const
62{
63 return QStringLiteral( "rasteranalysis" );
64}
65
66QString QgsZonalStatisticsAlgorithm::shortDescription() const
67{
68 return QObject::tr( "Calculates statistics for a raster layer's values for each feature of an overlapping polygon vector layer." );
69}
70
71QString QgsZonalStatisticsAlgorithm::shortHelpString() const
72{
73 return QObject::tr( "This algorithm calculates statistics of a raster layer for each feature "
74 "of an overlapping polygon vector layer. The results will be written in place." );
75}
76
77Qgis::ProcessingAlgorithmFlags QgsZonalStatisticsAlgorithm::flags() const
78{
80}
81
82QgsZonalStatisticsAlgorithm *QgsZonalStatisticsAlgorithm::createInstance() const
83{
84 return new QgsZonalStatisticsAlgorithm();
85}
86
87void QgsZonalStatisticsAlgorithm::initAlgorithm( const QVariantMap & )
88{
89 QStringList statChoices;
90 statChoices.reserve( STATS.size() );
91 for ( const Qgis::ZonalStatistic stat : STATS )
92 {
93 statChoices << QgsZonalStatistics::displayName( stat );
94 }
95
96 addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT_RASTER" ), QObject::tr( "Raster layer" ) ) );
97 addParameter( new QgsProcessingParameterBand( QStringLiteral( "RASTER_BAND" ),
98 QObject::tr( "Raster band" ), 1, QStringLiteral( "INPUT_RASTER" ) ) );
99 addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT_VECTOR" ), QObject::tr( "Vector layer containing zones" ),
100 QList< int >() << static_cast< int >( Qgis::ProcessingSourceType::VectorPolygon ) ) );
101 addParameter( new QgsProcessingParameterString( QStringLiteral( "COLUMN_PREFIX" ), QObject::tr( "Output column prefix" ), QStringLiteral( "_" ) ) );
102
103 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "STATISTICS" ), QObject::tr( "Statistics to calculate" ),
104 statChoices, true, QVariantList() << 0 << 1 << 2 ) );
105
106 addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "INPUT_VECTOR" ), QObject::tr( "Zonal statistics" ), Qgis::ProcessingSourceType::VectorPolygon ) );
107}
108
109bool QgsZonalStatisticsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
110{
111 QgsRasterLayer *rasterLayer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT_RASTER" ), context );
112 if ( !rasterLayer )
113 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT_RASTER" ) ) );
114
115 mBand = parameterAsInt( parameters, QStringLiteral( "RASTER_BAND" ), context );
116 if ( mBand < 1 || mBand > rasterLayer->bandCount() )
117 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand )
118 .arg( rasterLayer->bandCount() ) );
119
120 mInterface.reset( rasterLayer->dataProvider()->clone() );
121 mCrs = rasterLayer->crs();
122 mPixelSizeX = rasterLayer->rasterUnitsPerPixelX();
123 mPixelSizeY = rasterLayer->rasterUnitsPerPixelY();
124
125 mPrefix = parameterAsString( parameters, QStringLiteral( "COLUMN_PREFIX" ), context );
126
127 const QList< int > stats = parameterAsEnums( parameters, QStringLiteral( "STATISTICS" ), context );
128 mStats = Qgis::ZonalStatistics();
129 for ( const int s : stats )
130 {
131 mStats |= STATS.at( s );
132 }
133
134 return true;
135}
136
137QVariantMap QgsZonalStatisticsAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
138{
139 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT_VECTOR" ), context );
140 if ( !layer )
141 throw QgsProcessingException( QObject::tr( "Invalid zones layer" ) );
142
143 QgsZonalStatistics zs( layer,
144 mInterface.get(),
145 mCrs,
146 mPixelSizeX,
147 mPixelSizeY,
148 mPrefix,
149 mBand,
150 Qgis::ZonalStatistics( mStats )
151 );
152
153 zs.calculateStatistics( feedback );
154
155 QVariantMap outputs;
156 outputs.insert( QStringLiteral( "INPUT_VECTOR" ), layer->id() );
157 return outputs;
158}
159
@ VectorPolygon
Vector polygon layers.
ZonalStatistic
Statistics to be calculated during a zonal statistics operation.
Definition qgis.h:4919
@ 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.
@ Count
Pixel count.
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3076
QFlags< ZonalStatistic > ZonalStatistics
Statistics to be calculated during a zonal statistics operation.
Definition qgis.h:4942
@ Deprecated
Algorithm is deprecated.
@ NoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:82
QString id
Definition qgsmaplayer.h:78
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
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.
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 ...
static QString displayName(Qgis::ZonalStatistic statistic)
Returns the friendly display name for a statistic.