30QString QgsRasterCalculatorAlgorithm::name()
const
32 return QStringLiteral(
"rastercalc" );
35QString QgsRasterCalculatorAlgorithm::displayName()
const
37 return QObject::tr(
"Raster calculator" );
40QStringList QgsRasterCalculatorAlgorithm::tags()
const
42 return QObject::tr(
"raster,calculator" ).split(
',' );
45QString QgsRasterCalculatorAlgorithm::group()
const
47 return QObject::tr(
"Raster analysis" );
50QString QgsRasterCalculatorAlgorithm::groupId()
const
52 return QStringLiteral(
"rasteranalysis" );
55QString QgsRasterCalculatorAlgorithm::shortHelpString()
const
57 return QObject::tr(
"This algorithm performs algebraic operations using raster layers." );
60QString QgsRasterCalculatorAlgorithm::shortDescription()
const
62 return QObject::tr(
"Performs algebraic operations using raster layers." );
65QgsRasterCalculatorAlgorithm *QgsRasterCalculatorAlgorithm::createInstance()
const
67 return new QgsRasterCalculatorAlgorithm();
70void QgsRasterCalculatorAlgorithm::initAlgorithm(
const QVariantMap & )
74 auto extentParam = std::make_unique<QgsProcessingParameterExtent>( QStringLiteral(
"EXTENT" ), QObject::tr(
"Output extent" ), QVariant(),
true );
75 extentParam->setHelp( QObject::tr(
"Extent of the output layer. If not specified, the extent will be the overall extent of all input layers" ) );
76 addParameter( extentParam.release() );
77 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 );
78 cellSizeParam->setHelp( QObject::tr(
"Cell size of the output layer. If not specified, the smallest cell size from the input layers will be used" ) );
79 addParameter( cellSizeParam.release() );
80 auto crsParam = std::make_unique<QgsProcessingParameterCrs>( QStringLiteral(
"CRS" ), QObject::tr(
"Output CRS" ), QVariant(),
true );
81 crsParam->setHelp( QObject::tr(
"CRS of the output layer. If not specified, the CRS of the first input layer will be used" ) );
82 addParameter( crsParam.release() );
84 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATION_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
85 creationOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
87 addParameter( creationOptsParam.release() );
94 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
96 for (
const QgsMapLayer *layer : std::as_const( layers ) )
99 clonedLayer->moveToThread(
nullptr );
100 mLayers << clonedLayer;
103 if ( mLayers.isEmpty() )
105 feedback->
reportError( QObject::tr(
"No layers selected" ),
false );
114 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
116 layer->moveToThread( QThread::currentThread() );
120 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
122 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
126 crs = mLayers.at( 0 )->crs();
130 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
132 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context, crs );
139 double minCellSize = 1e9;
141 QVector<QgsRasterCalculatorEntry> entries;
151 for (
int i = 0; i < nBands; ++i )
154 entry.
ref = QStringLiteral(
"%1@%2" ).arg( rLayer->
name() ).arg( i + 1 );
161 if ( rLayer->
crs() != crs )
164 ext = ct.transformBoundingBox( ext );
168 if ( cellSize < minCellSize )
170 minCellSize = cellSize;
174 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
177 cellSize = minCellSize;
180 const QString creationOptions = parameterAsString( parameters, QStringLiteral(
"CREATION_OPTIONS" ), context ).trimmed();
181 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
182 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
183 const QFileInfo fi( outputFile );
186 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
187 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
190 calc.setCreationOptions( creationOptions.split(
'|', Qt::SplitBehaviorFlags::SkipEmptyParts ) );
192 qDeleteAll( mLayers );
213 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
222QString QgsRasterCalculatorModelerAlgorithm::name()
const
224 return QStringLiteral(
"modelerrastercalc" );
227QString QgsRasterCalculatorModelerAlgorithm::displayName()
const
229 return QObject::tr(
"Raster calculator" );
232QStringList QgsRasterCalculatorModelerAlgorithm::tags()
const
234 return QObject::tr(
"raster,calculator" ).split(
',' );
237QString QgsRasterCalculatorModelerAlgorithm::group()
const
239 return QObject::tr(
"Raster analysis" );
242QString QgsRasterCalculatorModelerAlgorithm::groupId()
const
244 return QStringLiteral(
"rasteranalysis" );
247QgsRasterCalculatorModelerAlgorithm *QgsRasterCalculatorModelerAlgorithm::createInstance()
const
249 return new QgsRasterCalculatorModelerAlgorithm();
254 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
256 layer->moveToThread( QThread::currentThread() );
260 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
262 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
266 crs = mLayers.at( 0 )->crs();
270 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
272 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context, crs );
279 double minCellSize = 1e9;
281 QVector<QgsRasterCalculatorEntry> entries;
293 for (
int i = 0; i < nBands; ++i )
296 entry.
ref = QStringLiteral(
"%1@%2" ).arg( indexToName( n ) ).arg( i + 1 );
303 if ( rLayer->
crs() != crs )
306 ext = ct.transformBoundingBox( ext );
310 if ( cellSize < minCellSize )
312 minCellSize = cellSize;
316 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
319 cellSize = minCellSize;
322 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
323 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
324 const QFileInfo fi( outputFile );
327 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
328 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
332 qDeleteAll( mLayers );
353 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
357QString QgsRasterCalculatorModelerAlgorithm::indexToName(
int index )
const
365 mod = ( div - 1 ) % 26;
366 name =
static_cast<char>( 65 + mod ) + name;
367 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.
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.