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( 
"This algorithm performs algebraic operations using raster layers and generates in-memory result." );
 
   58QString QgsVirtualRasterCalculatorAlgorithm::shortDescription()
 const 
   60  return QObject::tr( 
"Performs algebraic operations using raster layers and generates in-memory result." );
 
   63QgsVirtualRasterCalculatorAlgorithm *QgsVirtualRasterCalculatorAlgorithm::createInstance()
 const 
   65  return new QgsVirtualRasterCalculatorAlgorithm();
 
   68void QgsVirtualRasterCalculatorAlgorithm::initAlgorithm( 
const QVariantMap & )
 
   72  auto extentParam = std::make_unique<QgsProcessingParameterExtent>( QStringLiteral( 
"EXTENT" ), QObject::tr( 
"Output extent" ), QVariant(), 
true );
 
   73  extentParam->setHelp( QObject::tr( 
"Extent of the output layer. If not specified, the extent will be the overall extent of all input layers" ) );
 
   74  addParameter( extentParam.release() );
 
   75  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 );
 
   76  cellSizeParam->setHelp( QObject::tr( 
"Cell size of the output layer. If not specified, the smallest cell size from the input layers will be used" ) );
 
   77  addParameter( cellSizeParam.release() );
 
   78  auto crsParam = std::make_unique<QgsProcessingParameterCrs>( QStringLiteral( 
"CRS" ), QObject::tr( 
"Output CRS" ), QVariant(), 
true );
 
   79  crsParam->setHelp( QObject::tr( 
"CRS of the output layer. If not specified, the CRS of the first input layer will be used" ) );
 
   80  addParameter( crsParam.release() );
 
   81  addParameter( 
new QgsProcessingParameterString( QStringLiteral( 
"LAYER_NAME" ), QObject::tr( 
"Output layer name" ), QVariant(), 
false, 
true ) );
 
   89  const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral( 
"LAYERS" ), context );
 
   90  if ( layers.isEmpty() )
 
   96  if ( parameters.value( QStringLiteral( 
"CRS" ) ).isValid() )
 
   98    crs = parameterAsCrs( parameters, QStringLiteral( 
"CRS" ), context );
 
  102    crs = layers.at( 0 )->crs();
 
  106  if ( parameters.value( QStringLiteral( 
"EXTENT" ) ).isValid() )
 
  108    bbox = parameterAsExtent( parameters, QStringLiteral( 
"EXTENT" ), context, 
crs );
 
  115  double minCellSize = 1e9;
 
  120    const QgsRasterLayer *rLayer = qobject_cast<const QgsRasterLayer *>( layer );
 
  133    if ( rLayer->
crs() != 
crs )
 
  136      ext = ct.transformBoundingBox( ext );
 
  140    if ( cellSize < minCellSize )
 
  142      minCellSize = cellSize;
 
  146  double cellSize = parameterAsDouble( parameters, QStringLiteral( 
"CELL_SIZE" ), context );
 
  149    cellSize = minCellSize;
 
  152  const QString expression = parameterAsExpression( parameters, QStringLiteral( 
"EXPRESSION" ), context );
 
  153  QString layerName = parameterAsString( parameters, QStringLiteral( 
"LAYER_NAME" ), context );
 
  154  if ( layerName.isEmpty() )
 
  156    layerName = expression;
 
  159  double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
 
  160  double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
 
  162  rasterParameters.
crs = 
crs;
 
  163  rasterParameters.
extent = bbox;
 
  164  rasterParameters.
width = width;
 
  165  rasterParameters.
height = height;
 
  166  rasterParameters.
formula = expression;
 
  168  std::unique_ptr<QgsRasterLayer> layer;
 
  170  if ( !layer->isValid() )
 
  172    feedback->
