29QString QgsVirtualRasterCalculatorAlgorithm::name()
const
31 return QStringLiteral(
"virtualrastercalc" );
34QString QgsVirtualRasterCalculatorAlgorithm::displayName()
const
36 return QObject::tr(
"Raster calculator (virtual)" );
39QStringList QgsVirtualRasterCalculatorAlgorithm::tags()
const
41 return QObject::tr(
"raster,calculator,virtual" ).split(
',' );
44QString QgsVirtualRasterCalculatorAlgorithm::group()
const
46 return QObject::tr(
"Raster analysis" );
49QString QgsVirtualRasterCalculatorAlgorithm::groupId()
const
51 return QStringLiteral(
"rasteranalysis" );
54QString QgsVirtualRasterCalculatorAlgorithm::shortHelpString()
const
56 return QObject::tr(
"This algorithm performs algebraic operations using raster layers and generates in-memory result." );
59QString QgsVirtualRasterCalculatorAlgorithm::shortDescription()
const
61 return QObject::tr(
"Performs algebraic operations using raster layers and generates in-memory result." );
64QgsVirtualRasterCalculatorAlgorithm *QgsVirtualRasterCalculatorAlgorithm::createInstance()
const
66 return new QgsVirtualRasterCalculatorAlgorithm();
69void QgsVirtualRasterCalculatorAlgorithm::initAlgorithm(
const QVariantMap & )
73 auto extentParam = std::make_unique<QgsProcessingParameterExtent>( QStringLiteral(
"EXTENT" ), QObject::tr(
"Output extent" ), QVariant(),
true );
74 extentParam->setHelp( QObject::tr(
"Extent of the output layer. If not specified, the extent will be the overall extent of all input layers" ) );
75 addParameter( extentParam.release() );
76 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 );
77 cellSizeParam->setHelp( QObject::tr(
"Cell size of the output layer. If not specified, the smallest cell size from the input layers will be used" ) );
78 addParameter( cellSizeParam.release() );
79 auto crsParam = std::make_unique<QgsProcessingParameterCrs>( QStringLiteral(
"CRS" ), QObject::tr(
"Output CRS" ), QVariant(),
true );
80 crsParam->setHelp( QObject::tr(
"CRS of the output layer. If not specified, the CRS of the first input layer will be used" ) );
81 addParameter( crsParam.release() );
82 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"LAYER_NAME" ), QObject::tr(
"Output layer name" ), QVariant(),
false,
true ) );
90 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
91 if ( layers.isEmpty() )
97 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
99 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
103 crs = layers.at( 0 )->crs();
107 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
109 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context, crs );
116 double minCellSize = 1e9;
121 const QgsRasterLayer *rLayer = qobject_cast<const QgsRasterLayer *>( layer );
134 if ( rLayer->
crs() != crs )
137 ext = ct.transformBoundingBox( ext );
141 if ( cellSize < minCellSize )
143 minCellSize = cellSize;
147 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
150 cellSize = minCellSize;
153 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
154 QString layerName = parameterAsString( parameters, QStringLiteral(
"LAYER_NAME" ), context );
155 if ( layerName.isEmpty() )
157 layerName = expression;
160 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
161 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
163 rasterParameters.
crs = crs;
164 rasterParameters.
extent = bbox;
165 rasterParameters.
width = width;
166 rasterParameters.
height = height;
167 rasterParameters.
formula = expression;
169 std::unique_ptr<QgsRasterLayer> layer;
171 if ( !layer->isValid() )
173 feedback->
reportError( QObject::tr(
"Failed to create virtual raster layer" ) );
178 const QString layerId = layer->id();
184 outputs.insert( QStringLiteral(
"OUTPUT" ), layerId );
193QString QgsVirtualRasterCalculatorModelerAlgorithm::name()
const
195 return QStringLiteral(
"modelervirtualrastercalc" );
198QString QgsVirtualRasterCalculatorModelerAlgorithm::displayName()
const
200 return QObject::tr(
"Raster calculator (virtual)" );
203QStringList QgsVirtualRasterCalculatorModelerAlgorithm::tags()
const
205 return QObject::tr(
"raster,calculator,virtual" ).split(
',' );
208QString QgsVirtualRasterCalculatorModelerAlgorithm::group()
const
210 return QObject::tr(
"Raster analysis" );
213QString QgsVirtualRasterCalculatorModelerAlgorithm::groupId()
const
215 return QStringLiteral(
"rasteranalysis" );
218QgsVirtualRasterCalculatorModelerAlgorithm *QgsVirtualRasterCalculatorModelerAlgorithm::createInstance()
const
220 return new QgsVirtualRasterCalculatorModelerAlgorithm();
225 Q_UNUSED( feedback );
227 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral(
"INPUT" ), context );
228 if ( layers.isEmpty() )
234 if ( parameters.value( QStringLiteral(
"CRS" ) ).isValid() )
236 crs = parameterAsCrs( parameters, QStringLiteral(
"CRS" ), context );
240 crs = layers.at( 0 )->crs();
244 if ( parameters.value( QStringLiteral(
"EXTENT" ) ).isValid() )
246 bbox = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context, crs );
253 double minCellSize = 1e9;
259 const QgsRasterLayer *rLayer = qobject_cast<const QgsRasterLayer *>( layer );
267 rasterLayer.
name = indexToName( n );
273 if ( rLayer->
crs() != crs )
276 ext = ct.transformBoundingBox( ext );
280 if ( cellSize < minCellSize )
282 minCellSize = cellSize;
286 double cellSize = parameterAsDouble( parameters, QStringLiteral(
"CELL_SIZE" ), context );
289 cellSize = minCellSize;
292 const QString expression = parameterAsExpression( parameters, QStringLiteral(
"EXPRESSION" ), context );
293 QString layerName = parameterAsString( parameters, QStringLiteral(
"LAYER_NAME" ), context );
294 if ( layerName.isEmpty() )
296 layerName = expression;
299 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
300 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
302 rasterParameters.
crs = crs;
303 rasterParameters.
extent = bbox;
304 rasterParameters.
width = width;
305 rasterParameters.
height = height;
306 rasterParameters.
formula = expression;
308 std::unique_ptr<QgsRasterLayer> layer;
310 if ( !layer->isValid() )
312 feedback->
reportError( QObject::tr(
"Failed to create virtual raster layer" ) );
317 const QString layerId = layer->id();
323 outputs.insert( QStringLiteral(
"OUTPUT" ), layerId );
327QString QgsVirtualRasterCalculatorModelerAlgorithm::indexToName(
int index )
const
335 mod = ( div - 1 ) % 26;
336 name =
static_cast<char>( 65 + mod ) + name;
337 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.
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.
Struct that stores the information about the parameters that should be given to the QgsVirtualRasterP...
QList< QgsRasterDataProvider::VirtualRasterInputLayers > rInputLayers
QgsCoordinateReferenceSystem crs