QGIS API Documentation  3.21.0-Master (5b68dc587e)
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 "qgsexception.h"
27 #include "qgsprocessingfeedback.h"
28 #include "qgsprocessingutils.h"
29 
30 
31 #include <QThread>
32 #include <QPointer>
33 
35 
46 class CORE_EXPORT QgsProcessingContext
47 {
48  public:
49 
51  enum Flag
52  {
53  // UseSelectionIfPresent = 1 << 0,
54  };
55  Q_DECLARE_FLAGS( Flags, Flag )
56 
57 
62  enum LogLevel
63  {
64  DefaultLevel = 0,
66  };
67 
72 
74  QgsProcessingContext( const QgsProcessingContext &other ) = delete;
77 
79 
85  {
86  mFlags = other.mFlags;
87  mProject = other.mProject;
88  mTransformContext = other.mTransformContext;
89  mExpressionContext = other.mExpressionContext;
90  mInvalidGeometryCallback = other.mInvalidGeometryCallback;
91  mUseDefaultInvalidGeometryCallback = other.mUseDefaultInvalidGeometryCallback;
92  mInvalidGeometryCheck = other.mInvalidGeometryCheck;
93  mTransformErrorCallback = other.mTransformErrorCallback;
94  mDefaultEncoding = other.mDefaultEncoding;
95  mFeedback = other.mFeedback;
96  mPreferredVectorFormat = other.mPreferredVectorFormat;
97  mPreferredRasterFormat = other.mPreferredRasterFormat;
98  mEllipsoid = other.mEllipsoid;
99  mDistanceUnit = other.mDistanceUnit;
100  mAreaUnit = other.mAreaUnit;
101  mLogLevel = other.mLogLevel;
102  }
103 
108  QgsProcessingContext::Flags flags() const { return mFlags; }
109 
114  void setFlags( QgsProcessingContext::Flags flags ) { mFlags = flags; }
115 
120  QgsProject *project() const { return mProject; }
121 
130  void setProject( QgsProject *project )
131  {
132  mProject = project;
133  if ( mProject )
134  {
135  mTransformContext = mProject->transformContext();
136  if ( mEllipsoid.isEmpty() )
137  mEllipsoid = mProject->ellipsoid();
138  if ( mDistanceUnit == QgsUnitTypes::DistanceUnknownUnit )
139  mDistanceUnit = mProject->distanceUnits();
140  if ( mAreaUnit == QgsUnitTypes::AreaUnknownUnit )
141  mAreaUnit = mProject->areaUnits();
142  }
143  }
144 
148  QgsExpressionContext &expressionContext() { return mExpressionContext; }
149 
153  SIP_SKIP const QgsExpressionContext &expressionContext() const { return mExpressionContext; }
154 
158  void setExpressionContext( const QgsExpressionContext &context ) { mExpressionContext = context; }
159 
164  QgsCoordinateTransformContext transformContext() const { return mTransformContext; }
165 
174  void setTransformContext( const QgsCoordinateTransformContext &context ) { mTransformContext = context; }
175 
182  QString ellipsoid() const;
183 
192  void setEllipsoid( const QString &ellipsoid );
193 
201  QgsUnitTypes::DistanceUnit distanceUnit() const;
202 
212  void setDistanceUnit( QgsUnitTypes::DistanceUnit unit );
213 
221  QgsUnitTypes::AreaUnit areaUnit() const;
222 
232  void setAreaUnit( QgsUnitTypes::AreaUnit areaUnit );
233 
240  QgsDateTimeRange currentTimeRange() const;
241 
248  void setCurrentTimeRange( const QgsDateTimeRange &currentTimeRange );
249 
254  QgsMapLayerStore *temporaryLayerStore() { return &tempLayerStore; }
255 
261  class CORE_EXPORT LayerDetails
262  {
263  public:
264 
268  LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType )
269  : name( name )
270  , outputName( outputName )
271  , layerTypeHint( layerTypeHint )
272  , project( project )
273  {}
274 
276  LayerDetails() = default;
277 
284  QString name;
285 
291  bool forceName = false;
292 
296  QString outputName;
297 
304 
310  QgsProcessingLayerPostProcessorInterface *postProcessor() const;
311 
320  void setPostProcessor( QgsProcessingLayerPostProcessorInterface *processor SIP_TRANSFER );
321 
327  void setOutputLayerName( QgsMapLayer *layer ) const;
328 
330  QgsProject *project = nullptr;
331 
332  private:
333 
334  // Ideally a unique_ptr, but cannot be due to use within QMap. Is cleaned up by QgsProcessingContext.
335  QgsProcessingLayerPostProcessorInterface *mPostProcessor = nullptr;
336 
337  };
338 
346  QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
347  {
348  return mLayersToLoadOnCompletion;
349  }
350 
360  bool willLoadLayerOnCompletion( const QString &layer ) const
361  {
362  return mLayersToLoadOnCompletion.contains( layer );
363  }
364 
372  void setLayersToLoadOnCompletion( const QMap< QString, QgsProcessingContext::LayerDetails > &layers );
373 
382  void addLayerToLoadOnCompletion( const QString &layer, const QgsProcessingContext::LayerDetails &details );
383 
398  {
399  return mLayersToLoadOnCompletion[ layer ];
400  }
401 
406  QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryCheck; }
407 
414  void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck check );
415 
423 #ifndef SIP_RUN
424  void setInvalidGeometryCallback( const std::function< void( const QgsFeature & ) > &callback ) { mInvalidGeometryCallback = callback; mUseDefaultInvalidGeometryCallback = false; }
425 #else
426  void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
427  % MethodCode
428  Py_BEGIN_ALLOW_THREADS
429 
430  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
431  {
432  SIP_BLOCK_THREADS
433  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
434  SIP_UNBLOCK_THREADS
435  } );
436 
437  Py_END_ALLOW_THREADS
438  % End
439 #endif
440 
448  SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback( QgsFeatureSource *source = nullptr ) const;
449 
455  SIP_SKIP std::function< void( const QgsFeature & ) > defaultInvalidGeometryCallbackForCheck( QgsFeatureRequest::InvalidGeometryCheck check, QgsFeatureSource *source = nullptr ) const;
456 
464 #ifndef SIP_RUN
465  void setTransformErrorCallback( const std::function< void( const QgsFeature & ) > &callback ) { mTransformErrorCallback = callback; }
466 #else
467  void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
468  % MethodCode
469  Py_BEGIN_ALLOW_THREADS
470 
471  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
472  {
473  SIP_BLOCK_THREADS
474  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
475  SIP_UNBLOCK_THREADS
476  } );
477 
478  Py_END_ALLOW_THREADS
479  % End
480 #endif
481 
489  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
490 
495  QString defaultEncoding() const { return mDefaultEncoding; }
496 
501  void setDefaultEncoding( const QString &encoding ) { mDefaultEncoding = encoding; }
502 
507  QgsProcessingFeedback *feedback() { return mFeedback; }
508 
517  void setFeedback( QgsProcessingFeedback *feedback ) { mFeedback = feedback; }
518 
523  QThread *thread() { return tempLayerStore.thread(); }
524 
531  void pushToThread( QThread *thread )
532  {
533  // cppcheck-suppress assertWithSideEffect
534  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" );
535  tempLayerStore.moveToThread( thread );
536  }
537 
545  void takeResultsFrom( QgsProcessingContext &context );
546 
557  QgsMapLayer *getMapLayer( const QString &identifier );
558 
567  QgsMapLayer *takeResultLayer( const QString &id ) SIP_TRANSFERBACK;
568 
587  QString preferredVectorFormat() const { return mPreferredVectorFormat; }
588 
604  void setPreferredVectorFormat( const QString &format ) { mPreferredVectorFormat = format; }
605 
624  QString preferredRasterFormat() const { return mPreferredRasterFormat; }
625 
641  void setPreferredRasterFormat( const QString &format ) { mPreferredRasterFormat = format; }
642 
649  LogLevel logLevel() const;
650 
657  void setLogLevel( LogLevel level );
658 
659  private:
660 
661  QgsProcessingContext::Flags mFlags = QgsProcessingContext::Flags();
662  QPointer< QgsProject > mProject;
663  QgsCoordinateTransformContext mTransformContext;
664 
665  QString mEllipsoid;
668 
669  QgsDateTimeRange mCurrentTimeRange;
670 
672  QgsMapLayerStore tempLayerStore;
673  QgsExpressionContext mExpressionContext;
674 
676  bool mUseDefaultInvalidGeometryCallback = true;
677  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
678 
679  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
680  QString mDefaultEncoding;
681  QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
682 
683  QPointer< QgsProcessingFeedback > mFeedback;
684 
685  QString mPreferredVectorFormat;
686  QString mPreferredRasterFormat;
687 
688  LogLevel mLogLevel = DefaultLevel;
689 
690 #ifdef SIP_RUN
692 #endif
693 };
694 
695 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::Flags )
696 
697 
698 
711 {
712  public:
713 
715 
730  virtual void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
731 
732 };
733 
734 
735 #endif // QGSPROCESSINGPARAMETERS_H
736 
737 
738 
739 
Contains information about the context in which a coordinate transform is executed.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
InvalidGeometryCheck
Handling of features with invalid geometries.
@ GeometryNoCheck
No invalid geometry checking.
An interface for objects which provide features via a getFeatures method.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Details for layers to load into projects.
LayerDetails()=default
Default constructor.
QString name
Friendly name for layer, possibly for use when loading layer into project.
LayerDetails(const QString &name, QgsProject *project, const QString &outputName=QString(), QgsProcessingUtils::LayerHint layerTypeHint=QgsProcessingUtils::LayerHint::UnknownType)
Constructor for LayerDetails.
QString outputName
Associated output name from algorithm which generated the layer.
Contains information about the context in which a processing algorithm is executed.
QString defaultEncoding() const
Returns the default encoding to use for newly created files.
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
QString preferredRasterFormat() const
Returns the preferred raster format to use for vector outputs.
QgsProcessingContext::Flags flags() const
Returns any flags set in the context.
LogLevel
Logging level for algorithms to use when pushing feedback messages.
@ Verbose
Verbose logging.
std::function< void(const QgsFeature &) > transformErrorCallback() const
Returns the callback function to use when encountering a transform error when iterating features.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
QgsExpressionContext & expressionContext()
Returns the expression context.
QgsProcessingContext(const QgsProcessingContext &other)=delete
QgsProcessingContext cannot be copied.
void setDefaultEncoding(const QString &encoding)
Sets the default encoding to use for newly created files.
QThread * thread()
Returns the thread in which the context lives.
void setFeedback(QgsProcessingFeedback *feedback)
Sets an associated feedback object.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
void setFlags(QgsProcessingContext::Flags flags)
Sets flags for the context.
void setProject(QgsProject *project)
Sets the project in which the algorithm will be executed.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
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 pushToThread(QThread *thread)
Pushes the thread affinity for the context (including all layers contained in the temporaryLayerStore...
Flag
Flags that affect how processing algorithms are run.
QgsProcessingFeedback * feedback()
Returns the associated feedback object.
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...
const QgsExpressionContext & expressionContext() const
Returns the expression context.
QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const
Returns the behavior used for checking invalid geometries in input layers.
void copyThreadSafeSettings(const QgsProcessingContext &other)
Copies all settings which are safe for use across different threads from other to this context.
QgsProcessingContext & operator=(const QgsProcessingContext &other)=delete
QgsProcessingContext cannot be copied.
void setInvalidGeometryCallback(const std::function< void(const QgsFeature &) > &callback)
Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is s...
void setPreferredVectorFormat(const QString &format)
Sets the preferred vector format to use for vector outputs.
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context.
void setTransformErrorCallback(const std::function< void(const QgsFeature &) > &callback)
Sets a callback function to use when encountering a transform error when iterating features.
QString preferredVectorFormat() const
Returns the preferred vector format to use for vector outputs.
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 setPreferredRasterFormat(const QString &format)
Sets the preferred raster format to use for vector outputs.
Base class for providing feedback from a processing algorithm.
An interface for layer post-processing handlers for execution following a processing algorithm operat...
virtual ~QgsProcessingLayerPostProcessorInterface()=default
virtual void postProcessLayer(QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback)=0
Post-processes the specified layer, following successful execution of a processing algorithm.
LayerHint
Layer type hints.
@ UnknownType
Unknown layer type.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:101
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:107
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:68
@ DistanceUnknownUnit
Unknown distance unit.
Definition: qgsunittypes.h:78
AreaUnit
Units of area.
Definition: qgsunittypes.h:94
@ AreaUnknownUnit
Unknown areal unit.
Definition: qgsunittypes.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
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.