QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsalgorithmrasterminmax.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrasterminmax.cpp
3 ---------------------
4 begin : October 2024
5 copyright : (C) 2024 by 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
21
22QString QgsRasterMinMaxAlgorithm::name() const
23{
24 return QStringLiteral( "rasterminmax" );
25}
26
27QString QgsRasterMinMaxAlgorithm::displayName() const
28{
29 return QObject::tr( "Raster minimum/maximum" );
30}
31
32QStringList QgsRasterMinMaxAlgorithm::tags() const
33{
34 return QObject::tr( "dem,statistics,value,extrema,extremes,largest,smallest" ).split( ',' );
35}
36
37QString QgsRasterMinMaxAlgorithm::group() const
38{
39 return QObject::tr( "Raster analysis" );
40}
41
42QString QgsRasterMinMaxAlgorithm::groupId() const
43{
44 return QStringLiteral( "rasteranalysis" );
45}
46
47void QgsRasterMinMaxAlgorithm::initAlgorithm( const QVariantMap & )
48{
49 addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
50 addParameter( new QgsProcessingParameterBand( QStringLiteral( "BAND" ), QObject::tr( "Band number" ), 1, QStringLiteral( "INPUT" ) ) );
51 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "EXTRACT" ), QObject::tr( "Extract extrema" ), QStringList() << QObject::tr( "Minimum and Maximum" ) << QObject::tr( "Minimum" ) << QObject::tr( "Maximum" ), false, 0 ) );
52
53 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Output" ), Qgis::ProcessingSourceType::VectorPoint, QVariant(), true, true ) );
54
55 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "MINIMUM" ), QObject::tr( "Minimum" ) ) );
56 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "MAXIMUM" ), QObject::tr( "Maximum" ) ) );
57}
58
59QString QgsRasterMinMaxAlgorithm::shortHelpString() const
60{
61 return QObject::tr( "This algorithm extracts extrema (minimum and maximum) values from a given band of the raster layer.\n\n"
62 "The output is a vector layer containing point features for the selected extrema, at the center of the associated pixel.\n\n"
63 "If multiple pixels in the raster share the minimum or maximum value, then only one of these pixels will be included in the output." );
64}
65
66QString QgsRasterMinMaxAlgorithm::shortDescription() const
67{
68 return QObject::tr( "Calculates the minimum and maximum pixel in a raster layer." );
69}
70
71QgsRasterMinMaxAlgorithm *QgsRasterMinMaxAlgorithm::createInstance() const
72{
73 return new QgsRasterMinMaxAlgorithm();
74}
75
76bool QgsRasterMinMaxAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
77{
78 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
79
80 if ( !layer )
81 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
82
83 mBand = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
84 if ( mBand < 1 || mBand > layer->bandCount() )
85 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( layer->bandCount() ) );
86
87 mInterface.reset( layer->dataProvider()->clone() );
88 mHasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( mBand );
89 mLayerWidth = layer->width();
90 mLayerHeight = layer->height();
91 mExtent = layer->extent();
92 mCrs = layer->crs();
93 mRasterUnitsPerPixelX = std::abs( layer->rasterUnitsPerPixelX() );
94 mRasterUnitsPerPixelY = std::abs( layer->rasterUnitsPerPixelY() );
95 return true;
96}
97
98QVariantMap QgsRasterMinMaxAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
99{
100 QString dest;
101 std::unique_ptr<QgsFeatureSink> sink;
102 if ( parameters.value( QStringLiteral( "OUTPUT" ) ).isValid() )
103 {
104 QgsFields outFields;
105 outFields.append( QgsField( QStringLiteral( "value" ), QMetaType::Type::Double, QString(), 20, 8 ) );
106 outFields.append( QgsField( QStringLiteral( "extremum_type" ), QMetaType::Type::QString ) );
107 sink.reset( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, outFields, Qgis::WkbType::Point, mCrs ) );
108 if ( !sink )
109 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
110 }
111
112 const int extractType = parameterAsInt( parameters, QStringLiteral( "EXTRACT" ), context );
113
114 QgsRasterIterator iter( mInterface.get() );
115 iter.startRasterRead( mBand, mLayerWidth, mLayerHeight, mExtent );
116
117 int iterLeft = 0;
118 int iterTop = 0;
119 int iterCols = 0;
120 int iterRows = 0;
121 std::unique_ptr<QgsRasterBlock> rasterBlock;
122
123 double rasterMinimum = std::numeric_limits<double>::quiet_NaN();
124 double rasterMaximum = std::numeric_limits<double>::quiet_NaN();
125 QgsPointXY rasterMinPoint;
126 QgsPointXY rasterMaxPoint;
127
128 auto blockRowColToXY = [this]( const QgsRectangle &blockExtent, int row, int col ) -> QgsPointXY {
129 return QgsPointXY( blockExtent.xMinimum() + mRasterUnitsPerPixelX * ( col + 0.5 ), blockExtent.yMaximum() - mRasterUnitsPerPixelY * ( row + 0.5 ) );
130 };
131
132 QgsRectangle blockExtent;
133 while ( iter.readNextRasterPart( mBand, iterCols, iterRows, rasterBlock, iterLeft, iterTop, &blockExtent ) )
134 {
135 if ( feedback->isCanceled() )
136 break;
137
138 double blockMinimum = std::numeric_limits<double>::quiet_NaN();
139 double blockMaximum = std::numeric_limits<double>::quiet_NaN();
140 int blockMinRow = 0;
141 int blockMinCol = 0;
142 int blockMaxRow = 0;
143 int blockMaxCol = 0;
144 switch ( extractType )
145 {
146 case 0:
147 {
148 if ( rasterBlock->minimumMaximum( blockMinimum, blockMinRow, blockMinCol, blockMaximum, blockMaxRow, blockMaxCol ) )
149 {
150 if ( std::isnan( rasterMinimum ) || blockMinimum < rasterMinimum )
151 {
152 rasterMinimum = blockMinimum;
153 rasterMinPoint = blockRowColToXY( blockExtent, blockMinRow, blockMinCol );
154 }
155 if ( std::isnan( rasterMaximum ) || blockMaximum > rasterMaximum )
156 {
157 rasterMaximum = blockMaximum;
158 rasterMaxPoint = blockRowColToXY( blockExtent, blockMaxRow, blockMaxCol );
159 }
160 }
161 break;
162 }
163
164 case 1:
165 {
166 if ( rasterBlock->minimum( blockMinimum, blockMinRow, blockMinCol ) )
167 {
168 if ( std::isnan( rasterMinimum ) || blockMinimum < rasterMinimum )
169 {
170 rasterMinimum = blockMinimum;
171 rasterMinPoint = blockRowColToXY( blockExtent, blockMinRow, blockMinCol );
172 }
173 }
174 break;
175 }
176
177 case 2:
178 {
179 if ( rasterBlock->maximum( blockMaximum, blockMaxRow, blockMaxCol ) )
180 {
181 if ( std::isnan( rasterMaximum ) || blockMaximum > rasterMaximum )
182 {
183 rasterMaximum = blockMaximum;
184 rasterMaxPoint = blockRowColToXY( blockExtent, blockMaxRow, blockMaxCol );
185 }
186 }
187 break;
188 }
189 default:
190 break;
191 }
192 feedback->setProgress( 100 * iter.progress( mBand ) );
193 }
194
195 QVariantMap outputs;
196 if ( sink )
197 {
198 QgsFeature f;
199 if ( !std::isnan( rasterMinimum ) )
200 {
201 f.setAttributes( QgsAttributes() << rasterMinimum << QStringLiteral( "minimum" ) );
202 f.setGeometry( QgsGeometry::fromPointXY( rasterMinPoint ) );
203 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
204 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
205 }
206 if ( !std::isnan( rasterMaximum ) )
207 {
208 f.setAttributes( QgsAttributes() << rasterMaximum << QStringLiteral( "maximum" ) );
209 f.setGeometry( QgsGeometry::fromPointXY( rasterMaxPoint ) );
210 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
211 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
212 }
213 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
214 }
215 outputs.insert( QStringLiteral( "MINIMUM" ), !std::isnan( rasterMinimum ) ? QVariant::fromValue( rasterMinimum ) : QVariant() );
216 outputs.insert( QStringLiteral( "MAXIMUM" ), !std::isnan( rasterMaximum ) ? QVariant::fromValue( rasterMaximum ) : QVariant() );
217 return outputs;
218}
219
220
@ VectorPoint
Vector point layers.
A vector of attributes.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
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
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:83
A class to represent a 2D point.
Definition qgspointxy.h:60
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 numeric 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 feature sink output for processing algorithms.
A raster layer parameter for processing algorithms.
QgsRasterDataProvider * clone() const override=0
Clone itself, create deep copy.
virtual bool sourceHasNoDataValue(int bandNo) const
Returns true if source band has no data value.
Iterator for sequentially processing raster cells.
Represents a raster layer.
int height() const
Returns the height of the (unclipped) raster.
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.
int width() const
Returns the width of the (unclipped) raster.
A rectangle specified with double values.
double xMinimum
double yMaximum