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<
82 cellSizeParam->
setHelp( QObject::tr(
"Cell size of the output layer. If not specified, the smallest cell size from the input layers will be used" ) );
83 addParameter( cellSizeParam.release() );
84 auto crsParam = std::make_unique<QgsProcessingParameterCrs>( u
"CRS"_s, QObject::tr(
"Output CRS" ), QVariant(),
true );
85 crsParam->setHelp( QObject::tr(
"CRS of the output layer. If not specified, the CRS of the first input layer will be used" ) );
86 addParameter( crsParam.release() );
93 QGS_MARK_ALGORITHM_SOURCE
97 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u
"LAYERS"_s, context );
98 if ( layers.isEmpty() )
104 if ( parameters.value( u
"CRS"_s ).isValid() )
106 crs = parameterAsCrs( parameters, u
"CRS"_s, context );
110 crs = layers.at( 0 )->crs();
114 if ( parameters.value( u
"EXTENT"_s ).isValid() )
116 bbox = parameterAsExtent( parameters, u
"EXTENT"_s, context, crs );
123 double minCellSize = 1e9;
128 const QgsRasterLayer *rLayer = qobject_cast<const QgsRasterLayer *>( layer );
141 if ( rLayer->
crs() != crs )
144 ext = ct.transformBoundingBox( ext );
148 if ( cellSize < minCellSize )
150 minCellSize = cellSize;
154 double cellSize = parameterAsDouble( parameters, u
"CELL_SIZE"_s, context );
157 cellSize = minCellSize;
160 const QString expression = parameterAsExpression( parameters, u
"EXPRESSION"_s, context );
161 QString layerName = parameterAsString( parameters, u
"LAYER_NAME"_s, context );
162 if ( layerName.isEmpty() )
164 layerName = expression;
167 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
168 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
170 rasterParameters.
crs = crs;
171 rasterParameters.
extent = bbox;
172 rasterParameters.
width = width;
173 rasterParameters.
height = height;
174 rasterParameters.
formula = expression;
176 std::unique_ptr<QgsRasterLayer> layer;
178 if ( !layer->isValid() )
180 feedback->
reportError( QObject::tr(
"Failed to create virtual raster layer" ) );
185 const QString layerId = layer->id();
191 outputs.insert( u
"OUTPUT"_s, layerId );
200QString QgsVirtualRasterCalculatorModelerAlgorithm::name()
const
202 return u
"modelervirtualrastercalc"_s;
205QString QgsVirtualRasterCalculatorModelerAlgorithm::displayName()
const
207 return QObject::tr(
"Raster calculator (virtual)" );
210QStringList QgsVirtualRasterCalculatorModelerAlgorithm::tags()
const
212 return QObject::tr(
"raster,calculator,virtual" ).split(
',' );
215QString QgsVirtualRasterCalculatorModelerAlgorithm::group()
const
217 return QObject::tr(
"Raster analysis" );
220QString QgsVirtualRasterCalculatorModelerAlgorithm::groupId()
const
222 return u
"rasteranalysis"_s;
225QgsVirtualRasterCalculatorModelerAlgorithm *QgsVirtualRasterCalculatorModelerAlgorithm::createInstance()
const
227 return new QgsVirtualRasterCalculatorModelerAlgorithm();
232 QGS_MARK_ALGORITHM_SOURCE
234 Q_UNUSED( feedback );
236 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u
"INPUT"_s, context );
237 if ( layers.isEmpty() )
243 if ( parameters.value( u
"CRS"_s ).isValid() )
245 crs = parameterAsCrs( parameters, u
"CRS"_s, context );
249 crs = layers.at( 0 )->crs();
253 if ( parameters.value( u
"EXTENT"_s ).isValid() )
255 bbox = parameterAsExtent( parameters, u
"EXTENT"_s, context, crs );
262 double minCellSize = 1e9;
268 const QgsRasterLayer *rLayer = qobject_cast<const QgsRasterLayer *>( layer );
276 rasterLayer.
name = indexToName( n );
282 if ( rLayer->
crs() != crs )
285 ext = ct.transformBoundingBox( ext );
289 if ( cellSize < minCellSize )
291 minCellSize = cellSize;
295 double cellSize = parameterAsDouble( parameters, u
"CELL_SIZE"_s, context );
298 cellSize = minCellSize;
301 const QString expression = parameterAsExpression( parameters, u
"EXPRESSION"_s, context );
302 QString layerName = parameterAsString( parameters, u
"LAYER_NAME"_s, context );
303 if ( layerName.isEmpty() )
305 layerName = expression;
308 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
309 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
311 rasterParameters.
crs = crs;
312 rasterParameters.
extent = bbox;
313 rasterParameters.
width = width;
314 rasterParameters.
height = height;
315 rasterParameters.
formula = expression;
317 std::unique_ptr<QgsRasterLayer> layer;
319 if ( !layer->isValid() )
321 feedback->
reportError( QObject::tr(
"Failed to create virtual raster layer" ) );
326 const QString layerId = layer->id();
332 outputs.insert( u
"OUTPUT"_s, layerId );
336QString QgsVirtualRasterCalculatorModelerAlgorithm::indexToName(
int index )
const
344 mod = ( div - 1 ) % 26;
345 name =
static_cast<char>( 65 + mod ) + name;
346 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 Q_INVOKABLE 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.
void setHelp(const QString &help)
Sets the help for the parameter.
An expression parameter for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
A numeric parameter for processing algorithms.
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