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 auto 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 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 );
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 auto 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() );
78 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATE_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
79 createOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
81 addParameter( createOptsParam.release() );
88 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
90 for (
const QgsMapLayer *layer : std::as_const( layers ) )
93 clonedLayer->moveToThread(
nullptr );
94 mLayers << clonedLayer;
97 if ( mLayers.isEmpty() )
99 feedback->
reportError( QObject::tr(
"No layers selected" ),
false );
108 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
110 layer->moveToThread( QThread::currentThread() );
114 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
116 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
120 crs = mLayers.at( 0 )->crs();
124 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
126 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context,
crs );
133 double minCellSize = 1e9;
135 QVector<QgsRasterCalculatorEntry> entries;
145 for (
int i = 0; i < nBands; ++i )
148 entry.
ref = QStringLiteral(
"%1@%2" ).arg( rLayer->
name() ).arg( i + 1 );
155 if ( rLayer->
crs() !=
crs )
158 ext = ct.transformBoundingBox( ext );
162 if ( cellSize < minCellSize )
164 minCellSize = cellSize;
168 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
171 cellSize = minCellSize;
174 const QString createOptions = parameterAsString( parameters, QStringLiteral(
"CREATE_OPTIONS" ), context ).trimmed();
175 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
176 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
177 const QFileInfo fi( outputFile );
180 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
181 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
184 calc.setCreateOptions( createOptions.split(
'|', Qt::SplitBehaviorFlags::SkipEmptyParts ) );
186 qDeleteAll( mLayers );
207 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
216QString QgsRasterCalculatorModelerAlgorithm::name()
const
218 return QStringLiteral(
"modelerrastercalc" );
221QString QgsRasterCalculatorModelerAlgorithm::displayName()
const
223 return QObject::tr(
"Raster calculator" );
226QStringList QgsRasterCalculatorModelerAlgorithm::tags()
const
228 return QObject::tr(
"raster,calculator" ).split(
',' );
231QString QgsRasterCalculatorModelerAlgorithm::group()
const
233 return QObject::tr(
"Raster analysis" );
236QString QgsRasterCalculatorModelerAlgorithm::groupId()
const
238 return QStringLiteral(
"rasteranalysis" );
241QgsRasterCalculatorModelerAlgorithm *QgsRasterCalculatorModelerAlgorithm::createInstance()
const
243 return new QgsRasterCalculatorModelerAlgorithm();
248 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
250 layer->moveToThread( QThread::currentThread() );
254 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
256 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
260 crs = mLayers.at( 0 )->crs();
264 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
266 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context,
crs );
273 double minCellSize = 1e9;
275 QVector<QgsRasterCalculatorEntry> entries;
287 for (
int i = 0; i < nBands; ++i )
290 entry.
ref = QStringLiteral(
"%1@%2" ).arg( indexToName( n ) ).arg( i + 1 );
297 if ( rLayer->
crs() !=
crs )
300 ext = ct.transformBoundingBox( ext );
304 if ( cellSize < minCellSize )
306 minCellSize = cellSize;
310 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
313 cellSize = minCellSize;
316 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
317 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
318 const QFileInfo fi( outputFile );
321 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
322 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
326 qDeleteAll( mLayers );
347 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
351QString QgsRasterCalculatorModelerAlgorithm::indexToName(
int index )
const
359 mod = ( div - 1 ) % 26;
360 name =
static_cast<char>( 65 + mod ) + name;
361 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.
@ 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