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(
"This algorithm performs algebraic operations using raster layers." );
59QString QgsRasterCalculatorAlgorithm::shortDescription()
const
61 return QObject::tr(
"Performs algebraic operations using raster layers." );
64QgsRasterCalculatorAlgorithm *QgsRasterCalculatorAlgorithm::createInstance()
const
66 return new QgsRasterCalculatorAlgorithm();
69void QgsRasterCalculatorAlgorithm::initAlgorithm(
const QVariantMap & )
73 auto extentParam = std::make_unique<QgsProcessingParameterExtent>( QStringLiteral(
"EXTENT" ), QObject::tr(
"Output extent" ), QVariant(),
true );
74 extentParam->setHelp( QObject::tr(
"Extent of the output layer. If not specified, the extent will be the overall extent of all input layers" ) );
75 addParameter( extentParam.release() );
76 auto 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 );
77 cellSizeParam->setHelp( QObject::tr(
"Cell size of the output layer. If not specified, the smallest cell size from the input layers will be used" ) );
78 addParameter( cellSizeParam.release() );
79 auto crsParam = std::make_unique<QgsProcessingParameterCrs>( QStringLiteral(
"CRS" ), QObject::tr(
"Output CRS" ), QVariant(),
true );
80 crsParam->setHelp( QObject::tr(
"CRS of the output layer. If not specified, the CRS of the first input layer will be used" ) );
81 addParameter( crsParam.release() );
83 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATION_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
84 creationOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
86 addParameter( creationOptsParam.release() );
93 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
95 for (
const QgsMapLayer *layer : std::as_const( layers ) )
98 clonedLayer->moveToThread(
nullptr );
99 mLayers << clonedLayer;
102 if ( mLayers.isEmpty() )
104 feedback->
reportError( QObject::tr(
"No layers selected" ),
false );
113 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
115 layer->moveToThread( QThread::currentThread() );
119 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
121 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
125 crs = mLayers.at( 0 )->crs();
129 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
131 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context,
crs );
138 double minCellSize = 1e9;
140 QVector<QgsRasterCalculatorEntry> entries;
150 for (
int i = 0; i < nBands; ++i )
153 entry.
ref = QStringLiteral(
"%1@%2" ).arg( rLayer->
name() ).arg( i + 1 );
160 if ( rLayer->
crs() !=
crs )
163 ext = ct.transformBoundingBox( ext );
167 if ( cellSize < minCellSize )
169 minCellSize = cellSize;
173 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
176 cellSize = minCellSize;
179 const QString creationOptions = parameterAsString( parameters, QStringLiteral(
"CREATION_OPTIONS" ), context ).trimmed();
180 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
181 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
182 const QFileInfo fi( outputFile );
185 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
186 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
189 calc.setCreationOptions( creationOptions.split(
'|', Qt::SplitBehaviorFlags::SkipEmptyParts ) );
191 qDeleteAll( mLayers );
212 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
221QString QgsRasterCalculatorModelerAlgorithm::name()
const
223 return QStringLiteral(
"modelerrastercalc" );
226QString QgsRasterCalculatorModelerAlgorithm::displayName()
const
228 return QObject::tr(
"Raster calculator" );
231QStringList QgsRasterCalculatorModelerAlgorithm::tags()
const
233 return QObject::tr(
"raster,calculator" ).split(
',' );
236QString QgsRasterCalculatorModelerAlgorithm::group()
const
238 return QObject::tr(
"Raster analysis" );
241QString QgsRasterCalculatorModelerAlgorithm::groupId()
const
243 return QStringLiteral(
"rasteranalysis" );
246QgsRasterCalculatorModelerAlgorithm *QgsRasterCalculatorModelerAlgorithm::createInstance()
const
248 return new QgsRasterCalculatorModelerAlgorithm();
253 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
255 layer->moveToThread( QThread::currentThread() );
259 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
261 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
265 crs = mLayers.at( 0 )->crs();
269 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
271 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context,
crs );
278 double minCellSize = 1e9;
280 QVector<QgsRasterCalculatorEntry> entries;
292 for (
int i = 0; i < nBands; ++i )
295 entry.
ref = QStringLiteral(
"%1@%2" ).arg( indexToName( n ) ).arg( i + 1 );
302 if ( rLayer->
crs() !=
crs )
305 ext = ct.transformBoundingBox( ext );
309 if ( cellSize < minCellSize )
311 minCellSize = cellSize;
315 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
318 cellSize = minCellSize;
321 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
322 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
323 const QFileInfo fi( outputFile );
326 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
327 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
331 qDeleteAll( mLayers );
352 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
356QString QgsRasterCalculatorModelerAlgorithm::indexToName(
int index )
const
364 mod = ( div - 1 ) % 26;
365 name =
static_cast<char>( 65 + mod ) + name;
366 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 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.
@ 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.
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