QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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" ),
78 QObject::tr( "Raster band" ), 1, QStringLiteral( "INPUT_RASTER" ) ) );
79}
80
81QString QgsZonalMinimumMaximumPointAlgorithm::outputName() const
82{
83 return QObject::tr( "Zonal Extrema" );
84}
85
86QgsFields QgsZonalMinimumMaximumPointAlgorithm::outputFields( const QgsFields &inputFields ) const
87{
88 Q_UNUSED( inputFields )
89 return mOutputFields;
90}
91
92Qgis::ProcessingSourceType QgsZonalMinimumMaximumPointAlgorithm::outputLayerType() const
93{
95}
96
97Qgis::WkbType QgsZonalMinimumMaximumPointAlgorithm::outputWkbType( Qgis::WkbType ) const
98{
100}
101
102QgsFeatureSink::SinkFlags QgsZonalMinimumMaximumPointAlgorithm::sinkFlags() const
103{
105}
106
107Qgis::ProcessingAlgorithmDocumentationFlags QgsZonalMinimumMaximumPointAlgorithm::documentationFlags() const
108{
110}
111
112QgsCoordinateReferenceSystem QgsZonalMinimumMaximumPointAlgorithm::outputCrs( const QgsCoordinateReferenceSystem & ) const
113{
114 return mCrs;
115}
116
117bool QgsZonalMinimumMaximumPointAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
118{
119 QgsRasterLayer *rasterLayer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT_RASTER" ), context );
120 if ( !rasterLayer )
121 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT_RASTER" ) ) );
122
123 mBand = parameterAsInt( parameters, QStringLiteral( "RASTER_BAND" ), context );
124 if ( mBand < 1 || mBand > rasterLayer->bandCount() )
125 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand )
126 .arg( rasterLayer->bandCount() ) );
127
128 if ( !rasterLayer->dataProvider() )
129 throw QgsProcessingException( QObject::tr( "Invalid raster layer. Layer %1 is invalid." ).arg( rasterLayer->id() ) );
130
131 mRaster.reset( rasterLayer->dataProvider()->clone() );
132 mCrs = rasterLayer->crs();
133 mPixelSizeX = rasterLayer->rasterUnitsPerPixelX();
134 mPixelSizeY = rasterLayer->rasterUnitsPerPixelY();
135 std::unique_ptr<QgsFeatureSource> source( parameterAsSource( parameters, inputParameterName(), context ) );
136
137 QgsFields newFields;
138 newFields.append( QgsField( QStringLiteral( "value" ), QMetaType::Type::Double, QString(), 20, 8 ) );
139 newFields.append( QgsField( QStringLiteral( "extremum_type" ), QMetaType::Type::QString ) );
140 mOutputFields = QgsProcessingUtils::combineFields( source->fields(), newFields );
141
142 return true;
143}
144
145QgsFeatureList QgsZonalMinimumMaximumPointAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
146{
147 if ( !mCreatedTransform )
148 {
149 mCreatedTransform = true;
150 mFeatureToRasterTransform = QgsCoordinateTransform( sourceCrs(), mCrs, context.transformContext() );
151 }
152
153 Q_UNUSED( feedback )
154 QgsAttributes attributes = feature.attributes();
155
156 QgsGeometry geometry = feature.geometry();
157 try
158 {
159 geometry.transform( mFeatureToRasterTransform );
160 }
161 catch ( QgsCsException & )
162 {
163 if ( feedback )
164 feedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( feature.id() ) );
165 }
166
167 const QMap<Qgis::ZonalStatistic, QVariant> results = QgsZonalStatistics::calculateStatistics( mRaster.get(), geometry, mPixelSizeX, mPixelSizeY, mBand,
169
170 QgsFeature minPointFeature( mOutputFields );
171 QgsAttributes minAttributes = attributes;
172 minAttributes << results.value( Qgis::ZonalStatistic::Min ) << QStringLiteral( "minimum" );
173 minPointFeature.setAttributes( minAttributes );
174 minPointFeature.setGeometry( QgsGeometry::fromPointXY( results.value( Qgis::ZonalStatistic::MinimumPoint ).value< QgsPointXY >() ) );
175
176 QgsFeature maxPointFeature( mOutputFields );
177 QgsAttributes maxAttributes = attributes;
178 maxAttributes << results.value( Qgis::ZonalStatistic::Max ) << QStringLiteral( "maximum" );
179 maxPointFeature.setAttributes( maxAttributes );
180 maxPointFeature.setGeometry( QgsGeometry::fromPointXY( results.value( Qgis::ZonalStatistic::MaximumPoint ).value< QgsPointXY >() ) );
181
182 return QgsFeatureList { minPointFeature, maxPointFeature };
183}
184
185bool QgsZonalMinimumMaximumPointAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
186{
187 Q_UNUSED( layer )
188 return false;
189}
190
ProcessingSourceType
Processing data source types.
Definition qgis.h:3270
@ 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:3367
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