QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 #include "qgsprocessingutils.h"
30 
32 
43 class CORE_EXPORT QgsProcessingContext
44 {
45  public:
46 
48  enum Flag
49  {
50  // UseSelectionIfPresent = 1 << 0,
51  };
52  Q_DECLARE_FLAGS( Flags, Flag )
53 
54 
58  {
59  auto callback = [ = ]( const QgsFeature & feature )
60  {
61  if ( mFeedback )
62  mFeedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( feature.id() ) );
63  };
64  mTransformErrorCallback = callback;
65  }
66 
68  QgsProcessingContext( const QgsProcessingContext &other ) = delete;
70  QgsProcessingContext &operator=( const QgsProcessingContext &other ) = delete;
71 
73 
79  {
80  mFlags = other.mFlags;
81  mProject = other.mProject;
82  mTransformContext = other.mTransformContext;
83  mExpressionContext = other.mExpressionContext;
84  mInvalidGeometryCallback = other.mInvalidGeometryCallback;
85  mInvalidGeometryCheck = other.mInvalidGeometryCheck;
86  mTransformErrorCallback = other.mTransformErrorCallback;
87  mDefaultEncoding = other.mDefaultEncoding;
88  mFeedback = other.mFeedback;
89  }
90 
95  QgsProcessingContext::Flags flags() const { return mFlags; }
96 
101  void setFlags( QgsProcessingContext::Flags flags ) { mFlags = flags; }
102 
107  QgsProject *project() const { return mProject; }
108 
117  void setProject( QgsProject *project )
118  {
119  mProject = project;
120  if ( mProject )
121  mTransformContext = mProject->transformContext();
122  }
123 
127  QgsExpressionContext &expressionContext() { return mExpressionContext; }
128 
132  SIP_SKIP const QgsExpressionContext &expressionContext() const { return mExpressionContext; }
133 
137  void setExpressionContext( const QgsExpressionContext &context ) { mExpressionContext = context; }
138 
143  QgsCoordinateTransformContext transformContext() const { return mTransformContext; }
144 
153  void setTransformContext( const QgsCoordinateTransformContext &context ) { mTransformContext = context; }
154 
159  QgsMapLayerStore *temporaryLayerStore() { return &tempLayerStore; }
160 
166  class CORE_EXPORT LayerDetails
167  {
168  public:
169 
173  LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType )
174  : name( name )
175  , outputName( outputName )
176  , layerTypeHint( layerTypeHint )
177  , project( project )
178  {}
179 
181  LayerDetails() = default;
182 
184  QString name;
185 
187  QString outputName;
188 
195 
201  QgsProcessingLayerPostProcessorInterface *postProcessor() const;
202 
211  void setPostProcessor( QgsProcessingLayerPostProcessorInterface *processor SIP_TRANSFER );
212 
214  QgsProject *project = nullptr;
215 
216  private:
217 
218  // Ideally a unique_ptr, but cannot be due to use within QMap. Is cleaned up by QgsProcessingContext.
219  QgsProcessingLayerPostProcessorInterface *mPostProcessor = nullptr;
220 
221  };
222 
230  QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
231  {
232  return mLayersToLoadOnCompletion;
233  }
234 
244  bool willLoadLayerOnCompletion( const QString &layer ) const
245  {
246  return mLayersToLoadOnCompletion.contains( layer );
247  }
248 
256  void setLayersToLoadOnCompletion( const QMap< QString, QgsProcessingContext::LayerDetails > &layers );
257 
266  void addLayerToLoadOnCompletion( const QString &layer, const QgsProcessingContext::LayerDetails &details );
267 
282  {
283  return mLayersToLoadOnCompletion[ layer ];
284  }
285 
290  QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryCheck; }
291 
298  void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck check );
299 
307 #ifndef SIP_RUN
308  void setInvalidGeometryCallback( const std::function< void( const QgsFeature & ) > &callback ) { mInvalidGeometryCallback = callback; }
309 #else
310  void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
311  % MethodCode
312  Py_BEGIN_ALLOW_THREADS
313 
314  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
315  {
316  SIP_BLOCK_THREADS
317  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
318  SIP_UNBLOCK_THREADS
319  } );
320 
321  Py_END_ALLOW_THREADS
322  % End
323 #endif
324 
332  SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; }
333 
341 #ifndef SIP_RUN
342  void setTransformErrorCallback( const std::function< void( const QgsFeature & ) > &callback ) { mTransformErrorCallback = callback; }
343 #else
344  void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
345  % MethodCode
346  Py_BEGIN_ALLOW_THREADS
347 
348  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
349  {
350  SIP_BLOCK_THREADS
351  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
352  SIP_UNBLOCK_THREADS
353  } );
354 
355  Py_END_ALLOW_THREADS
356  % End
357 #endif
358 
366  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
367 
372  QString defaultEncoding() const { return mDefaultEncoding; }
373 
378  void setDefaultEncoding( const QString &encoding ) { mDefaultEncoding = encoding; }
379 
384  QgsProcessingFeedback *feedback() { return mFeedback; }
385 
394  void setFeedback( QgsProcessingFeedback *feedback ) { mFeedback = feedback; }
395 
400  QThread *thread() { return tempLayerStore.thread(); }
401 
408  void pushToThread( QThread *thread )
409  {
410  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" );
411  tempLayerStore.moveToThread( thread );
412  }
413 
421  void takeResultsFrom( QgsProcessingContext &context );
422 
433  QgsMapLayer *getMapLayer( const QString &identifier );
434 
443  QgsMapLayer *takeResultLayer( const QString &id ) SIP_TRANSFERBACK;
444 
445  private:
446 
447  QgsProcessingContext::Flags mFlags = nullptr;
448  QPointer< QgsProject > mProject;
449  QgsCoordinateTransformContext mTransformContext;
451  QgsMapLayerStore tempLayerStore;
452  QgsExpressionContext mExpressionContext;
454  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
455  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
456  QString mDefaultEncoding;
457  QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
458 
459  QPointer< QgsProcessingFeedback > mFeedback;
460 
461 #ifdef SIP_RUN
463 #endif
464 };
465 
466 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::Flags )
467 
468 
469 
482 {
483  public:
484 
485  virtual ~QgsProcessingLayerPostProcessorInterface() = default;
486 
501  virtual void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
502 
503 };
504 
505 
506 #endif // QGSPROCESSINGPARAMETERS_H
507 
508 
509 
510 
void setProject(QgsProject *project)
Sets the project in which the algorithm will be executed.
bool willLoadLayerOnCompletion(const QString &layer) const
Returns true if the given layer (by ID or datasource) will be loaded into the current project upon co...
Base class for all map layer types.
Definition: qgsmaplayer.h:78
Base class for providing feedback from a processing algorithm.
QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
Returns a map of layers (by ID or datasource) to LayerDetails, to load into the canvas upon completio...
An interface for layer post-processing handlers for execution following a processing algorithm operat...
LayerHint
Layer type hints.
void setInvalidGeometryCallback(const std::function< void(const QgsFeature &) > &callback)
Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is s...
LayerDetails(const QString &name, QgsProject *project, const QString &outputName=QString(), QgsProcessingUtils::LayerHint layerTypeHint=QgsProcessingUtils::LayerHint::UnknownType)
Constructor for LayerDetails.
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:55
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
InvalidGeometryCheck
Handling of features with invalid geometries.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
QString name
Friendly name for layer, to use when loading layer into project.
std::function< void(const QgsFeature &) > transformErrorCallback() const
Returns the callback function to use when encountering a transform error when iterating features...
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.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
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:89
No invalid geometry checking.
void setFeedback(QgsProcessingFeedback *feedback)
Sets an associated feedback object.
QString outputName
Associated output name from algorithm which generated the layer.
Contains information about the context in which a coordinate transform is executed.
Details for layers to load into projects.
QgsExpressionContext & expressionContext()
Returns the expression context.
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:96
QString defaultEncoding() const
Returns the default encoding to use for newly created files.
QgsProcessingContext::LayerDetails & layerToLoadOnCompletionDetails(const QString &layer)
Returns a reference to the details for a given layer which is loaded on completion of the algorithm o...
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context.
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.