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