QGIS API Documentation  3.17.0-Master (df2c9ff931)
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  mUseDefaultInvalidGeometryCallback = other.mUseDefaultInvalidGeometryCallback;
78  mInvalidGeometryCheck = other.mInvalidGeometryCheck;
79  mTransformErrorCallback = other.mTransformErrorCallback;
80  mDefaultEncoding = other.mDefaultEncoding;
81  mFeedback = other.mFeedback;
82  mPreferredVectorFormat = other.mPreferredVectorFormat;
83  mPreferredRasterFormat = other.mPreferredRasterFormat;
84  mEllipsoid = other.mEllipsoid;
85  mDistanceUnit = other.mDistanceUnit;
86  mAreaUnit = other.mAreaUnit;
87  }
88 
93  QgsProcessingContext::Flags flags() const { return mFlags; }
94 
99  void setFlags( QgsProcessingContext::Flags flags ) { mFlags = flags; }
100 
105  QgsProject *project() const { return mProject; }
106 
115  void setProject( QgsProject *project )
116  {
117  mProject = project;
118  if ( mProject )
119  {
120  mTransformContext = mProject->transformContext();
121  if ( mEllipsoid.isEmpty() )
122  mEllipsoid = mProject->ellipsoid();
123  if ( mDistanceUnit == QgsUnitTypes::DistanceUnknownUnit )
124  mDistanceUnit = mProject->distanceUnits();
125  if ( mAreaUnit == QgsUnitTypes::AreaUnknownUnit )
126  mAreaUnit = mProject->areaUnits();
127  }
128  }
129 
133  QgsExpressionContext &expressionContext() { return mExpressionContext; }
134 
138  SIP_SKIP const QgsExpressionContext &expressionContext() const { return mExpressionContext; }
139 
143  void setExpressionContext( const QgsExpressionContext &context ) { mExpressionContext = context; }
144 
149  QgsCoordinateTransformContext transformContext() const { return mTransformContext; }
150 
159  void setTransformContext( const QgsCoordinateTransformContext &context ) { mTransformContext = context; }
160 
167  QString ellipsoid() const;
168 
177  void setEllipsoid( const QString &ellipsoid );
178 
186  QgsUnitTypes::DistanceUnit distanceUnit() const;
187 
197  void setDistanceUnit( QgsUnitTypes::DistanceUnit unit );
198 
206  QgsUnitTypes::AreaUnit areaUnit() const;
207 
217  void setAreaUnit( QgsUnitTypes::AreaUnit areaUnit );
218 
225  QgsDateTimeRange currentTimeRange() const;
226 
233  void setCurrentTimeRange( const QgsDateTimeRange &currentTimeRange );
234 
239  QgsMapLayerStore *temporaryLayerStore() { return &tempLayerStore; }
240 
246  class CORE_EXPORT LayerDetails
247  {
248  public:
249 
253  LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType )
254  : name( name )
255  , outputName( outputName )
256  , layerTypeHint( layerTypeHint )
257  , project( project )
258  {}
259 
261  LayerDetails() = default;
262 
269  QString name;
270 
276  bool forceName = false;
277 
281  QString outputName;
282 
289 
295  QgsProcessingLayerPostProcessorInterface *postProcessor() const;
296 
305  void setPostProcessor( QgsProcessingLayerPostProcessorInterface *processor SIP_TRANSFER );
306 
312  void setOutputLayerName( QgsMapLayer *layer ) const;
313 
315  QgsProject *project = nullptr;
316 
317  private:
318 
319  // Ideally a unique_ptr, but cannot be due to use within QMap. Is cleaned up by QgsProcessingContext.
320  QgsProcessingLayerPostProcessorInterface *mPostProcessor = nullptr;
321 
322  };
323 
331  QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
332  {
333  return mLayersToLoadOnCompletion;
334  }
335 
345  bool willLoadLayerOnCompletion( const QString &layer ) const
346  {
347  return mLayersToLoadOnCompletion.contains( layer );
348  }
349 
357  void setLayersToLoadOnCompletion( const QMap< QString, QgsProcessingContext::LayerDetails > &layers );
358 
367  void addLayerToLoadOnCompletion( const QString &layer, const QgsProcessingContext::LayerDetails &details );
368 
383  {
384  return mLayersToLoadOnCompletion[ layer ];
385  }
386 
391  QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryCheck; }
392 
399  void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck check );
400 
408 #ifndef SIP_RUN
409  void setInvalidGeometryCallback( const std::function< void( const QgsFeature & ) > &callback ) { mInvalidGeometryCallback = callback; mUseDefaultInvalidGeometryCallback = false; }
410 #else
411  void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
412  % MethodCode
413  Py_BEGIN_ALLOW_THREADS
414 
415  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
416  {
417  SIP_BLOCK_THREADS
418  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
419  SIP_UNBLOCK_THREADS
420  } );
421 
422  Py_END_ALLOW_THREADS
423  % End
424 #endif
425 
433  SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback( QgsFeatureSource *source = nullptr ) const;
434 
440  SIP_SKIP std::function< void( const QgsFeature & ) > defaultInvalidGeometryCallbackForCheck( QgsFeatureRequest::InvalidGeometryCheck check, QgsFeatureSource *source = nullptr ) const;
441 
449 #ifndef SIP_RUN
450  void setTransformErrorCallback( const std::function< void( const QgsFeature & ) > &callback ) { mTransformErrorCallback = callback; }
451 #else
452  void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
453  % MethodCode
454  Py_BEGIN_ALLOW_THREADS
455 
456  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
457  {
458  SIP_BLOCK_THREADS
459  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
460  SIP_UNBLOCK_THREADS
461  } );
462 
463  Py_END_ALLOW_THREADS
464  % End
465 #endif
466 
474  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
475 
480  QString defaultEncoding() const { return mDefaultEncoding; }
481 
486  void setDefaultEncoding( const QString &encoding ) { mDefaultEncoding = encoding; }
487 
492  QgsProcessingFeedback *feedback() { return mFeedback; }
493 
502  void setFeedback( QgsProcessingFeedback *feedback ) { mFeedback = feedback; }
503 
508  QThread *thread() { return tempLayerStore.thread(); }
509 
516  void pushToThread( QThread *thread )
517  {
518  // cppcheck-suppress assertWithSideEffect
519  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" );
520  tempLayerStore.moveToThread( thread );
521  }
522 
530  void takeResultsFrom( QgsProcessingContext &context );
531 
542  QgsMapLayer *getMapLayer( const QString &identifier );
543 
552  QgsMapLayer *takeResultLayer( const QString &id ) SIP_TRANSFERBACK;
553 
572  QString preferredVectorFormat() const { return mPreferredVectorFormat; }
573 
589  void setPreferredVectorFormat( const QString &format ) { mPreferredVectorFormat = format; }
590 
609  QString preferredRasterFormat() const { return mPreferredRasterFormat; }
610 
626  void setPreferredRasterFormat( const QString &format ) { mPreferredRasterFormat = format; }
627 
628  private:
629 
630  QgsProcessingContext::Flags mFlags = QgsProcessingContext::Flags();
631  QPointer< QgsProject > mProject;
632  QgsCoordinateTransformContext mTransformContext;
633 
634  QString mEllipsoid;
637 
638  QgsDateTimeRange mCurrentTimeRange;
639 
641  QgsMapLayerStore tempLayerStore;
642  QgsExpressionContext mExpressionContext;
643 
645  bool mUseDefaultInvalidGeometryCallback = true;
646  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
647 
648  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
649  QString mDefaultEncoding;
650  QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
651 
652  QPointer< QgsProcessingFeedback > mFeedback;
653 
654  QString mPreferredVectorFormat;
655  QString mPreferredRasterFormat;
656 
657 #ifdef SIP_RUN
659 #endif
660 };
661 
662 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::Flags )
663 
664 
665 
678 {
679  public:
680 
681  virtual ~QgsProcessingLayerPostProcessorInterface() = default;
682 
697  virtual void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
698 
699 };
700 
701 
702 #endif // QGSPROCESSINGPARAMETERS_H
703 
704 
705 
706 
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:84
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...
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
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.
Unknown areal unit.
Definition: qgsunittypes.h:106
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, possibly for 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:98
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.
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:67
QString preferredRasterFormat() const
Returns the preferred raster format to use for vector outputs.
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:105
Unknown distance unit.
Definition: qgsunittypes.h:78
QString defaultEncoding() const
Returns the default encoding to use for newly created files.
An interface for objects which provide features via a getFeatures method.
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.
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.
AreaUnit
Units of area.
Definition: qgsunittypes.h:93
Flag
Flags that affect how processing algorithms are run.
void setPreferredRasterFormat(const QString &format)
Sets the preferred raster format to use for vector outputs.