25using namespace Qt::StringLiterals;
34QString QgsRasterCalculatorAlgorithm::name()
const
36 return u
"rastercalc"_s;
39QString QgsRasterCalculatorAlgorithm::displayName()
const
41 return QObject::tr(
"Raster calculator" );
44QStringList QgsRasterCalculatorAlgorithm::tags()
const
46 return QObject::tr(
"raster,calculator" ).split(
',' );
49QString QgsRasterCalculatorAlgorithm::group()
const
51 return QObject::tr(
"Raster analysis" );
54QString QgsRasterCalculatorAlgorithm::groupId()
const
56 return u
"rasteranalysis"_s;
59QString QgsRasterCalculatorAlgorithm::shortHelpString()
const
61 return QObject::tr(
"This algorithm performs algebraic operations using raster layers." );
64QString QgsRasterCalculatorAlgorithm::shortDescription()
const
66 return QObject::tr(
"Performs algebraic operations using raster layers." );
69QgsRasterCalculatorAlgorithm *QgsRasterCalculatorAlgorithm::createInstance()
const
71 return new QgsRasterCalculatorAlgorithm();
74void QgsRasterCalculatorAlgorithm::initAlgorithm(
const QVariantMap & )
78 auto extentParam = std::make_unique<QgsProcessingParameterExtent>( u
"EXTENT"_s, QObject::tr(
"Output extent" ), QVariant(),
true );
79 extentParam->setHelp( QObject::tr(
"Extent of the output layer. If not specified, the extent will be the overall extent of all input layers" ) );
80 addParameter( extentParam.release() );
81 auto cellSizeParam = std::make_unique<QgsProcessingParameterNumber>( u
"CELL_SIZE"_s, QObject::tr(
"Output cell size (leave empty to set automatically)" ),
Qgis::ProcessingNumberParameterType::Double, QVariant(),
true, 0.0 );
82 cellSizeParam->setHelp( QObject::tr(
"Cell size of the output layer. If not specified, the smallest cell size from the input layers will be used" ) );
83 addParameter( cellSizeParam.release() );
84 auto crsParam = std::make_unique<QgsProcessingParameterCrs>( u
"CRS"_s, QObject::tr(
"Output CRS" ), QVariant(),
true );
85 crsParam->setHelp( QObject::tr(
"CRS of the output layer. If not specified, the CRS of the first input layer will be used" ) );
86 addParameter( crsParam.release() );
88 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( u
"CREATION_OPTIONS"_s, QObject::tr(
"Creation options" ), QVariant(),
false,
true );
89 creationOptsParam->setMetadata( QVariantMap( { { u
"widget_wrapper"_s, QVariantMap( { { u
"widget_type"_s, u
"rasteroptions"_s } } ) } } ) );
91 addParameter( creationOptsParam.release() );
98 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u
"LAYERS"_s, context );
100 for (
const QgsMapLayer *layer : std::as_const( layers ) )
103 clonedLayer->moveToThread(
nullptr );
104 mLayers << clonedLayer;
107 if ( mLayers.isEmpty() )
109 feedback->
reportError( QObject::tr(
"No layers selected" ),
false );
118 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
120 layer->moveToThread( QThread::currentThread() );
124 if ( parameters.value( u
"CRS"_s ).isValid() )
126 crs = parameterAsCrs( parameters, u
"CRS"_s, context );
130 crs = mLayers.at( 0 )->crs();
134 if ( parameters.value( u
"EXTENT"_s ).isValid() )
136 bbox = parameterAsExtent( parameters, u
"EXTENT"_s, context, crs );
143 double minCellSize = 1e9;
145 QVector<QgsRasterCalculatorEntry> entries;
155 for (
int i = 0; i < nBands; ++i )
158 entry.
ref = u
"%1@%2"_s.arg( rLayer->
name() ).arg( i + 1 );
165 if ( rLayer->
crs() != crs )
168 ext = ct.transformBoundingBox( ext );
172 if ( cellSize < minCellSize )
174 minCellSize = cellSize;
178 double cellSize = parameterAsDouble( parameters, u
"CELL_SIZE"_s, context );
181 cellSize = minCellSize;
184 const QString creationOptions = parameterAsString( parameters, u
"CREATION_OPTIONS"_s, context ).trimmed();
185 const QString expression = parameterAsExpression( parameters, u
"EXPRESSION"_s, context );
186 const QString outputFile = parameterAsOutputLayer( parameters, u
"OUTPUT"_s, context );
187 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u
"OUTPUT"_s, context );
189 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
190 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
193 calc.setCreationOptions( creationOptions.split(
'|', Qt::SplitBehaviorFlags::SkipEmptyParts ) );
195 qDeleteAll( mLayers );
216 outputs.insert( u
"OUTPUT"_s, outputFile );
225QString QgsRasterCalculatorModelerAlgorithm::name()
const
227 return u
"modelerrastercalc"_s;
230QString QgsRasterCalculatorModelerAlgorithm::displayName()
const
232 return QObject::tr(
"Raster calculator" );
235QStringList QgsRasterCalculatorModelerAlgorithm::tags()
const
237 return QObject::tr(
"raster,calculator" ).split(
',' );
240QString QgsRasterCalculatorModelerAlgorithm::group()
const
242 return QObject::tr(
"Raster analysis" );
245QString QgsRasterCalculatorModelerAlgorithm::groupId()
const
247 return u
"rasteranalysis"_s;
250QgsRasterCalculatorModelerAlgorithm *QgsRasterCalculatorModelerAlgorithm::createInstance()
const
252 return new QgsRasterCalculatorModelerAlgorithm();
257 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
259 layer->moveToThread( QThread::currentThread() );
263 if ( parameters.value( u
"CRS"_s ).isValid() )
265 crs = parameterAsCrs( parameters, u
"CRS"_s, context );
269 crs = mLayers.at( 0 )->crs();
273 if ( parameters.value( u
"EXTENT"_s ).isValid() )
275 bbox = parameterAsExtent( parameters, u
"EXTENT"_s, context, crs );
282 double minCellSize = 1e9;
284 QVector<QgsRasterCalculatorEntry> entries;
296 for (
int i = 0; i < nBands; ++i )
299 entry.
ref = u
"%1@%2"_s.arg( indexToName( n ) ).arg( i + 1 );
306 if ( rLayer->
crs() != crs )
309 ext = ct.transformBoundingBox( ext );
313 if ( cellSize < minCellSize )
315 minCellSize = cellSize;
319 double cellSize = parameterAsDouble( parameters, u
"CELL_SIZE"_s, context );
322 cellSize = minCellSize;
325 const QString expression = parameterAsExpression( parameters, u
"EXPRESSION"_s, context );
326 const QString outputFile = parameterAsOutputLayer( parameters, u
"OUTPUT"_s, context );
327 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u
"OUTPUT"_s, context );
329 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
330 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
334 qDeleteAll( mLayers );
355 outputs.insert( u
"OUTPUT"_s, outputFile );
359QString QgsRasterCalculatorModelerAlgorithm::indexToName(
int index )
const
367 mod = ( div - 1 ) % 26;
368 name =
static_cast<char>( 65 + mod ) + name;
369 div = ( int ) ( ( div - mod ) / 26 );
@ RasterCalculator
Raster calculator expression.
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
@ HideFromModeler
Algorithm should be hidden from the modeler.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ Double
Double/float values.
Represents a coordinate reference system (CRS).
Base class for all map layer types.
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
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.
An expression parameter for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
static QgsRectangle combineLayerExtents(const QList< QgsMapLayer * > &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context)
Combines the extent of several map layers.
Represents an individual raster layer/band number entry within a raster calculation.
QgsRasterLayer * raster
Raster layer associated with entry.
int bandNumber
Band number for entry.
QString ref
Name of entry.
Performs raster layer calculations.
Result
Result of the calculation.
@ InputLayerError
Error reading input layer.
@ BandError
Invalid band number for input.
@ CreateOutputError
Error creating output data file.
@ ParserError
Error parsing formula.
@ CalculationError
Error occurred while performing calculation.
@ MemoryError
Error allocating memory for result.
virtual int bandCount() const =0
Gets number of bands.
Represents a raster layer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
int width() const
Returns the width of the (unclipped) raster.
A rectangle specified with double values.