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<
83 cellSizeParam->
setHelp( QObject::tr(
"Cell size of the output layer. If not specified, the smallest cell size from the input layers will be used" ) );
84 addParameter( cellSizeParam.release() );
85 auto crsParam = std::make_unique<QgsProcessingParameterCrs>( u
"CRS"_s, QObject::tr(
"Output CRS" ), QVariant(),
true );
86 crsParam->setHelp( QObject::tr(
"CRS of the output layer. If not specified, the CRS of the first input layer will be used" ) );
87 addParameter( crsParam.release() );
89 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( u
"CREATION_OPTIONS"_s, QObject::tr(
"Creation options" ), QVariant(),
false,
true );
90 creationOptsParam->setMetadata( QVariantMap( { { u
"widget_wrapper"_s, QVariantMap( { { u
"widget_type"_s, u
"rasteroptions"_s } } ) } } ) );
92 addParameter( creationOptsParam.release() );
99 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u
"LAYERS"_s, context );
101 for (
const QgsMapLayer *layer : std::as_const( layers ) )
104 clonedLayer->moveToThread(
nullptr );
105 mLayers << clonedLayer;
108 if ( mLayers.isEmpty() )
110 feedback->
reportError( QObject::tr(
"No layers selected" ),
false );
119 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
121 layer->moveToThread( QThread::currentThread() );
125 if ( parameters.value( u
"CRS"_s ).isValid() )
127 crs = parameterAsCrs( parameters, u
"CRS"_s, context );
131 crs = mLayers.at( 0 )->crs();
135 if ( parameters.value( u
"EXTENT"_s ).isValid() )
137 bbox = parameterAsExtent( parameters, u
"EXTENT"_s, context, crs );
144 double minCellSize = 1e9;
146 QVector<QgsRasterCalculatorEntry> entries;
156 for (
int i = 0; i < nBands; ++i )
159 entry.
ref = u
"%1@%2"_s.arg( rLayer->
name() ).arg( i + 1 );
166 if ( rLayer->
crs() != crs )
169 ext = ct.transformBoundingBox( ext );
173 if ( cellSize < minCellSize )
175 minCellSize = cellSize;
179 double cellSize = parameterAsDouble( parameters, u
"CELL_SIZE"_s, context );
182 cellSize = minCellSize;
185 const QString creationOptions = parameterAsString( parameters, u
"CREATION_OPTIONS"_s, context ).trimmed();
186 const QString expression = parameterAsExpression( parameters, u
"EXPRESSION"_s, context );
187 const QString outputFile = parameterAsOutputLayer( parameters, u
"OUTPUT"_s, context );
188 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u
"OUTPUT"_s, context );
190 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
191 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
194 calc.setCreationOptions( creationOptions.split(
'|', Qt::SplitBehaviorFlags::SkipEmptyParts ) );
196 qDeleteAll( mLayers );
217 outputs.insert( u
"OUTPUT"_s, outputFile );
226QString QgsRasterCalculatorModelerAlgorithm::name()
const
228 return u
"modelerrastercalc"_s;
231QString QgsRasterCalculatorModelerAlgorithm::displayName()
const
233 return QObject::tr(
"Raster calculator" );
236QStringList QgsRasterCalculatorModelerAlgorithm::tags()
const
238 return QObject::tr(
"raster,calculator" ).split(
',' );
241QString QgsRasterCalculatorModelerAlgorithm::group()
const
243 return QObject::tr(
"Raster analysis" );
246QString QgsRasterCalculatorModelerAlgorithm::groupId()
const
248 return u
"rasteranalysis"_s;
251QgsRasterCalculatorModelerAlgorithm *QgsRasterCalculatorModelerAlgorithm::createInstance()
const
253 return new QgsRasterCalculatorModelerAlgorithm();
258 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
260 layer->moveToThread( QThread::currentThread() );
264 if ( parameters.value( u
"CRS"_s ).isValid() )
266 crs = parameterAsCrs( parameters, u
"CRS"_s, context );
270 crs = mLayers.at( 0 )->crs();
274 if ( parameters.value( u
"EXTENT"_s ).isValid() )
276 bbox = parameterAsExtent( parameters, u
"EXTENT"_s, context, crs );
283 double minCellSize = 1e9;
285 QVector<QgsRasterCalculatorEntry> entries;
297 for (
int i = 0; i < nBands; ++i )
300 entry.
ref = u
"%1@%2"_s.arg( indexToName( n ) ).arg( i + 1 );
307 if ( rLayer->
crs() != crs )
310 ext = ct.transformBoundingBox( ext );
314 if ( cellSize < minCellSize )
316 minCellSize = cellSize;
320 double cellSize = parameterAsDouble( parameters, u
"CELL_SIZE"_s, context );
323 cellSize = minCellSize;
326 const QString expression = parameterAsExpression( parameters, u
"EXPRESSION"_s, context );
327 const QString outputFile = parameterAsOutputLayer( parameters, u
"OUTPUT"_s, context );
328 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u
"OUTPUT"_s, context );
330 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
331 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
335 qDeleteAll( mLayers );
356 outputs.insert( u
"OUTPUT"_s, outputFile );
360QString QgsRasterCalculatorModelerAlgorithm::indexToName(
int index )
const
368 mod = ( div - 1 ) % 26;
369 name =
static_cast<char>( 65 + mod ) + name;
370 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.
void setHelp(const QString &help)
Sets the help for the parameter.
An expression parameter for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
A numeric parameter for processing algorithms.
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.