QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgsprocessingcontext.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingcontext.cpp
3  ----------------------
4  begin : April 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsprocessingcontext.h"
19 #include "qgsprocessingutils.h"
20 
22 {
23  for ( auto it = mLayersToLoadOnCompletion.constBegin(); it != mLayersToLoadOnCompletion.constEnd(); ++it )
24  {
25  delete it.value().postProcessor();
26  }
27 }
28 
29 void QgsProcessingContext::setLayersToLoadOnCompletion( const QMap<QString, QgsProcessingContext::LayerDetails> &layers )
30 {
31  for ( auto it = mLayersToLoadOnCompletion.constBegin(); it != mLayersToLoadOnCompletion.constEnd(); ++it )
32  {
33  if ( !layers.contains( it.key() ) || layers.value( it.key() ).postProcessor() != it.value().postProcessor() )
34  delete it.value().postProcessor();
35  }
36  mLayersToLoadOnCompletion = layers;
37 }
38 
40 {
41  if ( mLayersToLoadOnCompletion.contains( layer ) && mLayersToLoadOnCompletion.value( layer ).postProcessor() != details.postProcessor() )
42  delete mLayersToLoadOnCompletion.value( layer ).postProcessor();
43 
44  mLayersToLoadOnCompletion.insert( layer, details );
45 }
46 
48 {
49  mInvalidGeometryCheck = check;
50 
51  switch ( mInvalidGeometryCheck )
52  {
54  {
55  auto callback = []( const QgsFeature & feature )
56  {
57  throw QgsProcessingException( QObject::tr( "Feature (%1) has invalid geometry. Please fix the geometry or change the Processing setting to the \"Ignore invalid input features\" option." ).arg( feature.id() ) );
58  };
59  mInvalidGeometryCallback = callback;
60  break;
61  }
62 
64  {
65  auto callback = [ = ]( const QgsFeature & feature )
66  {
67  if ( mFeedback )
68  mFeedback->reportError( QObject::tr( "Feature (%1) has invalid geometry and has been skipped. Please fix the geometry or change the Processing setting to the \"Ignore invalid input features\" option." ).arg( feature.id() ) );
69  };
70  mInvalidGeometryCallback = callback;
71  break;
72  }
73 
74  default:
75  break;
76  }
77 }
78 
80 {
81  setLayersToLoadOnCompletion( context.mLayersToLoadOnCompletion );
82  context.mLayersToLoadOnCompletion.clear();
83  tempLayerStore.transferLayersFromStore( context.temporaryLayerStore() );
84 }
85 
86 QgsMapLayer *QgsProcessingContext::getMapLayer( const QString &identifier )
87 {
88  return QgsProcessingUtils::mapLayerFromString( identifier, *this, false );
89 }
90 
92 {
93  return tempLayerStore.takeMapLayer( tempLayerStore.mapLayer( id ) );
94 }
95 
96 
97 
99 {
100  return mPostProcessor;
101 }
102 
104 {
105  if ( mPostProcessor && mPostProcessor != processor )
106  delete mPostProcessor;
107 
108  mPostProcessor = processor;
109 }
Base class for all map layer types.
Definition: qgsmaplayer.h:78
void setLayersToLoadOnCompletion(const QMap< QString, QgsProcessingContext::LayerDetails > &layers)
Sets the map of layers (by ID or datasource) to LayerDetails, to load into the canvas upon completion...
An interface for layer post-processing handlers for execution following a processing algorithm operat...
void setPostProcessor(QgsProcessingLayerPostProcessorInterface *processor)
Sets the layer post-processor.
QgsMapLayer * takeResultLayer(const QString &id)
Takes the result map layer with matching id from the context and transfers ownership of it back to th...
Skip any features with invalid geometry. This requires a slow geometry validity check for every featu...
QgsMapLayer * takeMapLayer(QgsMapLayer *layer)
Takes a layer from the store.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
InvalidGeometryCheck
Handling of features with invalid geometries.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType)
Interprets a string as a map layer within the supplied context.
QgsProcessingLayerPostProcessorInterface * postProcessor() const
Layer post-processor.
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution...
void takeResultsFrom(QgsProcessingContext &context)
Takes the results from another context and merges them with the results currently stored in this cont...
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsMapLayer * getMapLayer(const QString &identifier)
Returns a map layer from the context with a matching identifier.
void setInvalidGeometryCheck(QgsFeatureRequest::InvalidGeometryCheck check)
Sets the behavior used for checking invalid geometries in input layers.
Details for layers to load into projects.
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...
void transferLayersFromStore(QgsMapLayerStore *other)
Transfers all the map layers contained within another map layer store and adds them to this store...
QgsMapLayer * mapLayer(const QString &id) const
Retrieve a pointer to a layer by layer id.
Contains information about the context in which a processing algorithm is executed.
Close iterator on encountering any features with invalid geometry. This requires a slow geometry vali...