24using namespace Qt::StringLiterals;
33QString QgsVirtualRasterCalculatorAlgorithm::name()
const
35 return u
"virtualrastercalc"_s;
38QString QgsVirtualRasterCalculatorAlgorithm::displayName()
const
40 return QObject::tr(
"Raster calculator (virtual)" );
43QStringList QgsVirtualRasterCalculatorAlgorithm::tags()
const
45 return QObject::tr(
"raster,calculator,virtual" ).split(
',' );
48QString QgsVirtualRasterCalculatorAlgorithm::group()
const
50 return QObject::tr(
"Raster analysis" );
53QString QgsVirtualRasterCalculatorAlgorithm::groupId()
const
55 return u
"rasteranalysis"_s;
58QString QgsVirtualRasterCalculatorAlgorithm::shortHelpString()
const
60 return QObject::tr(
"This algorithm performs algebraic operations using raster layers and generates in-memory result." );
63QString QgsVirtualRasterCalculatorAlgorithm::shortDescription()
const
65 return QObject::tr(
"Performs algebraic operations using raster layers and generates in-memory result." );
68QgsVirtualRasterCalculatorAlgorithm *QgsVirtualRasterCalculatorAlgorithm::createInstance()
const
70 return new QgsVirtualRasterCalculatorAlgorithm();
73void QgsVirtualRasterCalculatorAlgorithm::initAlgorithm(
const QVariantMap & )
77 auto extentParam = std::make_unique<QgsProcessingParameterExtent>( u
"EXTENT"_s, QObject::tr(
"Output extent" ), QVariant(),
true );
78 extentParam->setHelp( QObject::tr(
"Extent of the output layer. If not specified, the extent will be the overall extent of all input layers" ) );
79 addParameter( extentParam.release() );
80 auto cellSizeParam = std::make_unique<QgsProcessingParameterNumber>( u
"CELL_SIZE"_s, QObject::tr(
"Output cell size (leave empty to set automatically)" ),
Qgis::ProcessingNumberParameterType::Double, QVariant(),
true, 0.0 );
81 cellSizeParam->setHelp( QObject::tr(
"Cell size of the output layer. If not specified, the smallest cell size from the input layers will be used" ) );
82 addParameter( cellSizeParam.release() );
83 auto crsParam = std::make_unique<QgsProcessingParameterCrs>( u
"CRS"_s, QObject::tr(
"Output CRS" ), QVariant(),
true );
84 crsParam->setHelp( QObject::tr(
"CRS of the output layer. If not specified, the CRS of the first input layer will be used" ) );
85 addParameter( crsParam.release() );
94 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u
"LAYERS"_s, context );
95 if ( layers.isEmpty() )
101 if ( parameters.value( u
"CRS"_s ).isValid() )
103 crs = parameterAsCrs( parameters, u
"CRS"_s, context );
107 crs = layers.at( 0 )->crs();
111 if ( parameters.value( u
"EXTENT"_s ).isValid() )
113 bbox = parameterAsExtent( parameters, u
"EXTENT"_s, context, crs );
120 double minCellSize = 1e9;
125 const QgsRasterLayer *rLayer = qobject_cast<const QgsRasterLayer *>( layer );
138 if ( rLayer->
crs() != crs )
141 ext = ct.transformBoundingBox( ext );
145 if ( cellSize < minCellSize )
147 minCellSize = cellSize;
151 double cellSize = parameterAsDouble( parameters, u
"CELL_SIZE"_s, context );
154 cellSize = minCellSize;
157 const QString expression = parameterAsExpression( parameters, u
"EXPRESSION"_s, context );
158 QString layerName = parameterAsString( parameters, u
"LAYER_NAME"_s, context );
159 if ( layerName.isEmpty() )
161 layerName = expression;
164 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
165 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
167 rasterParameters.
crs = crs;
168 rasterParameters.
extent = bbox;
169 rasterParameters.
width = width;
170 rasterParameters.
height = height;
171 rasterParameters.
formula = expression;
173 std::unique_ptr<QgsRasterLayer> layer;
175 if ( !layer->isValid() )
177 feedback->
reportError( QObject::tr(
"Failed to create virtual raster layer" ) );
182 const QString layerId = layer->id();
188 outputs.insert( u
"OUTPUT"_s, layerId );
197QString QgsVirtualRasterCalculatorModelerAlgorithm::name()
const
199 return u
"modelervirtualrastercalc"_s;
202QString QgsVirtualRasterCalculatorModelerAlgorithm::displayName()
const
204 return QObject::tr(
"Raster calculator (virtual)" );
207QStringList QgsVirtualRasterCalculatorModelerAlgorithm::tags()
const
209 return QObject::tr(
"raster,calculator,virtual" ).split(
',' );
212QString QgsVirtualRasterCalculatorModelerAlgorithm::group()
const
214 return QObject::tr(
"Raster analysis" );
217QString QgsVirtualRasterCalculatorModelerAlgorithm::groupId()
const
219 return u
"rasteranalysis"_s;
222QgsVirtualRasterCalculatorModelerAlgorithm *QgsVirtualRasterCalculatorModelerAlgorithm::createInstance()
const
224 return new QgsVirtualRasterCalculatorModelerAlgorithm();
229 Q_UNUSED( feedback );
231 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u
"INPUT"_s, context );
232 if ( layers.isEmpty() )
238 if ( parameters.value( u
"CRS"_s ).isValid() )
240 crs = parameterAsCrs( parameters, u
"CRS"_s, context );
244 crs = layers.at( 0 )->crs();
248 if ( parameters.value( u
"EXTENT"_s ).isValid() )
250 bbox = parameterAsExtent( parameters, u
"EXTENT"_s, context, crs );
257 double minCellSize = 1e9;
263 const QgsRasterLayer *rLayer = qobject_cast<const QgsRasterLayer *>( layer );
271 rasterLayer.
name = indexToName( n );
277 if ( rLayer->
crs() != crs )
280 ext = ct.transformBoundingBox( ext );
284 if ( cellSize < minCellSize )
286 minCellSize = cellSize;
290 double cellSize = parameterAsDouble( parameters, u
"CELL_SIZE"_s, context );
293 cellSize = minCellSize;
296 const QString expression = parameterAsExpression( parameters, u
"EXPRESSION"_s, context );
297 QString layerName = parameterAsString( parameters, u
"LAYER_NAME"_s, context );
298 if ( layerName.isEmpty() )
300 layerName = expression;
303 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
304 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
306 rasterParameters.
crs = crs;
307 rasterParameters.
extent = bbox;
308 rasterParameters.
width = width;
309 rasterParameters.
height = height;
310 rasterParameters.
formula = expression;
312 std::unique_ptr<QgsRasterLayer> layer;
314 if ( !layer->isValid() )
316 feedback->
reportError( QObject::tr(
"Failed to create virtual raster layer" ) );
321 const QString layerId = layer->id();
327 outputs.insert( u
"OUTPUT"_s, layerId );
331QString QgsVirtualRasterCalculatorModelerAlgorithm::indexToName(
int index )
const
339 mod = ( div - 1 ) % 26;
340 name =
static_cast<char>( 65 + mod ) + name;
341 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