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 layerName, QStringLiteral(
"virtualraster" ) );
166 if ( !layer->isValid() )
168 feedback->
reportError( QObject::tr(
"Failed to create virtual raster layer" ) );
173 const QString layerId = layer->id();
179 outputs.insert( QStringLiteral(
"OUTPUT" ), layerId );
188QString QgsVirtualRasterCalculatorModelerAlgorithm::name()
const
190 return QStringLiteral(
"modelervirtualrastercalc" );
193QString QgsVirtualRasterCalculatorModelerAlgorithm::displayName()
const
195 return QObject::tr(
"Raster calculator (virtual)" );
198QStringList QgsVirtualRasterCalculatorModelerAlgorithm::tags()
const
200 return QObject::tr(
"raster,calculator,virtual" ).split(
',' );
203QString QgsVirtualRasterCalculatorModelerAlgorithm::group()
const
205 return QObject::tr(
"Raster analysis" );
208QString QgsVirtualRasterCalculatorModelerAlgorithm::groupId()
const
210 return QStringLiteral(
"rasteranalysis" );
213QgsVirtualRasterCalculatorModelerAlgorithm *QgsVirtualRasterCalculatorModelerAlgorithm::createInstance()
const
215 return new QgsVirtualRasterCalculatorModelerAlgorithm();
220 Q_UNUSED( feedback );
222 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral(
"INPUT" ), context );
223 if ( layers.isEmpty() )
229 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
231 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
235 crs = layers.at( 0 )->crs();
239 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
241 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context,
crs );
248 double minCellSize = 1e9;
254 const QgsRasterLayer *rLayer = qobject_cast<const QgsRasterLayer *>( layer );
262 rasterLayer.
name = indexToName( n );
268 if ( rLayer->
crs() !=
crs )
271 ext = ct.transformBoundingBox( ext );
275 if ( cellSize < minCellSize )
277 minCellSize = cellSize;
281 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
284 cellSize = minCellSize;
287 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
288 QString layerName = parameterAsString( parameters, QStringLiteral(
"LAYER_NAME" ), context );
289 if ( layerName.isEmpty() )
291 layerName = expression;
294 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
295 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
297 rasterParameters.
crs =
crs;
298 rasterParameters.
extent = bbox;
299 rasterParameters.
width = width;
300 rasterParameters.
height = height;
301 rasterParameters.
formula = expression;
303 std::unique_ptr< QgsRasterLayer > layer;
305 layerName, QStringLiteral(
"virtualraster" ) );
306 if ( !layer->isValid() )
308 feedback->
reportError( QObject::tr(
"Failed to create virtual raster layer" ) );
313 const QString layerId = layer->id();
319 outputs.insert( QStringLiteral(
"OUTPUT" ), layerId );
323QString QgsVirtualRasterCalculatorModelerAlgorithm::indexToName(
int index )
const
331 mod = ( div - 1 ) % 26;
332 name =
static_cast<char>( 65 + mod ) + name;
333 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.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
const QgsCoordinateReferenceSystem & crs
Struct that stores the information about the parameters that should be given to the QgsVirtualRasterP...
QList< QgsRasterDataProvider::VirtualRasterInputLayers > rInputLayers
QgsCoordinateReferenceSystem crs