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() );
95 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u
"LAYERS"_s, context );
96 if ( layers.isEmpty() )
102 if ( parameters.value( u
"CRS"_s ).isValid() )
104 crs = parameterAsCrs( parameters, u
"CRS"_s, context );
108 crs = layers.at( 0 )->crs();
112 if ( parameters.value( u
"EXTENT"_s ).isValid() )
114 bbox = parameterAsExtent( parameters, u
"EXTENT"_s, context, crs );
121 double minCellSize = 1e9;
126 const QgsRasterLayer *rLayer = qobject_cast<const QgsRasterLayer *>( layer );
139 if ( rLayer->
crs() != crs )
142 ext = ct.transformBoundingBox( ext );
146 if ( cellSize < minCellSize )
148 minCellSize = cellSize;
152 double cellSize = parameterAsDouble( parameters, u
"CELL_SIZE"_s, context );
155 cellSize = minCellSize;
158 const QString expression = parameterAsExpression( parameters, u
"EXPRESSION"_s, context );
159 QString layerName = parameterAsString( parameters, u
"LAYER_NAME"_s, context );
160 if ( layerName.isEmpty() )
162 layerName = expression;
165 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
166 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
168 rasterParameters.
crs = crs;
169 rasterParameters.
extent = bbox;
170 rasterParameters.
width = width;
171 rasterParameters.
height = height;
172 rasterParameters.
formula = expression;
174 std::unique_ptr<QgsRasterLayer> layer;
176 if ( !layer->isValid() )
178 feedback->
reportError( QObject::tr(
"Failed to create virtual raster layer" ) );
183 const QString layerId = layer->id();
189 outputs.insert( u
"OUTPUT"_s, layerId );
198QString QgsVirtualRasterCalculatorModelerAlgorithm::name()
const
200 return u
"modelervirtualrastercalc"_s;
203QString QgsVirtualRasterCalculatorModelerAlgorithm::displayName()
const
205 return QObject::tr(
"Raster calculator (virtual)" );
208QStringList QgsVirtualRasterCalculatorModelerAlgorithm::tags()
const
210 return QObject::tr(
"raster,calculator,virtual" ).split(
',' );
213QString QgsVirtualRasterCalculatorModelerAlgorithm::group()
const
215 return QObject::tr(
"Raster analysis" );
218QString QgsVirtualRasterCalculatorModelerAlgorithm::groupId()
const
220 return u
"rasteranalysis"_s;
223QgsVirtualRasterCalculatorModelerAlgorithm *QgsVirtualRasterCalculatorModelerAlgorithm::createInstance()
const
225 return new QgsVirtualRasterCalculatorModelerAlgorithm();
230 Q_UNUSED( feedback );
232 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u
"INPUT"_s, context );
233 if ( layers.isEmpty() )
239 if ( parameters.value( u
"CRS"_s ).isValid() )
241 crs = parameterAsCrs( parameters, u
"CRS"_s, context );
245 crs = layers.at( 0 )->crs();
249 if ( parameters.value( u
"EXTENT"_s ).isValid() )
251 bbox = parameterAsExtent( parameters, u
"EXTENT"_s, context, crs );
258 double minCellSize = 1e9;
264 const QgsRasterLayer *rLayer = qobject_cast<const QgsRasterLayer *>( layer );
272 rasterLayer.
name = indexToName( n );
278 if ( rLayer->
crs() != crs )
281 ext = ct.transformBoundingBox( ext );
285 if ( cellSize < minCellSize )
287 minCellSize = cellSize;
291 double cellSize = parameterAsDouble( parameters, u
"CELL_SIZE"_s, context );
294 cellSize = minCellSize;
297 const QString expression = parameterAsExpression( parameters, u
"EXPRESSION"_s, context );
298 QString layerName = parameterAsString( parameters, u
"LAYER_NAME"_s, context );
299 if ( layerName.isEmpty() )
301 layerName = expression;
304 double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
305 double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
307 rasterParameters.
crs = crs;
308 rasterParameters.
extent = bbox;
309 rasterParameters.
width = width;
310 rasterParameters.
height = height;
311 rasterParameters.
formula = expression;
313 std::unique_ptr<QgsRasterLayer> layer;
315 if ( !layer->isValid() )
317 feedback->
reportError( QObject::tr(
"Failed to create virtual raster layer" ) );
322 const QString layerId = layer->id();
328 outputs.insert( u
"OUTPUT"_s, layerId );
332QString QgsVirtualRasterCalculatorModelerAlgorithm::indexToName(
int index )
const
340 mod = ( div - 1 ) % 26;
341 name =
static_cast<char>( 65 + mod ) + name;
342 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.
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