QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsalgorithmzonalminmaxpoint.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmzonalminmaxpoint.cpp
3 ------------------------------
4 begin : November 2024
5 copyright : (C) 2024 Nyall Dawson
6 email : nyall dot dawson 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
20#include "qgszonalstatistics.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsZonalMinimumMaximumPointAlgorithm::name() const
29{
30 return u"zonalminmaxpoint"_s;
31}
32
33QString QgsZonalMinimumMaximumPointAlgorithm::displayName() const
34{
35 return QObject::tr( "Zonal minimum/maximum point" );
36}
37
38QStringList QgsZonalMinimumMaximumPointAlgorithm::tags() const
39{
40 return QObject::tr( "stats,statistics,zones,maximum,minimum,raster,extrema,extremum" ).split( ',' );
41}
42
43QString QgsZonalMinimumMaximumPointAlgorithm::group() const
44{
45 return QObject::tr( "Raster analysis" );
46}
47
48QString QgsZonalMinimumMaximumPointAlgorithm::groupId() const
49{
50 return u"rasteranalysis"_s;
51}
52
53QString QgsZonalMinimumMaximumPointAlgorithm::shortDescription() const
54{
55 return QObject::tr( "Extracts point locations for minimum and maximum raster values within polygon zones." );
56}
57
58QString QgsZonalMinimumMaximumPointAlgorithm::shortHelpString() const
59{
60 return QObject::tr(
61 "This algorithm extracts point features corresponding to the minimum "
62 "and maximum pixel values contained within polygon zones.\n\n"
63 "The output will contain one point feature for the minimum and one "
64 "for the maximum raster value for every individual zonal feature "
65 "from a polygon layer.\n\n"
66 "The created point layer will be in the same spatial reference system as the selected raster layer."
67 );
68}
69
70QList<int> QgsZonalMinimumMaximumPointAlgorithm::inputLayerTypes() const
71{
72 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
73}
74
75QgsZonalMinimumMaximumPointAlgorithm *QgsZonalMinimumMaximumPointAlgorithm::createInstance() const
76{
77 return new QgsZonalMinimumMaximumPointAlgorithm();
78}
79
80void QgsZonalMinimumMaximumPointAlgorithm::initParameters( const QVariantMap &configuration )
81{
82 Q_UNUSED( configuration )
83 addParameter( new QgsProcessingParameterRasterLayer( u"INPUT_RASTER"_s, QObject::tr( "Raster layer" ) ) );
84 addParameter( new QgsProcessingParameterBand( u"RASTER_BAND"_s, QObject::tr( "Raster band" ), 1, u"INPUT_RASTER"_s ) );
85}
86
87QString QgsZonalMinimumMaximumPointAlgorithm::outputName() const
88{
89 return QObject::tr( "Zonal Extrema" );
90}
91
92QgsFields QgsZonalMinimumMaximumPointAlgorithm::outputFields( const QgsFields &inputFields ) const
93{
94 Q_UNUSED( inputFields )
95 return mOutputFields;
96}
97
98Qgis::ProcessingSourceType QgsZonalMinimumMaximumPointAlgorithm::outputLayerType() const
99{
101}
102
103Qgis::WkbType QgsZonalMinimumMaximumPointAlgorithm::outputWkbType( Qgis::WkbType ) const
104{
106}
107
108QgsFeatureSink::SinkFlags QgsZonalMinimumMaximumPointAlgorithm::sinkFlags() const
109{
111}
112
113Qgis::ProcessingAlgorithmDocumentationFlags QgsZonalMinimumMaximumPointAlgorithm::documentationFlags() const
114{
116}
117
118QgsCoordinateReferenceSystem QgsZonalMinimumMaximumPointAlgorithm::outputCrs( const QgsCoordinateReferenceSystem & ) const
119{
120 return mCrs;
121}
122
123bool QgsZonalMinimumMaximumPointAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
124{
125 QgsRasterLayer *rasterLayer = parameterAsRasterLayer( parameters, u"INPUT_RASTER"_s, context );
126 if ( !rasterLayer )
127 throw QgsProcessingException( invalidRasterError( parameters, u"INPUT_RASTER"_s ) );
128
129 mBand = parameterAsInt( parameters, u"RASTER_BAND"_s, context );
130 if ( mBand < 1 || mBand > rasterLayer->bandCount() )
131 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( rasterLayer->bandCount() ) );
132
133 if ( !rasterLayer->dataProvider() )
134 throw QgsProcessingException( QObject::tr( "Invalid raster layer. Layer %1 is invalid." ).arg( rasterLayer->id() ) );
135
136 mRaster.reset( rasterLayer->dataProvider()->clone() );
137 mCrs = rasterLayer->crs();
138 mPixelSizeX = rasterLayer->rasterUnitsPerPixelX();
139 mPixelSizeY = rasterLayer->rasterUnitsPerPixelY();
140 std::unique_ptr<QgsFeatureSource> source( parameterAsSource( parameters, inputParameterName(), context ) );
141
142 QgsFields newFields;
143 newFields.append( QgsField( u"value"_s, QMetaType::Type::Double, QString(), 20, 8 ) );
144 newFields.append( QgsField( u"extremum_type"_s, QMetaType::Type::QString ) );
145 mOutputFields = QgsProcessingUtils::combineFields( source->fields(), newFields );
146
147 return true;
148}
149
150QgsFeatureList QgsZonalMinimumMaximumPointAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
151{
152 if ( !mCreatedTransform )
153 {
154 mCreatedTransform = true;
155 mFeatureToRasterTransform = QgsCoordinateTransform( sourceCrs(), mCrs, context.transformContext() );
156 }
157
158 Q_UNUSED( feedback )
159 QgsAttributes attributes = feature.attributes();
160
161 QgsGeometry geometry = feature.geometry();
162 try
163 {
164 geometry.transform( mFeatureToRasterTransform );
165 }
166 catch ( QgsCsException & )
167 {
168 if ( feedback )
169 feedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( feature.id() ) );
170 }
171
172 const QMap<Qgis::ZonalStatistic, QVariant> results = QgsZonalStatistics::
174
175 QgsFeature minPointFeature( mOutputFields );
176 QgsAttributes minAttributes = attributes;
177 minAttributes << results.value( Qgis::ZonalStatistic::Min ) << u"minimum"_s;
178 minPointFeature.setAttributes( minAttributes );
179 minPointFeature.setGeometry( QgsGeometry::fromPointXY( results.value( Qgis::ZonalStatistic::MinimumPoint ).value<QgsPointXY>() ) );
180
181 QgsFeature maxPointFeature( mOutputFields );
182 QgsAttributes maxAttributes = attributes;
183 maxAttributes << results.value( Qgis::ZonalStatistic::Max ) << u"maximum"_s;
184 maxPointFeature.setAttributes( maxAttributes );
185 maxPointFeature.setGeometry( QgsGeometry::fromPointXY( results.value( Qgis::ZonalStatistic::MaximumPoint ).value<QgsPointXY>() ) );
186
187 return QgsFeatureList { minPointFeature, maxPointFeature };
188}
189
190bool QgsZonalMinimumMaximumPointAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
191{
192 Q_UNUSED( layer )
193 return false;
194}
195
ProcessingSourceType
Processing data source types.
Definition qgis.h:3645
@ VectorPoint
Vector point layers.
Definition qgis.h:3648
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3650
@ Max
Max of pixel values.
Definition qgis.h:6126
@ MinimumPoint
Pixel centroid for minimum pixel value.
Definition qgis.h:6132
@ Min
Min of pixel values.
Definition qgis.h:6125
@ MaximumPoint
Pixel centroid for maximum pixel value.
Definition qgis.h:6133
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3734
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3745
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ Point
Point.
Definition qgis.h:296
A vector of attributes.
Represents a coordinate reference system (CRS).
Handles coordinate transforms between two coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
QFlags< SinkFlag > SinkFlags
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsAttributes attributes
Definition qgsfeature.h:69
QgsFeatureId id
Definition qgsfeature.h:68
QgsGeometry geometry
Definition qgsfeature.h:71
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:75
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
Base class for all map layer types.
Definition qgsmaplayer.h:83
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:90
QString id
Definition qgsmaplayer.h:86
Represents a 2D point.
Definition qgspointxy.h:62
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A raster band parameter for Processing algorithms.
A raster layer parameter for processing algorithms.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
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.
Qgis::ZonalStatisticResult calculateStatistics(QgsFeedback *feedback)
Runs the calculation.
QList< QgsFeature > QgsFeatureList