QGIS API Documentation  3.0.2-Girona (307d082)
qgsprocessingcontext.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingcontext.h
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 #ifndef QGSPROCESSINGCONTEXT_H
19 #define QGSPROCESSINGCONTEXT_H
20 
21 #include "qgis_core.h"
22 #include "qgis.h"
23 #include "qgsproject.h"
24 #include "qgsexpressioncontext.h"
25 #include "qgsfeaturerequest.h"
26 #include "qgsmaplayerlistutils.h"
27 #include "qgsexception.h"
28 #include "qgsprocessingfeedback.h"
29 
40 class CORE_EXPORT QgsProcessingContext
41 {
42  public:
43 
45  enum Flag
46  {
47  // UseSelectionIfPresent = 1 << 0,
48  };
49  Q_DECLARE_FLAGS( Flags, Flag )
50 
51 
55  {
56  auto callback = [ = ]( const QgsFeature & feature )
57  {
58  if ( mFeedback )
59  mFeedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( feature.id() ) );
60  };
61  mTransformErrorCallback = callback;
62  }
63 
65  QgsProcessingContext( const QgsProcessingContext &other ) = delete;
67  QgsProcessingContext &operator=( const QgsProcessingContext &other ) = delete;
68 
74  {
75  mFlags = other.mFlags;
76  mProject = other.mProject;
77  mTransformContext = other.mTransformContext;
78  mExpressionContext = other.mExpressionContext;
79  mInvalidGeometryCallback = other.mInvalidGeometryCallback;
80  mInvalidGeometryCheck = other.mInvalidGeometryCheck;
81  mTransformErrorCallback = other.mTransformErrorCallback;
82  mDefaultEncoding = other.mDefaultEncoding;
83  mFeedback = other.mFeedback;
84  }
85 
90  QgsProcessingContext::Flags flags() const { return mFlags; }
91 
96  void setFlags( QgsProcessingContext::Flags flags ) { mFlags = flags; }
97 
102  QgsProject *project() const { return mProject; }
103 
112  void setProject( QgsProject *project )
113  {
114  mProject = project;
115  if ( mProject )
116  mTransformContext = mProject->transformContext();
117  }
118 
122  QgsExpressionContext &expressionContext() { return mExpressionContext; }
123 
127  SIP_SKIP const QgsExpressionContext &expressionContext() const { return mExpressionContext; }
128 
132  void setExpressionContext( const QgsExpressionContext &context ) { mExpressionContext = context; }
133 
138  QgsCoordinateTransformContext transformContext() const { return mTransformContext; }
139 
148  void setTransformContext( const QgsCoordinateTransformContext &context ) { mTransformContext = context; }
149 
154  QgsMapLayerStore *temporaryLayerStore() { return &tempLayerStore; }
155 
158  {
159 
163  LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString() )
164  : name( name )
165  , outputName( outputName )
166  , project( project )
167  {}
168 
170  QString name;
171 
173  QString outputName;
174 
176  QgsProject *project = nullptr;
177 
178  };
179 
185  QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
186  {
187  return mLayersToLoadOnCompletion;
188  }
189 
195  void setLayersToLoadOnCompletion( const QMap< QString, QgsProcessingContext::LayerDetails > &layers )
196  {
197  mLayersToLoadOnCompletion = layers;
198  }
199 
206  void addLayerToLoadOnCompletion( const QString &layer, const QgsProcessingContext::LayerDetails &details )
207  {
208  mLayersToLoadOnCompletion.insert( layer, details );
209  }
210 
215  QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryCheck; }
216 
223  void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck check );
224 
232 #ifndef SIP_RUN
233  void setInvalidGeometryCallback( const std::function< void( const QgsFeature & ) > &callback ) { mInvalidGeometryCallback = callback; }
234 #else
235  void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
236  % MethodCode
237  Py_BEGIN_ALLOW_THREADS
238 
239  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
240  {
241  SIP_BLOCK_THREADS
242  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
243  SIP_UNBLOCK_THREADS
244  } );
245 
246  Py_END_ALLOW_THREADS
247  % End
248 #endif
249 
257  SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; }
258 
266 #ifndef SIP_RUN
267  void setTransformErrorCallback( const std::function< void( const QgsFeature & ) > &callback ) { mTransformErrorCallback = callback; }
268 #else
269  void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
270  % MethodCode
271  Py_BEGIN_ALLOW_THREADS
272 
273  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
274  {
275  SIP_BLOCK_THREADS
276  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
277  SIP_UNBLOCK_THREADS
278  } );
279 
280  Py_END_ALLOW_THREADS
281  % End
282 #endif
283 
292  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
293 
298  QString defaultEncoding() const { return mDefaultEncoding; }
299 
304  void setDefaultEncoding( const QString &encoding ) { mDefaultEncoding = encoding; }
305 
310  QgsProcessingFeedback *feedback() { return mFeedback; }
311 
320  void setFeedback( QgsProcessingFeedback *feedback ) { mFeedback = feedback; }
321 
326  QThread *thread() { return tempLayerStore.thread(); }
327 
334  void pushToThread( QThread *thread )
335  {
336  Q_ASSERT_X( QThread::currentThread() == QgsProcessingContext::thread(), "QgsProcessingContext::pushToThread", "Cannot push context to another thread unless the current thread matches the existing context thread affinity" );
337  tempLayerStore.moveToThread( thread );
338  }
339 
347  void takeResultsFrom( QgsProcessingContext &context );
348 
359  QgsMapLayer *getMapLayer( const QString &identifier );
360 
369  QgsMapLayer *takeResultLayer( const QString &id ) SIP_TRANSFERBACK;
370 
371  private:
372 
373  QgsProcessingContext::Flags mFlags = nullptr;
374  QPointer< QgsProject > mProject;
375  QgsCoordinateTransformContext mTransformContext;
377  QgsMapLayerStore tempLayerStore;
378  QgsExpressionContext mExpressionContext;
380  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
381  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
382  QString mDefaultEncoding;
383  QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
384 
385  QPointer< QgsProcessingFeedback > mFeedback;
386 
387 #ifdef SIP_RUN
389 #endif
390 };
391 
392 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::Flags )
393 
394 #endif // QGSPROCESSINGPARAMETERS_H
395 
396 
397 
398 
void setProject(QgsProject *project)
Sets the project in which the algorithm will be executed.
Base class for all map layer types.
Definition: qgsmaplayer.h:56
Base class for providing feedback from a processing algorithm.
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...
QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
Returns a map of layers (by ID or datasource) to LayerDetails, to load into the canvas upon completio...
void setInvalidGeometryCallback(const std::function< void(const QgsFeature &) > &callback)
Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is s...
QThread * thread()
Returns the thread in which the context lives.
void pushToThread(QThread *thread)
Pushes the thread affinity for the context (including all layers contained in the temporaryLayerStore...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
InvalidGeometryCheck
Handling of features with invalid geometries.
QgsCoordinateTransformContext transformContext() const
Returns a copy of the project&#39;s coordinate transform context, which stores various information regard...
Definition: qgsproject.cpp:474
QgsProject * project() const
Returns the project in which the algorithm is being executed.
std::function< void(const QgsFeature &) > transformErrorCallback() const
Returns the callback function to use when encountering a transform error when iterating features...
QString outputName
Associated output name from algorithm which generated the layer.
QgsProcessingFeedback * feedback()
Returns the associated feedback object.
void setTransformErrorCallback(const std::function< void(const QgsFeature &) > &callback)
Sets a callback function to use when encountering a transform error when iterating features...
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution...
#define SIP_SKIP
Definition: qgis_sip.h:119
QgsProcessingContext::Flags flags() const
Returns any flags set in the context.
void copyThreadSafeSettings(const QgsProcessingContext &other)
Copies all settings which are safe for use across different threads from other to this context...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const
Returns the behavior used for checking invalid geometries in input layers.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
const QgsExpressionContext & expressionContext() const
Returns the expression context.
void setDefaultEncoding(const QString &encoding)
Sets the default encoding to use for newly created files.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Reads and writes project states.
Definition: qgsproject.h:82
QString name
Friendly name for layer, to use when loading layer into project.
LayerDetails(const QString &name, QgsProject *project, const QString &outputName=QString())
Constructor for LayerDetails.
No invalid geometry checking.
void setFeedback(QgsProcessingFeedback *feedback)
Sets an associated feedback object.
Contains information about the context in which a coordinate transform is executed.
QgsExpressionContext & expressionContext()
Returns the expression 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...
QString defaultEncoding() const
Returns the default encoding to use for newly created files.
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context.
Details for layers to load into projects.
void setFlags(QgsProcessingContext::Flags flags)
Sets flags for the context.
std::function< void(const QgsFeature &) > invalidGeometryCallback() const
Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck()...
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
Contains information about the context in which a processing algorithm is executed.
Flag
Flags that affect how processing algorithms are run.