28QString QgsVirtualRasterCalculatorAlgorithm::name()
const
30 return QStringLiteral(
"virtualrastercalc" );
33QString QgsVirtualRasterCalculatorAlgorithm::displayName()
const
35 return QObject::tr(
"Raster calculator (virtual)" );
38QStringList QgsVirtualRasterCalculatorAlgorithm::tags()
const
40 return QObject::tr(
"raster,calculator,virtual" ).split(
',' );
43QString QgsVirtualRasterCalculatorAlgorithm::group()
const
45 return QObject::tr(
"Raster analysis" );
48QString QgsVirtualRasterCalculatorAlgorithm::groupId()
const
50 return QStringLiteral(
"rasteranalysis" );
53QString QgsVirtualRasterCalculatorAlgorithm::shortHelpString()
const
55 return QObject::tr(
"Performs algebraic operations using raster layers and generates in-memory result." );
58QgsVirtualRasterCalculatorAlgorithm *QgsVirtualRasterCalculatorAlgorithm::createInstance()
const
60 return new QgsVirtualRasterCalculatorAlgorithm();
63void QgsVirtualRasterCalculatorAlgorithm::initAlgorithm(
const QVariantMap & )
67 std::unique_ptr<QgsProcessingParameterExtent> extentParam = std::make_unique<QgsProcessingParameterExtent>( QStringLiteral(
"EXTENT" ), QObject::tr(
"Output extent" ), QVariant(),
true );
68 extentParam->setHelp( QObject::tr(
"Extent of the output layer. If not specified, the extent will be the overall extent of all input layers" ) );
69 addParameter( extentParam.release() );
70 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 );
71 cellSizeParam->setHelp( QObject::tr(
"Cell size of the output layer. If not specified, the smallest cell size from the input layers will be used" ) );
72 addParameter( cellSizeParam.release() );
73 std::unique_ptr<QgsProcessingParameterCrs> crsParam = std::make_unique<QgsProcessingParameterCrs>( QStringLiteral(
"CRS" ), QObject::tr(
"Output CRS" ), QVariant(),
true );
74 crsParam->setHelp( QObject::tr(
"CRS of the output layer. If not specified, the CRS of the first input layer will be used" ) );
75 addParameter( crsParam.release() );
76 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"LAYER_NAME" ), QObject::tr(
"Output layer name" ), QVariant(),
false,
true ) );
84 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
85 if ( layers.isEmpty() )
91 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
93 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
97 crs = layers.at( 0 )->crs();
101 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
103 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context,
crs );
110 double minCellSize = 1e9;
115 const QgsRasterLayer *rLayer = qobject_cast<const QgsRasterLayer *>( layer );
128 if ( rLayer->
crs() !=
crs )
131 ext = ct.transformBoundingBox( ext );
135 if ( cellSize < minCellSize )
137 minCellSize = cellSize;
141 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
144 cellSize = minCellSize;
147 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
148 QString layerName = parameterAsString( parameters, QStringLiteral(
"LAYER_NAME" ), context );
149 if ( layerName.isEmpty() )
151 layerName = expression;
154 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
155 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
157 rasterParameters.
crs =
crs;
158 rasterParameters.
extent = bbox;
159 rasterParameters.
width = width;
160 rasterParameters.
height = height;
161 rasterParameters.
formula = expression;
163 std::unique_ptr<QgsRasterLayer> layer;
165 if ( !layer->isValid() )
167 feedback->
reportError( QObject::tr(
"Failed to create virtual raster layer" ) );
172 const QString layerId = layer->id();
178 outputs.insert( QStringLiteral(
"OUTPUT" ), layerId );
187QString QgsVirtualRasterCalculatorModelerAlgorithm::name()
const
189 return QStringLiteral(
"modelervirtualrastercalc" );
192QString QgsVirtualRasterCalculatorModelerAlgorithm::displayName()
const
194 return QObject::tr(
"Raster calculator (virtual)" );
197QStringList QgsVirtualRasterCalculatorModelerAlgorithm::tags()
const
199 return QObject::tr(
"raster,calculator,virtual" ).split(
',' );
202QString QgsVirtualRasterCalculatorModelerAlgorithm::group()
const
204 return QObject::tr(
"Raster analysis" );
207QString QgsVirtualRasterCalculatorModelerAlgorithm::groupId()
const
209 return QStringLiteral(
"rasteranalysis" );
212QgsVirtualRasterCalculatorModelerAlgorithm *QgsVirtualRasterCalculatorModelerAlgorithm::createInstance()
const
214 return new QgsVirtualRasterCalculatorModelerAlgorithm();
219 Q_UNUSED( feedback );
221 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral(
"INPUT" ), context );
222 if ( layers.isEmpty() )
228 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
230 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
234 crs = layers.at( 0 )->crs();
238 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
240 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context,
crs );
247 double minCellSize = 1e9;
253 const QgsRasterLayer *rLayer = qobject_cast<const QgsRasterLayer *>( layer );
261 rasterLayer.
name = indexToName( n );
267 if ( rLayer->
crs() !=
crs )
270 ext = ct.transformBoundingBox( ext );
274 if ( cellSize < minCellSize )
276 minCellSize = cellSize;
280 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
283 cellSize = minCellSize;
286 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
287 QString layerName = parameterAsString( parameters, QStringLiteral(
"LAYER_NAME" ), context );
288 if ( layerName.isEmpty() )
290 layerName = expression;
293 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
294 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
296 rasterParameters.
crs =
crs;
297 rasterParameters.
extent = bbox;
298 rasterParameters.
width = width;
299 rasterParameters.
height = height;
300 rasterParameters.
formula = expression;
302 std::unique_ptr<QgsRasterLayer> layer;
304 if ( !layer->isValid() )
306 feedback->
reportError( QObject::tr(
"Failed to create virtual raster layer" ) );
311 const QString layerId = layer->id();
317 outputs.insert( QStringLiteral(
"OUTPUT" ), layerId );
321QString QgsVirtualRasterCalculatorModelerAlgorithm::indexToName(
int index )
const
329 mod = ( div - 1 ) % 26;
330 name =
static_cast<char>( 65 + mod ) + name;
331 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.
@ NoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
@ Double
Double/float values.
This class represents a coordinate reference system (CRS).
virtual QString name() const =0
Returns a provider name.
QgsMapLayer * addMapLayer(QgsMapLayer *layer, bool takeOwnership=true)
Add a layer to the store.
Base class for all map layer types.
virtual QgsRectangle extent() const
Returns the extent of the layer.
QString source() const
Returns the source for 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.
Details for layers to load into projects.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
void addLayerToLoadOnCompletion(const QString &layer, const QgsProcessingContext::LayerDetails &details)
Adds a layer to load (by ID or datasource) into the canvas upon completion of the algorithm or model.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
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.
A raster layer output for processing algorithms.
An expression parameter for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
A string parameter for processing algorithms.
@ Raster
Raster layer type.
static QgsRectangle combineLayerExtents(const QList< QgsMapLayer * > &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context)
Combines the extent of several map layers.
static QString encodeVirtualRasterProviderUri(const VirtualRasterParameters &parts)
Encodes the URI starting from the struct .
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
Struct that stores the information about the parameters that should be given to the QgsVirtualRasterP...
QList< QgsRasterDataProvider::VirtualRasterInputLayers > rInputLayers
QgsCoordinateReferenceSystem crs