reportError( QObject::tr( 
"Failed to create virtual raster layer" ) );
 
  177  const QString layerId = layer->id();
 
  183  outputs.insert( QStringLiteral( 
"OUTPUT" ), layerId );
 
  192QString QgsVirtualRasterCalculatorModelerAlgorithm::name()
 const 
  194  return QStringLiteral( 
"modelervirtualrastercalc" );
 
  197QString QgsVirtualRasterCalculatorModelerAlgorithm::displayName()
 const 
  199  return QObject::tr( 
"Raster calculator (virtual)" );
 
  202QStringList QgsVirtualRasterCalculatorModelerAlgorithm::tags()
 const 
  204  return QObject::tr( 
"raster,calculator,virtual" ).split( 
',' );
 
  207QString QgsVirtualRasterCalculatorModelerAlgorithm::group()
 const 
  209  return QObject::tr( 
"Raster analysis" );
 
  212QString QgsVirtualRasterCalculatorModelerAlgorithm::groupId()
 const 
  214  return QStringLiteral( 
"rasteranalysis" );
 
  217QgsVirtualRasterCalculatorModelerAlgorithm *QgsVirtualRasterCalculatorModelerAlgorithm::createInstance()
 const 
  219  return new QgsVirtualRasterCalculatorModelerAlgorithm();
 
  224  Q_UNUSED( feedback );
 
  226  const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral( 
"INPUT" ), context );
 
  227  if ( layers.isEmpty() )
 
  233  if ( parameters.value( QStringLiteral( 
"CRS" ) ).isValid() )
 
  235    crs = parameterAsCrs( parameters, QStringLiteral( 
"CRS" ), context );
 
  239    crs = layers.at( 0 )->crs();
 
  243  if ( parameters.value( QStringLiteral( 
"EXTENT" ) ).isValid() )
 
  245    bbox = parameterAsExtent( parameters, QStringLiteral( 
"EXTENT" ), context, 
crs );
 
  252  double minCellSize = 1e9;
 
  258    const QgsRasterLayer *rLayer = qobject_cast<const QgsRasterLayer *>( layer );
 
  266    rasterLayer.
name = indexToName( n );
 
  272    if ( rLayer->
crs() != 
crs )
 
  275      ext = ct.transformBoundingBox( ext );
 
  279    if ( cellSize < minCellSize )
 
  281      minCellSize = cellSize;
 
  285  double cellSize = parameterAsDouble( parameters, QStringLiteral( 
"CELL_SIZE" ), context );
 
  288    cellSize = minCellSize;
 
  291  const QString expression = parameterAsExpression( parameters, QStringLiteral( 
"EXPRESSION" ), context );
 
  292  QString layerName = parameterAsString( parameters, QStringLiteral( 
"LAYER_NAME" ), context );
 
  293  if ( layerName.isEmpty() )
 
  295    layerName = expression;
 
  298  double width = std::round( ( bbox.
xMaximum() - bbox.
xMinimum() ) / cellSize );
 
  299  double height = std::round( ( bbox.
yMaximum() - bbox.
yMinimum() ) / cellSize );
 
  301  rasterParameters.
crs = 
crs;
 
  302  rasterParameters.
extent = bbox;
 
  303  rasterParameters.
width = width;
 
  304  rasterParameters.
height = height;
 
  305  rasterParameters.
formula = expression;
 
  307  std::unique_ptr<QgsRasterLayer> layer;
 
  309  if ( !layer->isValid() )
 
  311    feedback->
reportError( QObject::tr( 
"Failed to create virtual raster layer" ) );
 
  316  const QString layerId = layer->id();
 
  322  outputs.insert( QStringLiteral( 
"OUTPUT" ), layerId );
 
  326QString QgsVirtualRasterCalculatorModelerAlgorithm::indexToName( 
int index )
 const 
  334    mod = ( div - 1 ) % 26;
 
  335    name = 
static_cast<char>( 65 + mod ) + name;
 
  336    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.
 
const QgsCoordinateReferenceSystem & crs
 
Struct that stores the information about the parameters that should be given to the QgsVirtualRasterP...
 
QList< QgsRasterDataProvider::VirtualRasterInputLayers > rInputLayers
 
QgsCoordinateReferenceSystem crs