33QString QgsReclassifyAlgorithmBase::group()
const
35 return QObject::tr(
"Raster analysis" );
38QString QgsReclassifyAlgorithmBase::groupId()
const
40 return QStringLiteral(
"rasteranalysis" );
43void QgsReclassifyAlgorithmBase::initAlgorithm(
const QVariantMap & )
46 QObject::tr(
"Raster layer" ) ) );
48 QObject::tr(
"Band number" ), 1, QStringLiteral(
"INPUT_RASTER" ) ) );
52 std::unique_ptr< QgsProcessingParameterNumber > noDataValueParam = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"NO_DATA" ),
55 addParameter( noDataValueParam.release() );
57 std::unique_ptr< QgsProcessingParameterEnum > boundsHandling = std::make_unique< QgsProcessingParameterEnum >( QStringLiteral(
"RANGE_BOUNDARIES" ),
58 QObject::tr(
"Range boundaries" ), QStringList() << QObject::tr(
"min < value <= max" )
59 << QObject::tr(
"min <= value < max" )
60 << QObject::tr(
"min <= value <= max" )
61 << QObject::tr(
"min < value < max" ),
64 addParameter( boundsHandling.release() );
66 std::unique_ptr< QgsProcessingParameterBoolean > missingValuesParam = std::make_unique< QgsProcessingParameterBoolean >( QStringLiteral(
"NODATA_FOR_MISSING" ),
67 QObject::tr(
"Use no data when no range matches value" ),
false,
false );
69 addParameter( missingValuesParam.release() );
71 std::unique_ptr< QgsProcessingParameterDefinition > typeChoice = QgsRasterAnalysisUtils::createRasterTypeParameter( QStringLiteral(
"DATA_TYPE" ), QObject::tr(
"Output data type" ),
Qgis::DataType::Float32 );
73 addParameter( typeChoice.release() );
80 mDataType = QgsRasterAnalysisUtils::rasterTypeChoiceToDataType( parameterAsEnum( parameters, QStringLiteral(
"DATA_TYPE" ), context ) );
81 if ( mDataType ==
Qgis::DataType::Int8 && atoi( GDALVersionInfo(
"VERSION_NUM" ) ) < GDAL_COMPUTE_VERSION( 3, 7, 0 ) )
84 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT_RASTER" ), context );
89 mBand = parameterAsInt( parameters, QStringLiteral(
"RASTER_BAND" ), context );
90 if ( mBand < 1 || mBand > layer->
bandCount() )
91 throw QgsProcessingException( QObject::tr(
"Invalid band number for RASTER_BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand )
99 mNbCellsXProvider = mInterface->xSize();
100 mNbCellsYProvider = mInterface->ySize();
102 mNoDataValue = parameterAsDouble( parameters, QStringLiteral(
"NO_DATA" ), context );
103 mUseNoDataForMissingValues = parameterAsBoolean( parameters, QStringLiteral(
"NODATA_FOR_MISSING" ), context );
105 const int boundsType = parameterAsEnum( parameters, QStringLiteral(
"RANGE_BOUNDARIES" ), context );
106 switch ( boundsType )
109 mBoundsType = QgsReclassifyUtils::RasterClass::IncludeMax;
113 mBoundsType = QgsReclassifyUtils::RasterClass::IncludeMin;
117 mBoundsType = QgsReclassifyUtils::RasterClass::IncludeMinAndMax;
121 mBoundsType = QgsReclassifyUtils::RasterClass::Exclusive;
125 return _prepareAlgorithm( parameters, context, feedback );
130 const QVector< QgsReclassifyUtils::RasterClass > classes = createClasses( mBoundsType, parameters, context, feedback );
132 QgsReclassifyUtils::reportClasses( classes, feedback );
133 QgsReclassifyUtils::checkForOverlaps( classes, feedback );
135 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
136 const QFileInfo fi( outputFile );
139 std::unique_ptr< QgsRasterFileWriter > writer = std::make_unique< QgsRasterFileWriter >( outputFile );
140 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
141 writer->setOutputFormat( outputFormat );
142 std::unique_ptr<QgsRasterDataProvider > provider( writer->createOneBandRaster( mDataType, mNbCellsXProvider, mNbCellsYProvider, mExtent, mCrs ) );
145 if ( !provider->isValid() )
148 provider->setNoDataValue( 1, mNoDataValue );
150 QgsReclassifyUtils::reclassify( classes, mInterface.get(), mBand, mExtent, mNbCellsXProvider, mNbCellsYProvider, provider.get(), mNoDataValue, mUseNoDataForMissingValues,
154 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
163QString QgsReclassifyByLayerAlgorithm::name()
const
165 return QStringLiteral(
"reclassifybylayer" );
168QString QgsReclassifyByLayerAlgorithm::displayName()
const
170 return QObject::tr(
"Reclassify by layer" );
173QStringList QgsReclassifyByLayerAlgorithm::tags()
const
175 return QObject::tr(
"raster,reclassify,classes,calculator" ).split(
',' );
178QString QgsReclassifyByLayerAlgorithm::shortHelpString()
const
180 return QObject::tr(
"This algorithm reclassifies a raster band by assigning new class values based on the ranges specified in a vector table." );
183QgsReclassifyByLayerAlgorithm *QgsReclassifyByLayerAlgorithm::createInstance()
const
185 return new QgsReclassifyByLayerAlgorithm();
188void QgsReclassifyByLayerAlgorithm::addAlgorithmParams()
202 std::unique_ptr< QgsFeatureSource >tableSource( parameterAsSource( parameters, QStringLiteral(
"INPUT_TABLE" ), context ) );
206 const QString fieldMin = parameterAsString( parameters, QStringLiteral(
"MIN_FIELD" ), context );
207 mMinFieldIdx = tableSource->fields().lookupField( fieldMin );
208 if ( mMinFieldIdx < 0 )
209 throw QgsProcessingException( QObject::tr(
"Invalid field specified for MIN_FIELD: %1" ).arg( fieldMin ) );
210 const QString fieldMax = parameterAsString( parameters, QStringLiteral(
"MAX_FIELD" ), context );
211 mMaxFieldIdx = tableSource->fields().lookupField( fieldMax );
212 if ( mMaxFieldIdx < 0 )
213 throw QgsProcessingException( QObject::tr(
"Invalid field specified for MAX_FIELD: %1" ).arg( fieldMax ) );
214 const QString fieldValue = parameterAsString( parameters, QStringLiteral(
"VALUE_FIELD" ), context );
215 mValueFieldIdx = tableSource->fields().lookupField( fieldValue );
216 if ( mValueFieldIdx < 0 )
217 throw QgsProcessingException( QObject::tr(
"Invalid field specified for VALUE_FIELD: %1" ).arg( fieldValue ) );
222 mTableIterator = tableSource->getFeatures( request );
229 QVector< QgsReclassifyUtils::RasterClass > classes;
231 while ( mTableIterator.nextFeature( f ) )
236 const QVariant minVariant = f.
attribute( mMinFieldIdx );
240 minValue = std::numeric_limits<double>::quiet_NaN();
244 minValue = minVariant.toDouble( &ok );
246 throw QgsProcessingException( QObject::tr(
"Invalid value for minimum: %1" ).arg( minVariant.toString() ) );
248 const QVariant maxVariant = f.
attribute( mMaxFieldIdx );
252 maxValue = std::numeric_limits<double>::quiet_NaN();
257 maxValue = maxVariant.toDouble( &ok );
259 throw QgsProcessingException( QObject::tr(
"Invalid value for maximum: %1" ).arg( maxVariant.toString() ) );
262 const double value = f.
attribute( mValueFieldIdx ).toDouble( &ok );
266 classes << QgsReclassifyUtils::RasterClass( minValue, maxValue, boundsType, value );
276QString QgsReclassifyByTableAlgorithm::name()
const
278 return QStringLiteral(
"reclassifybytable" );
281QString QgsReclassifyByTableAlgorithm::displayName()
const
283 return QObject::tr(
"Reclassify by table" );
286QStringList QgsReclassifyByTableAlgorithm::tags()
const
288 return QObject::tr(
"raster,reclassify,classes,calculator" ).split(
',' );
291QString QgsReclassifyByTableAlgorithm::shortHelpString()
const
293 return QObject::tr(
"This algorithm reclassifies a raster band by assigning new class values based on the ranges specified in a fixed table." );
296QgsReclassifyByTableAlgorithm *QgsReclassifyByTableAlgorithm::createInstance()
const
298 return new QgsReclassifyByTableAlgorithm();
301void QgsReclassifyByTableAlgorithm::addAlgorithmParams()
304 QObject::tr(
"Reclassification table" ),
305 1,
false, QStringList() << QObject::tr(
"Minimum" )
306 << QObject::tr(
"Maximum" )
307 << QObject::tr(
"Value" ) ) );
315QVector<QgsReclassifyUtils::RasterClass> QgsReclassifyByTableAlgorithm::createClasses( QgsReclassifyUtils::RasterClass::BoundsType boundsType,
const QVariantMap ¶meters,
QgsProcessingContext &context,
QgsProcessingFeedback * )
317 const QVariantList table = parameterAsMatrix( parameters, QStringLiteral(
"TABLE" ), context );
318 if ( table.count() % 3 != 0 )
319 throw QgsProcessingException( QObject::tr(
"Invalid value for TABLE: list must contain a multiple of 3 elements (found %1)" ).arg( table.count() ) );
321 const int rows = table.count() / 3;
322 QVector< QgsReclassifyUtils::RasterClass > classes;
323 classes.reserve( rows );
324 for (
int row = 0; row < rows; ++row )
329 const QVariant minVariant = table.at( row * 3 );
333 minValue = std::numeric_limits<double>::quiet_NaN();
337 minValue = minVariant.toDouble( &ok );
339 throw QgsProcessingException( QObject::tr(
"Invalid value for minimum: %1" ).arg( table.at( row * 3 ).toString() ) );
341 const QVariant maxVariant = table.at( row * 3 + 1 );
345 maxValue = std::numeric_limits<double>::quiet_NaN();
350 maxValue = maxVariant.toDouble( &ok );
352 throw QgsProcessingException( QObject::tr(
"Invalid value for maximum: %1" ).arg( table.at( row * 3 + 1 ).toString() ) );
355 const double value = table.at( row * 3 + 2 ).toDouble( &ok );
357 throw QgsProcessingException( QObject::tr(
"Invalid output value: %1" ).arg( table.at( row * 3 + 2 ).toString() ) );
359 classes << QgsReclassifyUtils::RasterClass( minValue, maxValue, boundsType, value );
@ Float32
Thirty two bit floating point (float)
@ Int8
Eight bit signed integer (qint8) (added in QGIS 3.30)
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
virtual 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.
A raster band parameter for Processing algorithms.
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
@ Numeric
Accepts numeric fields.
A table (matrix) parameter for processing algorithms.
@ Double
Double/float values.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
A raster layer parameter for processing algorithms.
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
QgsRasterDataProvider * clone() const override=0
Clone itself, create deep copy.
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
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.
BoundsType
Handling for min and max bounds.
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.
QList< int > QgsAttributeList