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 & )
69 std::unique_ptr<QgsProcessingParameterExtent> extentParam = std::make_unique<QgsProcessingParameterExtent>( QStringLiteral(
"EXTENT" ), QObject::tr(
"Output extent" ), QVariant(),
true );
70 extentParam->setHelp( QObject::tr(
"Extent of the output layer. If not specified, the extent will be the overall extent of all input layers" ) );
71 addParameter( extentParam.release() );
72 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 );
73 cellSizeParam->setHelp( QObject::tr(
"Cell size of the output layer. If not specified, the smallest cell size from the input layers will be used" ) );
74 addParameter( cellSizeParam.release() );
75 std::unique_ptr<QgsProcessingParameterCrs> crsParam = std::make_unique<QgsProcessingParameterCrs>( QStringLiteral(
"CRS" ), QObject::tr(
"Output CRS" ), QVariant(),
true );
76 crsParam->setHelp( QObject::tr(
"CRS of the output layer. If not specified, the CRS of the first input layer will be used" ) );
77 addParameter( crsParam.release() );
83 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
85 for (
const QgsMapLayer *layer : std::as_const( layers ) )
89 clonedLayer->moveToThread(
nullptr );
90 mLayers << clonedLayer;
93 if ( mLayers.isEmpty() )
95 feedback->
reportError( QObject::tr(
"No layers selected" ),
false );
105 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
107 layer->moveToThread( QThread::currentThread() );
111 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
113 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
117 crs = mLayers.at( 0 )->crs();
121 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
123 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context,
crs );
130 double minCellSize = 1e9;
132 QVector< QgsRasterCalculatorEntry > entries;
142 for (
int i = 0; i < nBands; ++i )
145 entry.
ref = QStringLiteral(
"%1@%2" ).arg( rLayer->
name() ).arg( i + 1 );
152 if ( rLayer->
crs() !=
crs )
155 ext = ct.transformBoundingBox( ext );
159 if ( cellSize < minCellSize )
161 minCellSize = cellSize;
165 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
168 cellSize = minCellSize;
171 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
172 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
173 const QFileInfo fi( outputFile );
176 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
177 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
181 qDeleteAll( mLayers );
202 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
211QString QgsRasterCalculatorModelerAlgorithm::name()
const
213 return QStringLiteral(
"modelerrastercalc" );
216QString QgsRasterCalculatorModelerAlgorithm::displayName()
const
218 return QObject::tr(
"Raster calculator" );
221QStringList QgsRasterCalculatorModelerAlgorithm::tags()
const
223 return QObject::tr(
"raster,calculator" ).split(
',' );
226QString QgsRasterCalculatorModelerAlgorithm::group()
const
228 return QObject::tr(
"Raster analysis" );
231QString QgsRasterCalculatorModelerAlgorithm::groupId()
const
233 return QStringLiteral(
"rasteranalysis" );
236QgsRasterCalculatorModelerAlgorithm *QgsRasterCalculatorModelerAlgorithm::createInstance()
const
238 return new QgsRasterCalculatorModelerAlgorithm();
243 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
245 layer->moveToThread( QThread::currentThread() );
249 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
251 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
255 crs = mLayers.at( 0 )->crs();
259 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
261 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context,
crs );
268 double minCellSize = 1e9;
270 QVector< QgsRasterCalculatorEntry > entries;
282 for (
int i = 0; i < nBands; ++i )
285 entry.
ref = QStringLiteral(
"%1@%2" ).arg( indexToName( n ) ).arg( i + 1 );
292 if ( rLayer->
crs() !=
crs )
295 ext = ct.transformBoundingBox( ext );
299 if ( cellSize < minCellSize )
301 minCellSize = cellSize;
305 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
308 cellSize = minCellSize;
311 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
312 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
313 const QFileInfo fi( outputFile );
316 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
317 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
321 qDeleteAll( mLayers );
342 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
346QString QgsRasterCalculatorModelerAlgorithm::indexToName(
int index )
const
354 mod = ( div - 1 ) % 26;
355 name =
static_cast<char>( 65 + mod ) + name;
356 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