29QString QgsRasterCalculatorAlgorithm::name()
const
31 return QStringLiteral(
"rastercalc" );
34QString QgsRasterCalculatorAlgorithm::displayName()
const
36 return QObject::tr(
"Raster calculator" );
39QStringList QgsRasterCalculatorAlgorithm::tags()
const
41 return QObject::tr(
"raster,calculator" ).split(
',' );
44QString QgsRasterCalculatorAlgorithm::group()
const
46 return QObject::tr(
"Raster analysis" );
49QString QgsRasterCalculatorAlgorithm::groupId()
const
51 return QStringLiteral(
"rasteranalysis" );
54QString QgsRasterCalculatorAlgorithm::shortHelpString()
const
56 return QObject::tr(
"Performing algebraic operations using raster layers." );
59QgsRasterCalculatorAlgorithm *QgsRasterCalculatorAlgorithm::createInstance()
const
61 return new QgsRasterCalculatorAlgorithm();
64void QgsRasterCalculatorAlgorithm::initAlgorithm(
const QVariantMap & )
68 std::unique_ptr<QgsProcessingParameterExtent> extentParam = std::make_unique<QgsProcessingParameterExtent>( QStringLiteral(
"EXTENT" ), QObject::tr(
"Output extent" ), QVariant(),
true );
69 extentParam->setHelp( QObject::tr(
"Extent of the output layer. If not specified, the extent will be the overall extent of all input layers" ) );
70 addParameter( extentParam.release() );
71 std::unique_ptr<QgsProcessingParameterNumber> cellSizeParam = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral(
"CELL_SIZE" ), QObject::tr(
"Output cell size (leave empty to set automatically)" ),
Qgis::ProcessingNumberParameterType::Double, QVariant(),
true, 0.0 );
72 cellSizeParam->setHelp( QObject::tr(
"Cell size of the output layer. If not specified, the smallest cell size from the input layers will be used" ) );
73 addParameter( cellSizeParam.release() );
74 std::unique_ptr<QgsProcessingParameterCrs> crsParam = std::make_unique<QgsProcessingParameterCrs>( QStringLiteral(
"CRS" ), QObject::tr(
"Output CRS" ), QVariant(),
true );
75 crsParam->setHelp( QObject::tr(
"CRS of the output layer. If not specified, the CRS of the first input layer will be used" ) );
76 addParameter( crsParam.release() );
82 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
84 for (
const QgsMapLayer *layer : std::as_const( layers ) )
87 clonedLayer->moveToThread(
nullptr );
88 mLayers << clonedLayer;
91 if ( mLayers.isEmpty() )
93 feedback->
reportError( QObject::tr(
"No layers selected" ),
false );
103 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
105 layer->moveToThread( QThread::currentThread() );
109 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
111 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
115 crs = mLayers.at( 0 )->crs();
119 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
121 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context,
crs );
128 double minCellSize = 1e9;
130 QVector<QgsRasterCalculatorEntry> entries;
140 for (
int i = 0; i < nBands; ++i )
143 entry.
ref = QStringLiteral(
"%1@%2" ).arg( rLayer->
name() ).arg( i + 1 );
150 if ( rLayer->
crs() !=
crs )
153 ext = ct.transformBoundingBox( ext );
157 if ( cellSize < minCellSize )
159 minCellSize = cellSize;
163 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
166 cellSize = minCellSize;
169 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
170 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
171 const QFileInfo fi( outputFile );
174 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
175 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
179 qDeleteAll( mLayers );
200 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
209QString QgsRasterCalculatorModelerAlgorithm::name()
const
211 return QStringLiteral(
"modelerrastercalc" );
214QString QgsRasterCalculatorModelerAlgorithm::displayName()
const
216 return QObject::tr(
"Raster calculator" );
219QStringList QgsRasterCalculatorModelerAlgorithm::tags()
const
221 return QObject::tr(
"raster,calculator" ).split(
',' );
224QString QgsRasterCalculatorModelerAlgorithm::group()
const
226 return QObject::tr(
"Raster analysis" );
229QString QgsRasterCalculatorModelerAlgorithm::groupId()
const
231 return QStringLiteral(
"rasteranalysis" );
234QgsRasterCalculatorModelerAlgorithm *QgsRasterCalculatorModelerAlgorithm::createInstance()
const
236 return new QgsRasterCalculatorModelerAlgorithm();
241 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
243 layer->moveToThread( QThread::currentThread() );
247 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
249 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
253 crs = mLayers.at( 0 )->crs();
257 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
259 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context,
crs );
266 double minCellSize = 1e9;
268 QVector<QgsRasterCalculatorEntry> entries;
280 for (
int i = 0; i < nBands; ++i )
283 entry.
ref = QStringLiteral(
"%1@%2" ).arg( indexToName( n ) ).arg( i + 1 );
290 if ( rLayer->
crs() !=
crs )
293 ext = ct.transformBoundingBox( ext );
297 if ( cellSize < minCellSize )
299 minCellSize = cellSize;
303 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
306 cellSize = minCellSize;
309 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
310 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
311 const QFileInfo fi( outputFile );
314 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
315 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
319 qDeleteAll( mLayers );
340 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
344QString QgsRasterCalculatorModelerAlgorithm::indexToName(
int index )
const
352 mod = ( div - 1 ) % 26;
353 name =
static_cast<char>( 65 + mod ) + name;
354 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.
@ Double
Double/float values.
This class 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 QgsMapLayer * clone() const =0
Returns a new instance equivalent to this one except for the id which is still unique.
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.
@ ParserError
Error parsing formula.
@ CalculationError
Error occurred while performing calculation.
@ MemoryError
Error allocating memory for result.
@ BandError
Invalid band number for input.
@ CreateOutputError
Error creating output data file.
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
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.
const QgsCoordinateReferenceSystem & crs