22using namespace Qt::StringLiterals;
26QString QgsRasterMinMaxAlgorithm::name()
const
28 return u
"rasterminmax"_s;
31QString QgsRasterMinMaxAlgorithm::displayName()
const
33 return QObject::tr(
"Raster minimum/maximum" );
36QStringList QgsRasterMinMaxAlgorithm::tags()
const
38 return QObject::tr(
"dem,statistics,value,extrema,extremes,largest,smallest" ).split(
',' );
41QString QgsRasterMinMaxAlgorithm::group()
const
43 return QObject::tr(
"Raster analysis" );
46QString QgsRasterMinMaxAlgorithm::groupId()
const
48 return u
"rasteranalysis"_s;
51void QgsRasterMinMaxAlgorithm::initAlgorithm(
const QVariantMap & )
56 new QgsProcessingParameterEnum( u
"EXTRACT"_s, QObject::tr(
"Extract extrema" ), QStringList() << QObject::tr(
"Minimum and Maximum" ) << QObject::tr(
"Minimum" ) << QObject::tr(
"Maximum" ),
false, 0 )
65QString QgsRasterMinMaxAlgorithm::shortHelpString()
const
68 "This algorithm extracts extrema (minimum and maximum) values from a given band of the raster layer.\n\n"
69 "The output is a vector layer containing point features for the selected extrema, at the center of the associated pixel.\n\n"
70 "If multiple pixels in the raster share the minimum or maximum value, then only one of these pixels will be included in the output."
74QString QgsRasterMinMaxAlgorithm::shortDescription()
const
76 return QObject::tr(
"Calculates the minimum and maximum pixel in a raster layer." );
79QgsRasterMinMaxAlgorithm *QgsRasterMinMaxAlgorithm::createInstance()
const
81 return new QgsRasterMinMaxAlgorithm();
86 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, u
"INPUT"_s, context );
91 mBand = parameterAsInt( parameters, u
"BAND"_s, context );
92 if ( mBand < 1 || mBand > layer->
bandCount() )
93 throw QgsProcessingException( QObject::tr(
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( layer->
bandCount() ) );
97 mLayerWidth = layer->
width();
98 mLayerHeight = layer->
height();
108 QGS_MARK_ALGORITHM_SOURCE
111 std::unique_ptr<QgsFeatureSink> sink;
112 if ( parameters.value( u
"OUTPUT"_s ).isValid() )
115 outFields.
append(
QgsField( u
"value"_s, QMetaType::Type::Double, QString(), 20, 8 ) );
116 outFields.
append(
QgsField( u
"extremum_type"_s, QMetaType::Type::QString ) );
117 sink.reset( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, outFields,
Qgis::WkbType::Point, mCrs ) );
122 const int extractType = parameterAsInt( parameters, u
"EXTRACT"_s, context );
125 iter.startRasterRead( mBand, mLayerWidth, mLayerHeight, mExtent );
131 std::unique_ptr<QgsRasterBlock> rasterBlock;
133 double rasterMinimum = std::numeric_limits<double>::quiet_NaN();
134 double rasterMaximum = std::numeric_limits<double>::quiet_NaN();
139 return QgsPointXY( blockExtent.
xMinimum() + mRasterUnitsPerPixelX * ( col + 0.5 ), blockExtent.
yMaximum() - mRasterUnitsPerPixelY * ( row + 0.5 ) );
143 while ( iter.readNextRasterPart( mBand, iterCols, iterRows, rasterBlock, iterLeft, iterTop, &blockExtent ) )
148 double blockMinimum = std::numeric_limits<double>::quiet_NaN();
149 double blockMaximum = std::numeric_limits<double>::quiet_NaN();
154 switch ( extractType )
158 if ( rasterBlock->minimumMaximum( blockMinimum, blockMinRow, blockMinCol, blockMaximum, blockMaxRow, blockMaxCol ) )
160 if ( std::isnan( rasterMinimum ) || blockMinimum < rasterMinimum )
162 rasterMinimum = blockMinimum;
163 rasterMinPoint = blockRowColToXY( blockExtent, blockMinRow, blockMinCol );
165 if ( std::isnan( rasterMaximum ) || blockMaximum > rasterMaximum )
167 rasterMaximum = blockMaximum;
168 rasterMaxPoint = blockRowColToXY( blockExtent, blockMaxRow, blockMaxCol );
176 if ( rasterBlock->minimum( blockMinimum, blockMinRow, blockMinCol ) )
178 if ( std::isnan( rasterMinimum ) || blockMinimum < rasterMinimum )
180 rasterMinimum = blockMinimum;
181 rasterMinPoint = blockRowColToXY( blockExtent, blockMinRow, blockMinCol );
189 if ( rasterBlock->maximum( blockMaximum, blockMaxRow, blockMaxCol ) )
191 if ( std::isnan( rasterMaximum ) || blockMaximum > rasterMaximum )
193 rasterMaximum = blockMaximum;
194 rasterMaxPoint = blockRowColToXY( blockExtent, blockMaxRow, blockMaxCol );
202 feedback->
setProgress( 100 * iter.progress( mBand ) );
209 if ( !std::isnan( rasterMinimum ) )
218 if ( !std::isnan( rasterMaximum ) )
229 outputs.insert( u
"OUTPUT"_s, dest );
231 outputs.insert( u
"MINIMUM"_s, !std::isnan( rasterMinimum ) ? QVariant::fromValue( rasterMinimum ) : QVariant() );
232 outputs.insert( u
"MAXIMUM"_s, !std::isnan( rasterMaximum ) ? QVariant::fromValue( rasterMaximum ) : QVariant() );
@ VectorPoint
Vector point layers.
@ 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...
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.
void setProgress(double progress)
Sets the current progress for the feedback object.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
virtual Q_INVOKABLE QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
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.
void featureAddedToSink(const QString &output)
Reports that a feature was added to the the sink associated with the specified algorithm output.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
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.