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