QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#include "qgszonalstatistics.h"
20
22
23QString QgsZonalMinimumMaximumPointAlgorithm::name() const
24{
25 return QStringLiteral( "zonalminmaxpoint" );
26}
27
28QString QgsZonalMinimumMaximumPointAlgorithm::displayName() const
29{
30 return QObject::tr( "Zonal minimum/maximum point" );
31}
32
33QStringList QgsZonalMinimumMaximumPointAlgorithm::tags() const
34{
35 return QObject::tr( "stats,statistics,zones,maximum,minimum,raster,extrema,extremum" ).split( ',' );
36}
37
38QString QgsZonalMinimumMaximumPointAlgorithm::group() const
39{
40 return QObject::tr( "Raster analysis" );
41}
42
43QString QgsZonalMinimumMaximumPointAlgorithm::groupId() const
44{
45 return QStringLiteral( "rasteranalysis" );
46}
47
48QString QgsZonalMinimumMaximumPointAlgorithm::shortDescription() const
49{
50 return QObject::tr( "Extracts point locations for minimum and maximum raster values within polygon zones." );
51}
52
53QString QgsZonalMinimumMaximumPointAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "This algorithm extracts point features corresponding to the minimum "
56 "and maximum pixel values contained within polygon zones.\n\n"
57 "The output will contain one point feature for the minimum and one "
58 "for the maximum raster value for every individual zonal feature "
59 "from a polygon layer.\n\n"
60 "The created point layer will be in the same spatial reference system as the selected raster layer." );
61}
62
63QList<int> QgsZonalMinimumMaximumPointAlgorithm::inputLayerTypes() const
64{
65 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
66}
67
68QgsZonalMinimumMaximumPointAlgorithm *QgsZonalMinimumMaximumPointAlgorithm::createInstance() const
69{
70 return new QgsZonalMinimumMaximumPointAlgorithm();
71}
72
73void QgsZonalMinimumMaximumPointAlgorithm::initParameters( const QVariantMap &configuration )
74{
75 Q_UNUSED( configuration )
76 addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT_RASTER" ), QObject::tr( "Raster layer" ) ) );
77 addParameter( new QgsProcessingParameterBand( QStringLiteral( "RASTER_BAND" ), QObject::tr( "Raster band" ), 1, QStringLiteral( "INPUT_RASTER" ) ) );
78}
79
80QString QgsZonalMinimumMaximumPointAlgorithm::outputName() const
81{
82 return QObject::tr( "Zonal Extrema" );
83}
84
85QgsFields QgsZonalMinimumMaximumPointAlgorithm::outputFields( const QgsFields &inputFields ) const
86{
87 Q_UNUSED( inputFields )
88 return mOutputFields;
89}
90
91Qgis::ProcessingSourceType QgsZonalMinimumMaximumPointAlgorithm::outputLayerType() const
92{
94}
95
96Qgis::WkbType QgsZonalMinimumMaximumPointAlgorithm::outputWkbType( Qgis::WkbType ) const
97{
99}
100
101QgsFeatureSink::SinkFlags QgsZonalMinimumMaximumPointAlgorithm::sinkFlags() const
102{
104}
105
106Qgis::ProcessingAlgorithmDocumentationFlags QgsZonalMinimumMaximumPointAlgorithm::documentationFlags() const
107{
109}
110
111QgsCoordinateReferenceSystem QgsZonalMinimumMaximumPointAlgorithm::outputCrs( const QgsCoordinateReferenceSystem & ) const
112{
113 return mCrs;
114}
115
116bool QgsZonalMinimumMaximumPointAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
117{
118 QgsRasterLayer *rasterLayer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT_RASTER" ), context );
119 if ( !rasterLayer )
120 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT_RASTER" ) ) );
121
122 mBand = parameterAsInt( parameters, QStringLiteral( "RASTER_BAND" ), context );
123 if ( mBand < 1 || mBand > rasterLayer->bandCount() )
124 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( rasterLayer->bandCount() ) );
125
126 if ( !rasterLayer->dataProvider() )
127 throw QgsProcessingException( QObject::tr( "Invalid raster layer. Layer %1 is invalid." ).arg( rasterLayer->id() ) );
128
129 mRaster.reset( rasterLayer->dataProvider()->clone() );
130 mCrs = rasterLayer->crs();
131 mPixelSizeX = rasterLayer->rasterUnitsPerPixelX();
132 mPixelSizeY = rasterLayer->rasterUnitsPerPixelY();
133 std::unique_ptr<QgsFeatureSource> source( parameterAsSource( parameters, inputParameterName(), context ) );
134
135 QgsFields newFields;
136 newFields.append( QgsField( QStringLiteral( "value" ), QMetaType::Type::Double, QString(), 20, 8 ) );
137 newFields.append( QgsField( QStringLiteral( "extremum_type" ), QMetaType::Type::QString ) );
138 mOutputFields = QgsProcessingUtils::combineFields( source->fields(), newFields );
139
140 return true;
141}
142
143QgsFeatureList QgsZonalMinimumMaximumPointAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
144{
145 if ( !mCreatedTransform )
146 {
147 mCreatedTransform = true;
148 mFeatureToRasterTransform = QgsCoordinateTransform( sourceCrs(), mCrs, context.transformContext() );
149 }
150
151 Q_UNUSED( feedback )
152 QgsAttributes attributes = feature.attributes();
153
154 QgsGeometry geometry = feature.geometry();
155 try
156 {
157 geometry.transform( mFeatureToRasterTransform );
158 }
159 catch ( QgsCsException & )
160 {
161 if ( feedback )
162 feedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( feature.id() ) );
163 }
164
165 const QMap<Qgis::ZonalStatistic, QVariant> results = QgsZonalStatistics::calculateStatistics( mRaster.get(), geometry, mPixelSizeX, mPixelSizeY, mBand, Qgis::ZonalStatistic::Min | Qgis::ZonalStatistic::MinimumPoint | Qgis::ZonalStatistic::Max | Qgis::ZonalStatistic::MaximumPoint );
166
167 QgsFeature minPointFeature( mOutputFields );
168 QgsAttributes minAttributes = attributes;
169 minAttributes << results.value( Qgis::ZonalStatistic::Min ) << QStringLiteral( "minimum" );
170 minPointFeature.setAttributes( minAttributes );
171 minPointFeature.setGeometry( QgsGeometry::fromPointXY( results.value( Qgis::ZonalStatistic::MinimumPoint ).value<QgsPointXY>() ) );
172
173 QgsFeature maxPointFeature( mOutputFields );
174 QgsAttributes maxAttributes = attributes;
175 maxAttributes << results.value( Qgis::ZonalStatistic::Max ) << QStringLiteral( "maximum" );
176 maxPointFeature.setAttributes( maxAttributes );
177 maxPointFeature.setGeometry( QgsGeometry::fromPointXY( results.value( Qgis::ZonalStatistic::MaximumPoint ).value<QgsPointXY>() ) );
178
179 return QgsFeatureList { minPointFeature, maxPointFeature };
180}
181
182bool QgsZonalMinimumMaximumPointAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
183{
184 Q_UNUSED( layer )
185 return false;
186}
187
ProcessingSourceType
Processing data source types.
Definition qgis.h:3315
@ VectorPoint
Vector point layers.
@ VectorPolygon
Vector polygon layers.
@ Max
Max of pixel values.
@ MinimumPoint
Pixel centroid for minimum pixel value.
@ Min
Min of pixel values.
@ MaximumPoint
Pixel centroid for maximum pixel value.
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3412
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
A vector of attributes.
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map 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:58
QgsAttributes attributes
Definition qgsfeature.h:67
QgsFeatureId id
Definition qgsfeature.h:66
QgsGeometry geometry
Definition qgsfeature.h:69
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
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:70
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:76
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:83
QString id
Definition qgsmaplayer.h:79
A class to represent a 2D point.
Definition qgspointxy.h:60
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