33using namespace Qt::StringLiterals;
42 QString styleFilePath;
49 if ( styleFilePath.isEmpty() )
53 switch ( layer->
type() )
57 QgsVectorLayer *vectorLayer = qobject_cast< QgsVectorLayer * >( layer );
61 styleFilePath = settings.
value( u
"Processing/Configuration/VECTOR_POINT_STYLE"_s, QString() ).toString();
64 styleFilePath = settings.
value( u
"Processing/Configuration/VECTOR_LINE_STYLE"_s, QString() ).toString();
67 styleFilePath = settings.
value( u
"Processing/Configuration/VECTOR_POLYGON_STYLE"_s, QString() ).toString();
77 styleFilePath = settings.
value( u
"Processing/Configuration/RASTER_STYLE"_s, QString() ).toString();
91 if ( !styleFilePath.isEmpty() )
114 auto it = parameters.constFind( output->name() );
120 QVariant outputValue = it.value();
121 if ( outputValue.userType() == qMetaTypeId<QgsProcessingOutputLayerDefinition >() )
128 outputValue = outputValue.toString();
130 if ( outputValue.toString() == destinationId )
132 return output->name();
142 if ( !destinationProject )
150 const QString resultsGroupName = settings.
value( u
"Processing/Configuration/RESULTS_GROUP_NAME"_s, QString() ).toString();
152 if ( !resultsGroupName.isEmpty() )
181 group = resultsGroup;
194 if ( settings.
value( u
"Processing/Configuration/VECTOR_FEATURE_COUNT"_s,
false ).toBool() )
203 std::unique_ptr< QgsProcessingFeedback > localFeedback;
206 localFeedback = std::make_unique< QgsProcessingFeedback >();
207 feedback = localFeedback.get();
219 feedback->
setProgressText( QObject::tr(
"Loading resulting layers" ) );
222 QStringList invalidLayers;
225 struct LayerToPostProcess
231 QList< LayerToPostProcess > layersToPostProcess;
235 for (
auto it = layersToLoad.constBegin(); it != layersToLoad.constEnd(); ++it, ++i )
240 if ( layersToLoad.size() > 2 )
243 feedback->
setProgress( 100 * i /
static_cast< float >( layersToLoad.size() ) );
246 const QString destId = it.key();
279 layersToPostProcess.append( LayerToPostProcess { layer, details } );
284 invalidLayers.append( destId );
288 result.
succeeded = invalidLayers.empty();
294 for (
const LayerToPostProcess &layer : std::as_const( layersToPostProcess ) )
296 layer.details.postProcessor()->postProcessLayer( layer.layer, context, feedback );
303 if ( !invalidLayers.empty() )
305 QString msg = QObject::tr(
"The following layers were not correctly generated." );
306 QStringList layerList;
307 layerList.reserve( invalidLayers.size() );
308 for (
const QString &layerId : std::as_const( invalidLayers ) )
310 layerList.append( u
"• %1"_s.arg( layerId ) );
312 msg += u
"\n%1\n"_s.arg( layerList.join(
'\n' ) );
313 msg += QObject::tr(
"You can check the 'Log Messages Panel' in QGIS main window to find more information about the execution of the algorithm." );
331 QVector<QgsProcessingResultsHandler::ResultLayerDetails> sortedLayers = layers;
333 return a.sortKey < b.sortKey;
336 bool haveSetActiveLayer =
false;
338 qsizetype defaultTargetGroupIndex = 0;
339 if (
auto currentSelectedLayer = qobject_cast< QgsLayerTreeLayer * >( currentSelectedNode ) )
341 defaultTargetGroup = qobject_cast< QgsLayerTreeGroup * >( currentSelectedLayer->parent() );
342 if ( defaultTargetGroup )
343 defaultTargetGroupIndex = defaultTargetGroup->
children().indexOf( currentSelectedNode );
345 if (
auto currentSelectedGroup = qobject_cast< QgsLayerTreeGroup * >( currentSelectedNode ) )
347 defaultTargetGroup = currentSelectedGroup;
353 QgsProject *project = layerDetails.destinationProject;
358 std::optional< QgsLayerTreeRegistryBridge::InsertionPoint > previousInsertionPoint;
364 std::optional< QgsLayerTreeRegistryBridge::InsertionPoint > insertionPoint;
365 if ( layerDetails.targetLayerTreeGroup )
373 if ( defaultTargetGroup )
383 if ( project && insertionPoint.has_value() )
395 if ( !haveSetActiveLayer )
397 resultLayer = layerDetails.layer;
398 haveSetActiveLayer =
true;
402 if ( project && previousInsertionPoint.has_value() )
@ Group
Composite group layer. Added in QGIS 3.24.
@ Plugin
Plugin based layer.
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
@ Mesh
Mesh layer. Added in QGIS 3.2.
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
static QgsProcessingRegistry * processingRegistry()
Returns the application's processing registry, used for managing processing providers,...
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Layer tree group node serves as a container for layers and further groups.
QgsLayerTreeGroup * findGroup(const QString &name)
Find group node with specified name.
QgsLayerTreeGroup * insertGroup(int index, const QString &name)
Insert a new group node with given name at specified position.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
Layer tree node points to a map layer.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
Base class for nodes in a layer tree.
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
void setExpanded(bool expanded)
Sets whether the node should be shown as expanded or collapsed in GUI.
Q_DECL_DEPRECATED void setLayerInsertionPoint(QgsLayerTreeGroup *parentGroup, int index)
Set where the new layers should be inserted - can be used to follow current selection.
InsertionPoint layerInsertionPoint() const
Returns the insertion point used to add layers to the tree.
QgsMapLayer * takeMapLayer(QgsMapLayer *layer)
Takes a layer from the store.
Base class for all map layer types.
virtual QString loadNamedStyle(const QString &theURI, bool &resultFlag, bool loadFromLocalDb, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories, Qgis::LoadStyleFlags flags=Qgis::LoadStyleFlags())
Loads a named style from file/local db/datasource db.
Abstract base class for processing algorithms.
Details for layers to load into projects.
int layerSortKey
Optional sorting key for sorting output layers when loading them into a project.
QString groupName
Optional name for a layer tree group under which to place the layer when loading it into a project.
QgsProcessingLayerPostProcessorInterface * postProcessor() const
Layer post-processor.
QgsProject * project
Destination project.
QString outputName
Associated output name from algorithm which generated the layer.
QgsProcessingUtils::LayerHint layerTypeHint
Layer type hint.
void setOutputLayerName(QgsMapLayer *layer) const
Sets a layer name to match this output, respecting any local user settings which affect this name.
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
Returns a map of layers (by ID or datasource) to LayerDetails, to load into the canvas upon completio...
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
QString defaultStyleForOutput(const QString &algorithmId, const QString &outputName) const
Returns the QML path to the default style to use for the specified output from an algorithm,...
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.
virtual void setProgressText(const QString &text)
Sets a progress report text string.
A post-processor for result layers created by Processing algorithms.
virtual void postProcessLayer(QgsMapLayer *layer, const QString &outputName, const QgsProcessingAlgorithm *algorithm)
Post-processes a layer, e.g.
virtual ~QgsProcessingLayerPostProcessor()
Base class for the definition of processing outputs.
Encapsulates settings relating to a feature sink or output raster layer for a processing algorithm.
QgsProcessingLayerPostProcessor * layerPostProcessor()
Returns the post-processor to use for all map layers generated by Processing algorithms.
QgsProcessingDefaultStyleRegistry * defaultStyleRegistry() const
Returns the default style registry, responsible for styling layers generated by Processing tools.
Contains details of a layer result from running an algorithm.
int sortKey
Sort order key for ordering output layers in the layer tree.
QgsLayerTreeGroup * targetLayerTreeGroup
Optional target layer tree group, where the layer should be placed.
QgsProject * destinationProject
Destination QGIS project.
static void configureResultLayerTreeLayer(QgsLayerTreeLayer *layerTreeLayer)
Applies post-processing steps to the QgsLayerTreeLayer created for an algorithm's output.
static QgsMapLayer * addResultLayers(const QVector< QgsProcessingResultsHandler::ResultLayerDetails > &layers, const QgsProcessingContext &context, QgsLayerTreeNode *currentSelectedNode)
Responsible for adding layers created by an algorithm to a project and the project's layer tree in th...
static QgsLayerTreeGroup * layerTreeResultsGroup(const QgsProcessingContext::LayerDetails &layerDetails, const QgsProcessingContext &context)
Returns the destination layer tree group to store results in, or nullptr if there is no specific dest...
virtual QgsMapLayer * handleAddResultLayers(const QgsProcessingResultsHandler::ResultDetails &details, const QgsProcessingContext &context)
Calls addResultLayers(), allowing for extra logic to be injected in subclasses.
virtual void setNewActiveLayer(QgsMapLayer *layer)
Handles setting the new project active layer after processing the results.
static QString determineOutputName(const QString &destinationId, const QgsProcessingContext::LayerDetails &details, const QgsProcessingAlgorithm *algorithm, QgsProcessingContext &context, const QVariantMap ¶meters)
Determines the desired layer name for a map layer output.
bool handleAlgorithmResults(const QgsProcessingAlgorithm *algorithm, QgsProcessingContext &context, const QVariantMap ¶meters, QgsProcessingFeedback *feedback=nullptr)
Handles the results from running an algorithm.
virtual QgsProcessingResultsHandler::ResultDetails handleAlgorithmResultsProtected(const QgsProcessingAlgorithm *algorithm, QgsProcessingContext &context, const QVariantMap ¶meters, QgsProcessingFeedback *feedback)
Handles the results from running an algorithm.
virtual ~QgsProcessingResultsHandler()
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags=QgsProcessing::LayerOptionsFlags())
Interprets a string as a map layer within the supplied context.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
QgsLayerTreeRegistryBridge * layerTreeRegistryBridge() const
Returns pointer to the helper class that synchronizes map layer registry with layer tree.
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
QgsMapLayer * addMapLayer(QgsMapLayer *mapLayer, bool addToLegend=true, bool takeOwnership=true)
Add a layer to the map of loaded layers.
A store for object properties.
QString valueAsString(const QgsExpressionContext &context, const QString &defaultString=QString(), bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a string.
QVariant value(const QgsExpressionContext &context, const QVariant &defaultValue=QVariant(), bool *ok=nullptr) const
Calculates the current value of the property, including any transforms which are set for the property...
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Represents a vector layer which manages a vector based dataset.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
QList< const QgsProcessingOutputDefinition * > QgsProcessingOutputDefinitions
List of processing parameters.
A structure to define the insertion point to the layer tree.
Stores results of running the handleAlgorithmResultsProtected method.
bool succeeded
true if all layers could be successfully loaded
QList< QgsProcessingResultsHandler::ResultLayerDetails > addedLayers
Details of newly added layers.