QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
60  QgsProcessingContext( const QgsProcessingContext &other ) = delete;
62  QgsProcessingContext &operator=( const QgsProcessingContext &other ) = delete;
63 
65 
71  {
72  mFlags = other.mFlags;
73  mProject = other.mProject;
74  mTransformContext = other.mTransformContext;
75  mExpressionContext = other.mExpressionContext;
76  mInvalidGeometryCallback = other.mInvalidGeometryCallback;
77  mInvalidGeometryCheck = other.mInvalidGeometryCheck;
78  mTransformErrorCallback = other.mTransformErrorCallback;
79  mDefaultEncoding = other.mDefaultEncoding;
80  mFeedback = other.mFeedback;
81  }
82 
87  QgsProcessingContext::Flags flags() const { return mFlags; }
88 
93  void setFlags( QgsProcessingContext::Flags flags ) { mFlags = flags; }
94 
99  QgsProject *project() const { return mProject; }
100 
109  void setProject( QgsProject *project )
110  {
111  mProject = project;
112  if ( mProject )
113  mTransformContext = mProject->transformContext();
114  }
115 
119  QgsExpressionContext &expressionContext() { return mExpressionContext; }
120 
124  SIP_SKIP const QgsExpressionContext &expressionContext() const { return mExpressionContext; }
125 
129  void setExpressionContext( const QgsExpressionContext &context ) { mExpressionContext = context; }
130 
135  QgsCoordinateTransformContext transformContext() const { return mTransformContext; }
136 
145  void setTransformContext( const QgsCoordinateTransformContext &context ) { mTransformContext = context; }
146 
151  QgsMapLayerStore *temporaryLayerStore() { return &tempLayerStore; }
152 
158  class CORE_EXPORT LayerDetails
159  {
160  public:
161 
165  LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType )
166  : name( name )
167  , outputName( outputName )
168  , layerTypeHint( layerTypeHint )
169  , project( project )
170  {}
171 
173  LayerDetails() = default;
174 
181  QString name;
182 
186  QString outputName;
187 
194 
200  QgsProcessingLayerPostProcessorInterface *postProcessor() const;
201 
210  void setPostProcessor( QgsProcessingLayerPostProcessorInterface *processor SIP_TRANSFER );
211 
217  void setOutputLayerName( QgsMapLayer *layer ) const;
218 
220  QgsProject *project = nullptr;
221 
222  private:
223 
224  // Ideally a unique_ptr, but cannot be due to use within QMap. Is cleaned up by QgsProcessingContext.
225  QgsProcessingLayerPostProcessorInterface *mPostProcessor = nullptr;
226 
227  };
228 
236  QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
237  {
238  return mLayersToLoadOnCompletion;
239  }
240 
250  bool willLoadLayerOnCompletion( const QString &layer ) const
251  {
252  return mLayersToLoadOnCompletion.contains( layer );
253  }
254 
262  void setLayersToLoadOnCompletion( const QMap< QString, QgsProcessingContext::LayerDetails > &layers );
263 
272  void addLayerToLoadOnCompletion( const QString &layer, const QgsProcessingContext::LayerDetails &details );
273 
288  {
289  return mLayersToLoadOnCompletion[ layer ];
290  }
291 
296  QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryCheck; }
297 
304  void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck check );
305 
313 #ifndef SIP_RUN
314  void setInvalidGeometryCallback( const std::function< void( const QgsFeature & ) > &callback ) { mInvalidGeometryCallback = callback; }
315 #else
316  void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
317  % MethodCode
318  Py_BEGIN_ALLOW_THREADS
319 
320  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
321  {
322  SIP_BLOCK_THREADS
323  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
324  SIP_UNBLOCK_THREADS
325  } );
326 
327  Py_END_ALLOW_THREADS
328  % End
329 #endif
330 
338  SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; }
339 
345  SIP_SKIP std::function< void( const QgsFeature & ) > defaultInvalidGeometryCallbackForCheck( QgsFeatureRequest::InvalidGeometryCheck check ) const;
346 
354 #ifndef SIP_RUN
355  void setTransformErrorCallback( const std::function< void( const QgsFeature & ) > &callback ) { mTransformErrorCallback = callback; }
356 #else
357  void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
358  % MethodCode
359  Py_BEGIN_ALLOW_THREADS
360 
361  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
362  {
363  SIP_BLOCK_THREADS
364  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
365  SIP_UNBLOCK_THREADS
366  } );
367 
368  Py_END_ALLOW_THREADS
369  % End
370 #endif
371 
379  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
380 
385  QString defaultEncoding() const { return mDefaultEncoding; }
386 
391  void setDefaultEncoding( const QString &encoding ) { mDefaultEncoding = encoding; }
392 
397  QgsProcessingFeedback *feedback() { return mFeedback; }
398 
407  void setFeedback( QgsProcessingFeedback *feedback ) { mFeedback = feedback; }
408 
413  QThread *thread() { return tempLayerStore.thread(); }
414 
421  void pushToThread( QThread *thread )
422  {
423  // cppcheck-suppress assertWithSideEffect
424  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" );
425  tempLayerStore.moveToThread( thread );
426  }
427 
435  void takeResultsFrom( QgsProcessingContext &context );
436 
447  QgsMapLayer *getMapLayer( const QString &identifier );
448 
457  QgsMapLayer *takeResultLayer( const QString &id ) SIP_TRANSFERBACK;
458 
477  QString preferredVectorFormat() const { return mPreferredVectorFormat; }
478 
494  void setPreferredVectorFormat( const QString &format ) { mPreferredVectorFormat = format; }
495 
514  QString preferredRasterFormat() const { return mPreferredRasterFormat; }
515 
531  void setPreferredRasterFormat( const QString &format ) { mPreferredRasterFormat = format; }
532 
533  private:
534 
535  QgsProcessingContext::Flags mFlags = QgsProcessingContext::Flags();
536  QPointer< QgsProject > mProject;
537  QgsCoordinateTransformContext mTransformContext;
539  QgsMapLayerStore tempLayerStore;
540  QgsExpressionContext mExpressionContext;
542  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
543  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
544  QString mDefaultEncoding;
545  QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
546 
547  QPointer< QgsProcessingFeedback > mFeedback;
548 
549  QString mPreferredVectorFormat;
550  QString mPreferredRasterFormat;
551 
552 #ifdef SIP_RUN
554 #endif
555 };
556 
557 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::Flags )
558 
559 
560 
573 {
574  public:
575 
576  virtual ~QgsProcessingLayerPostProcessorInterface() = default;
577 
592  virtual void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
593 
594 };
595 
596 
597 #endif // QGSPROCESSINGPARAMETERS_H
598 
599 
600 
601 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
QgsProcessingContext::layersToLoadOnCompletion
QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
Returns a map of layers (by ID or datasource) to LayerDetails, to load into the canvas upon completio...
Definition: qgsprocessingcontext.h:236
QgsCoordinateTransformContext
Definition: qgscoordinatetransformcontext.h:57
qgsfeaturerequest.h
QgsProcessingContext::willLoadLayerOnCompletion
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...
Definition: qgsprocessingcontext.h:250
QgsProcessingContext::project
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Definition: qgsprocessingcontext.h:99
QgsProcessingContext::preferredVectorFormat
QString preferredVectorFormat() const
Returns the preferred vector format to use for vector outputs.
Definition: qgsprocessingcontext.h:477
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsProject::transformContext
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:99
qgis.h
QgsProcessingContext::thread
QThread * thread()
Returns the thread in which the context lives.
Definition: qgsprocessingcontext.h:413
QgsProcessingUtils::LayerHint
LayerHint
Layer type hints.
Definition: qgsprocessingutils.h:136
QgsFeatureRequest::InvalidGeometryCheck
InvalidGeometryCheck
Handling of features with invalid geometries.
Definition: qgsfeaturerequest.h:112
QgsProject
Definition: qgsproject.h:92
SIP_TRANSFERBACK
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
QgsProcessingContext::setTransformErrorCallback
void setTransformErrorCallback(const std::function< void(const QgsFeature &) > &callback)
Sets a callback function to use when encountering a transform error when iterating features.
Definition: qgsprocessingcontext.h:355
QgsMapLayerStore
Definition: qgsmaplayerstore.h:35
qgsexpressioncontext.h
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsProcessingContext::pushToThread
void pushToThread(QThread *thread)
Pushes the thread affinity for the context (including all layers contained in the temporaryLayerStore...
Definition: qgsprocessingcontext.h:421
QgsProcessingContext::LayerDetails::LayerDetails
LayerDetails(const QString &name, QgsProject *project, const QString &outputName=QString(), QgsProcessingUtils::LayerHint layerTypeHint=QgsProcessingUtils::LayerHint::UnknownType)
Constructor for LayerDetails.
Definition: qgsprocessingcontext.h:165
QgsProcessingContext::invalidGeometryCheck
QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const
Returns the behavior used for checking invalid geometries in input layers.
Definition: qgsprocessingcontext.h:296
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsProcessingContext::transformErrorCallback
std::function< void(const QgsFeature &) > transformErrorCallback() const
Returns the callback function to use when encountering a transform error when iterating features.
Definition: qgsprocessingcontext.h:379
QgsProcessingContext::defaultEncoding
QString defaultEncoding() const
Returns the default encoding to use for newly created files.
Definition: qgsprocessingcontext.h:385
QgsProcessingContext::setDefaultEncoding
void setDefaultEncoding(const QString &encoding)
Sets the default encoding to use for newly created files.
Definition: qgsprocessingcontext.h:391
QgsProcessingContext::LayerDetails::name
QString name
Friendly name for layer, possibly for use when loading layer into project.
Definition: qgsprocessingcontext.h:181
QgsProcessingContext::copyThreadSafeSettings
void copyThreadSafeSettings(const QgsProcessingContext &other)
Copies all settings which are safe for use across different threads from other to this context.
Definition: qgsprocessingcontext.h:70
QgsProcessingContext::setInvalidGeometryCallback
void setInvalidGeometryCallback(const std::function< void(const QgsFeature &) > &callback)
Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is s...
Definition: qgsprocessingcontext.h:314
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsProcessingContext::preferredRasterFormat
QString preferredRasterFormat() const
Returns the preferred raster format to use for vector outputs.
Definition: qgsprocessingcontext.h:514
qgsprocessingfeedback.h
QgsProcessingContext::setExpressionContext
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
Definition: qgsprocessingcontext.h:129
QgsProcessingContext::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Definition: qgsprocessingcontext.h:135
QgsProcessingLayerPostProcessorInterface
An interface for layer post-processing handlers for execution following a processing algorithm operat...
Definition: qgsprocessingcontext.h:572
QgsProcessingContext::setTransformContext
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context.
Definition: qgsprocessingcontext.h:145
QgsProcessingContext::Flag
Flag
Flags that affect how processing algorithms are run.
Definition: qgsprocessingcontext.h:48
QgsProcessingContext::LayerDetails
Details for layers to load into projects.
Definition: qgsprocessingcontext.h:158
QgsProcessingContext::expressionContext
const QgsExpressionContext & expressionContext() const
Returns the expression context.
Definition: qgsprocessingcontext.h:124
QgsProcessingContext::temporaryLayerStore
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
Definition: qgsprocessingcontext.h:151
QgsProcessingContext::feedback
QgsProcessingFeedback * feedback()
Returns the associated feedback object.
Definition: qgsprocessingcontext.h:397
QgsProcessingContext::setFeedback
void setFeedback(QgsProcessingFeedback *feedback)
Sets an associated feedback object.
Definition: qgsprocessingcontext.h:407
qgsprocessingutils.h
QgsProcessingContext::LayerDetails::outputName
QString outputName
Associated output name from algorithm which generated the layer.
Definition: qgsprocessingcontext.h:186
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsProcessingContext::layerToLoadOnCompletionDetails
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...
Definition: qgsprocessingcontext.h:287
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:119
QgsProcessingContext::flags
QgsProcessingContext::Flags flags() const
Returns any flags set in the context.
Definition: qgsprocessingcontext.h:87
QgsFeatureRequest::GeometryNoCheck
@ GeometryNoCheck
No invalid geometry checking.
Definition: qgsfeaturerequest.h:114
QgsProcessingContext::setPreferredVectorFormat
void setPreferredVectorFormat(const QString &format)
Sets the preferred vector format to use for vector outputs.
Definition: qgsprocessingcontext.h:494
qgsexception.h
QgsFeature
Definition: qgsfeature.h:55
qgsmaplayerlistutils.h
QgsProcessingContext::setProject
void setProject(QgsProject *project)
Sets the project in which the algorithm will be executed.
Definition: qgsprocessingcontext.h:109
QgsProcessingContext::invalidGeometryCallback
std::function< void(const QgsFeature &) > invalidGeometryCallback() const
Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck()...
Definition: qgsprocessingcontext.h:338
QgsProcessingContext::setPreferredRasterFormat
void setPreferredRasterFormat(const QString &format)
Sets the preferred raster format to use for vector outputs.
Definition: qgsprocessingcontext.h:531
qgsproject.h
QgsProcessingContext::setFlags
void setFlags(QgsProcessingContext::Flags flags)
Sets flags for the context.
Definition: qgsprocessingcontext.h:93
QgsProcessingUtils::LayerHint::UnknownType
@ UnknownType
Unknown layer type.