QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
176  QString name;
177 
179  QString outputName;
180 
187 
193  QgsProcessingLayerPostProcessorInterface *postProcessor() const;
194 
203  void setPostProcessor( QgsProcessingLayerPostProcessorInterface *processor SIP_TRANSFER );
204 
206  QgsProject *project = nullptr;
207 
208  private:
209 
210  // Ideally a unique_ptr, but cannot be due to use within QMap. Is cleaned up by QgsProcessingContext.
211  QgsProcessingLayerPostProcessorInterface *mPostProcessor = nullptr;
212 
213  };
214 
222  QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
223  {
224  return mLayersToLoadOnCompletion;
225  }
226 
236  bool willLoadLayerOnCompletion( const QString &layer ) const
237  {
238  return mLayersToLoadOnCompletion.contains( layer );
239  }
240 
248  void setLayersToLoadOnCompletion( const QMap< QString, QgsProcessingContext::LayerDetails > &layers );
249 
258  void addLayerToLoadOnCompletion( const QString &layer, const QgsProcessingContext::LayerDetails &details );
259 
274  {
275  return mLayersToLoadOnCompletion[ layer ];
276  }
277 
282  QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryCheck; }
283 
290  void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck check );
291 
299 #ifndef SIP_RUN
300  void setInvalidGeometryCallback( const std::function< void( const QgsFeature & ) > &callback ) { mInvalidGeometryCallback = callback; }
301 #else
302  void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
303  % MethodCode
304  Py_BEGIN_ALLOW_THREADS
305 
306  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
307  {
308  SIP_BLOCK_THREADS
309  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
310  SIP_UNBLOCK_THREADS
311  } );
312 
313  Py_END_ALLOW_THREADS
314  % End
315 #endif
316 
324  SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; }
325 
333 #ifndef SIP_RUN
334  void setTransformErrorCallback( const std::function< void( const QgsFeature & ) > &callback ) { mTransformErrorCallback = callback; }
335 #else
336  void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
337  % MethodCode
338  Py_BEGIN_ALLOW_THREADS
339 
340  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
341  {
342  SIP_BLOCK_THREADS
343  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
344  SIP_UNBLOCK_THREADS
345  } );
346 
347  Py_END_ALLOW_THREADS
348  % End
349 #endif
350 
358  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
359 
364  QString defaultEncoding() const { return mDefaultEncoding; }
365 
370  void setDefaultEncoding( const QString &encoding ) { mDefaultEncoding = encoding; }
371 
376  QgsProcessingFeedback *feedback() { return mFeedback; }
377 
386  void setFeedback( QgsProcessingFeedback *feedback ) { mFeedback = feedback; }
387 
392  QThread *thread() { return tempLayerStore.thread(); }
393 
400  void pushToThread( QThread *thread )
401  {
402  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" );
403  tempLayerStore.moveToThread( thread );
404  }
405 
413  void takeResultsFrom( QgsProcessingContext &context );
414 
425  QgsMapLayer *getMapLayer( const QString &identifier );
426 
435  QgsMapLayer *takeResultLayer( const QString &id ) SIP_TRANSFERBACK;
436 
455  QString preferredVectorFormat() const { return mPreferredVectorFormat; }
456 
472  void setPreferredVectorFormat( const QString &format ) { mPreferredVectorFormat = format; }
473 
492  QString preferredRasterFormat() const { return mPreferredRasterFormat; }
493 
509  void setPreferredRasterFormat( const QString &format ) { mPreferredRasterFormat = format; }
510 
511  private:
512 
513  QgsProcessingContext::Flags mFlags = nullptr;
514  QPointer< QgsProject > mProject;
515  QgsCoordinateTransformContext mTransformContext;
517  QgsMapLayerStore tempLayerStore;
518  QgsExpressionContext mExpressionContext;
520  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
521  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
522  QString mDefaultEncoding;
523  QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
524 
525  QPointer< QgsProcessingFeedback > mFeedback;
526 
527  QString mPreferredVectorFormat;
528  QString mPreferredRasterFormat;
529 
530 #ifdef SIP_RUN
532 #endif
533 };
534 
535 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::Flags )
536 
537 
538 
551 {
552  public:
553 
554  virtual ~QgsProcessingLayerPostProcessorInterface() = default;
555 
570  virtual void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
571 
572 };
573 
574 
575 #endif // QGSPROCESSINGPARAMETERS_H
576 
577 
578 
579 
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:79
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.
QString preferredVectorFormat() const
Returns the preferred vector format to use for vector outputs.
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:48
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:126
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.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
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.
QString preferredRasterFormat() const
Returns the preferred raster format to use for vector outputs.
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 setPreferredVectorFormat(const QString &format)
Sets the preferred vector format to use for vector outputs.
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.
void setPreferredRasterFormat(const QString &format)
Sets the preferred raster format to use for vector outputs.