35QString QgsReclassifyAlgorithmBase::group()
const
37 return QObject::tr(
"Raster analysis" );
40QString QgsReclassifyAlgorithmBase::groupId()
const
42 return QStringLiteral(
"rasteranalysis" );
45void QgsReclassifyAlgorithmBase::initAlgorithm(
const QVariantMap & )
48 addParameter(
new QgsProcessingParameterBand( QStringLiteral(
"RASTER_BAND" ), QObject::tr(
"Band number" ), 1, QStringLiteral(
"INPUT_RASTER" ) ) );
54 addParameter( noDataValueParam.release() );
56 auto boundsHandling = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral(
"RANGE_BOUNDARIES" ), QObject::tr(
"Range boundaries" ), QStringList() << QObject::tr(
"min < value <= max" ) << QObject::tr(
"min <= value < max" ) << QObject::tr(
"min <= value <= max" ) << QObject::tr(
"min < value < max" ),
false, 0 );
58 addParameter( boundsHandling.release() );
60 auto missingValuesParam = std::make_unique<QgsProcessingParameterBoolean>( QStringLiteral(
"NODATA_FOR_MISSING" ), QObject::tr(
"Use NoData when no range matches value" ),
false,
false );
62 addParameter( missingValuesParam.release() );
64 std::unique_ptr<QgsProcessingParameterDefinition> typeChoice = QgsRasterAnalysisUtils::createRasterTypeParameter( QStringLiteral(
"DATA_TYPE" ), QObject::tr(
"Output data type" ),
Qgis::DataType::Float32 );
66 addParameter( typeChoice.release() );
70 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATE_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
71 createOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
73 addParameter( createOptsParam.release() );
75 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATION_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
76 creationOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
78 addParameter( creationOptsParam.release() );
85 mDataType = QgsRasterAnalysisUtils::rasterTypeChoiceToDataType( parameterAsEnum( parameters, QStringLiteral(
"DATA_TYPE" ), context ) );
86 if ( mDataType ==
Qgis::DataType::Int8 && atoi( GDALVersionInfo(
"VERSION_NUM" ) ) < GDAL_COMPUTE_VERSION( 3, 7, 0 ) )
89 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT_RASTER" ), context );
94 mBand = parameterAsInt( parameters, QStringLiteral(
"RASTER_BAND" ), context );
95 if ( mBand < 1 || mBand > layer->
bandCount() )
96 throw QgsProcessingException( QObject::tr(
"Invalid band number for RASTER_BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( layer->
bandCount() ) );
103 mNbCellsXProvider = mInterface->xSize();
104 mNbCellsYProvider = mInterface->ySize();
106 mNoDataValue = parameterAsDouble( parameters, QStringLiteral(
"NO_DATA" ), context );
107 mUseNoDataForMissingValues = parameterAsBoolean( parameters, QStringLiteral(
"NODATA_FOR_MISSING" ), context );
109 const int boundsType = parameterAsEnum( parameters, QStringLiteral(
"RANGE_BOUNDARIES" ), context );
110 switch ( boundsType )
113 mBoundsType = QgsReclassifyUtils::RasterClass::IncludeMax;
117 mBoundsType = QgsReclassifyUtils::RasterClass::IncludeMin;
121 mBoundsType = QgsReclassifyUtils::RasterClass::IncludeMinAndMax;
125 mBoundsType = QgsReclassifyUtils::RasterClass::Exclusive;
129 return _prepareAlgorithm( parameters, context, feedback );
134 const QVector<QgsReclassifyUtils::RasterClass> classes = createClasses( mBoundsType, parameters, context, feedback );
136 QgsReclassifyUtils::reportClasses( classes, feedback );
137 QgsReclassifyUtils::checkForOverlaps( classes, feedback );
139 QString creationOptions = parameterAsString( parameters, QStringLiteral(
"CREATION_OPTIONS" ), context ).trimmed();
141 const QString optionsString = parameterAsString( parameters, QStringLiteral(
"CREATE_OPTIONS" ), context );
142 if ( !optionsString.isEmpty() )
143 creationOptions = optionsString;
145 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
146 const QString outputFormat = parameterAsOutputRasterFormat( parameters, QStringLiteral(
"OUTPUT" ), context );
148 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
149 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
150 if ( !creationOptions.isEmpty() )
152 writer->setCreationOptions( creationOptions.split(
'|' ) );
155 writer->setOutputFormat( outputFormat );
156 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( mDataType, mNbCellsXProvider, mNbCellsYProvider, mExtent, mCrs ) );
159 if ( !provider->isValid() )
162 provider->setNoDataValue( 1, mNoDataValue );
164 QgsReclassifyUtils::reclassify( classes, mInterface.get(), mBand, mExtent, mNbCellsXProvider, mNbCellsYProvider, provider.get(), mNoDataValue, mUseNoDataForMissingValues, feedback );
167 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
176QString QgsReclassifyByLayerAlgorithm::name()
const
178 return QStringLiteral(
"reclassifybylayer" );
181QString QgsReclassifyByLayerAlgorithm::displayName()
const
183 return QObject::tr(
"Reclassify by layer" );
186QStringList QgsReclassifyByLayerAlgorithm::tags()
const
188 return QObject::tr(
"raster,reclassify,classes,calculator" ).split(
',' );
191QString QgsReclassifyByLayerAlgorithm::shortHelpString()
const
193 return QObject::tr(
"This algorithm reclassifies a raster band by assigning new class values based on the ranges specified in a vector table." );
196QString QgsReclassifyByLayerAlgorithm::shortDescription()
const
198 return QObject::tr(
"Reclassifies a raster band by assigning new class values based on the ranges specified in a vector table." );
201QgsReclassifyByLayerAlgorithm *QgsReclassifyByLayerAlgorithm::createInstance()
const
203 return new QgsReclassifyByLayerAlgorithm();
206void QgsReclassifyByLayerAlgorithm::addAlgorithmParams()
216 std::unique_ptr<QgsFeatureSource> tableSource( parameterAsSource( parameters, QStringLiteral(
"INPUT_TABLE" ), context ) );
220 const QString fieldMin = parameterAsString( parameters, QStringLiteral(
"MIN_FIELD" ), context );
221 mMinFieldIdx = tableSource->fields().lookupField( fieldMin );
222 if ( mMinFieldIdx < 0 )
223 throw QgsProcessingException( QObject::tr(
"Invalid field specified for MIN_FIELD: %1" ).arg( fieldMin ) );
224 const QString fieldMax = parameterAsString( parameters, QStringLiteral(
"MAX_FIELD" ), context );
225 mMaxFieldIdx = tableSource->fields().lookupField( fieldMax );
226 if ( mMaxFieldIdx < 0 )
227 throw QgsProcessingException( QObject::tr(
"Invalid field specified for MAX_FIELD: %1" ).arg( fieldMax ) );
228 const QString fieldValue = parameterAsString( parameters, QStringLiteral(
"VALUE_FIELD" ), context );
229 mValueFieldIdx = tableSource->fields().lookupField( fieldValue );
230 if ( mValueFieldIdx < 0 )
231 throw QgsProcessingException( QObject::tr(
"Invalid field specified for VALUE_FIELD: %1" ).arg( fieldValue ) );
236 mTableIterator = tableSource->getFeatures( request );
243 QVector<QgsReclassifyUtils::RasterClass> classes;
245 while ( mTableIterator.nextFeature( f ) )
250 const QVariant minVariant = f.
attribute( mMinFieldIdx );
254 minValue = std::numeric_limits<double>::quiet_NaN();
258 minValue = minVariant.toDouble( &ok );
260 throw QgsProcessingException( QObject::tr(
"Invalid value for minimum: %1" ).arg( minVariant.toString() ) );
262 const QVariant maxVariant = f.
attribute( mMaxFieldIdx );
266 maxValue = std::numeric_limits<double>::quiet_NaN();
271 maxValue = maxVariant.toDouble( &ok );
273 throw QgsProcessingException( QObject::tr(
"Invalid value for maximum: %1" ).arg( maxVariant.toString() ) );
276 const double value = f.
attribute( mValueFieldIdx ).toDouble( &ok );
280 classes << QgsReclassifyUtils::RasterClass( minValue, maxValue, boundsType, value );
290QString QgsReclassifyByTableAlgorithm::name()
const
292 return QStringLiteral(
"reclassifybytable" );
295QString QgsReclassifyByTableAlgorithm::displayName()
const
297 return QObject::tr(
"Reclassify by table" );
300QStringList QgsReclassifyByTableAlgorithm::tags()
const
302 return QObject::tr(
"raster,reclassify,classes,calculator" ).split(
',' );
305QString QgsReclassifyByTableAlgorithm::shortHelpString()
const
307 return QObject::tr(
"This algorithm reclassifies a raster band by assigning new class values based on the ranges specified in a fixed table." );
310QString QgsReclassifyByTableAlgorithm::shortDescription()
const
312 return QObject::tr(
"Reclassifies a raster band by assigning new class values based on the ranges specified in a fixed table." );
315QgsReclassifyByTableAlgorithm *QgsReclassifyByTableAlgorithm::createInstance()
const
317 return new QgsReclassifyByTableAlgorithm();
320void QgsReclassifyByTableAlgorithm::addAlgorithmParams()
322 addParameter(
new QgsProcessingParameterMatrix( QStringLiteral(
"TABLE" ), QObject::tr(
"Reclassification table" ), 1,
false, QStringList() << QObject::tr(
"Minimum" ) << QObject::tr(
"Maximum" ) << QObject::tr(
"Value" ) ) );
330QVector<QgsReclassifyUtils::RasterClass> QgsReclassifyByTableAlgorithm::createClasses( QgsReclassifyUtils::RasterClass::BoundsType boundsType,
const QVariantMap ¶meters,
QgsProcessingContext &context,
QgsProcessingFeedback * )
332 const QVariantList table = parameterAsMatrix( parameters, QStringLiteral(
"TABLE" ), context );
333 if ( table.count() % 3 != 0 )
334 throw QgsProcessingException( QObject::tr(
"Invalid value for TABLE: list must contain a multiple of 3 elements (found %1)" ).arg( table.count() ) );
336 const int rows = table.count() / 3;
337 QVector<QgsReclassifyUtils::RasterClass> classes;
338 classes.reserve( rows );
339 for (
int row = 0; row < rows; ++row )
344 const QVariant minVariant = table.at( row * 3 );
348 minValue = std::numeric_limits<double>::quiet_NaN();
352 minValue = minVariant.toDouble( &ok );
354 throw QgsProcessingException( QObject::tr(
"Invalid value for minimum: %1" ).arg( table.at( row * 3 ).toString() ) );
356 const QVariant maxVariant = table.at( row * 3 + 1 );
360 maxValue = std::numeric_limits<double>::quiet_NaN();
365 maxValue = maxVariant.toDouble( &ok );
367 throw QgsProcessingException( QObject::tr(
"Invalid value for maximum: %1" ).arg( table.at( row * 3 + 1 ).toString() ) );
370 const double value = table.at( row * 3 + 2 ).toDouble( &ok );
372 throw QgsProcessingException( QObject::tr(
"Invalid output value: %1" ).arg( table.at( row * 3 + 2 ).toString() ) );
374 classes << QgsReclassifyUtils::RasterClass( minValue, maxValue, boundsType, value );
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
@ Numeric
Accepts numeric fields.
@ Float32
Thirty two bit floating point (float).
@ Int8
Eight bit signed integer (qint8) (added in QGIS 3.30).
@ Hidden
Parameter is hidden and should not be shown to users.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ Double
Double/float values.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Q_INVOKABLE 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.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A table (matrix) parameter for processing algorithms.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
A raster layer parameter for processing algorithms.
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.
BoundsType
Handling for min and max bounds.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
QList< int > QgsAttributeList