QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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  // For future API flexibility only and to avoid sip issues, remove when real entries are added to flags.
54  Unused = 1 << 0,
55  };
56  Q_DECLARE_FLAGS( Flags, Flag )
57 
58 
63  enum LogLevel
64  {
65  DefaultLevel = 0,
67  };
68 
73 
75  QgsProcessingContext( const QgsProcessingContext &other ) = delete;
78 
80 
86  {
87  mFlags = other.mFlags;
88  mProject = other.mProject;
89  mTransformContext = other.mTransformContext;
90  mExpressionContext = other.mExpressionContext;
91  mInvalidGeometryCallback = other.mInvalidGeometryCallback;
92  mUseDefaultInvalidGeometryCallback = other.mUseDefaultInvalidGeometryCallback;
93  mInvalidGeometryCheck = other.mInvalidGeometryCheck;
94  mTransformErrorCallback = other.mTransformErrorCallback;
95  mDefaultEncoding = other.mDefaultEncoding;
96  mFeedback = other.mFeedback;
97  mPreferredVectorFormat = other.mPreferredVectorFormat;
98  mPreferredRasterFormat = other.mPreferredRasterFormat;
99  mEllipsoid = other.mEllipsoid;
100  mDistanceUnit = other.mDistanceUnit;
101  mAreaUnit = other.mAreaUnit;
102  mLogLevel = other.mLogLevel;
103  }
104 
109  QgsProcessingContext::Flags flags() const { return mFlags; }
110 
115  void setFlags( QgsProcessingContext::Flags flags ) { mFlags = flags; }
116 
121  QgsProject *project() const { return mProject; }
122 
131  void setProject( QgsProject *project )
132  {
133  mProject = project;
134  if ( mProject )
135  {
136  mTransformContext = mProject->transformContext();
137  if ( mEllipsoid.isEmpty() )
138  mEllipsoid = mProject->ellipsoid();
139  if ( mDistanceUnit == QgsUnitTypes::DistanceUnknownUnit )
140  mDistanceUnit = mProject->distanceUnits();
141  if ( mAreaUnit == QgsUnitTypes::AreaUnknownUnit )
142  mAreaUnit = mProject->areaUnits();
143  }
144  }
145 
149  QgsExpressionContext &expressionContext() { return mExpressionContext; }
150 
154  SIP_SKIP const QgsExpressionContext &expressionContext() const { return mExpressionContext; }
155 
159  void setExpressionContext( const QgsExpressionContext &context ) { mExpressionContext = context; }
160 
165  QgsCoordinateTransformContext transformContext() const { return mTransformContext; }
166 
175  void setTransformContext( const QgsCoordinateTransformContext &context ) { mTransformContext = context; }
176 
183  QString ellipsoid() const;
184 
193  void setEllipsoid( const QString &ellipsoid );
194 
202  QgsUnitTypes::DistanceUnit distanceUnit() const;
203 
213  void setDistanceUnit( QgsUnitTypes::DistanceUnit unit );
214 
222  QgsUnitTypes::AreaUnit areaUnit() const;
223 
233  void setAreaUnit( QgsUnitTypes::AreaUnit areaUnit );
234 
241  QgsDateTimeRange currentTimeRange() const;
242 
249  void setCurrentTimeRange( const QgsDateTimeRange &currentTimeRange );
250 
255  QgsMapLayerStore *temporaryLayerStore() { return &tempLayerStore; }
256 
262  class CORE_EXPORT LayerDetails
263  {
264  public:
265 
269  LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType )
270  : name( name )
271  , outputName( outputName )
272  , layerTypeHint( layerTypeHint )
273  , project( project )
274  {}
275 
277  LayerDetails() = default;
278 
285  QString name;
286 
292  bool forceName = false;
293 
297  QString outputName;
298 
305 
311  QgsProcessingLayerPostProcessorInterface *postProcessor() const;
312 
321  void setPostProcessor( QgsProcessingLayerPostProcessorInterface *processor SIP_TRANSFER );
322 
328  void setOutputLayerName( QgsMapLayer *layer ) const;
329 
331  QgsProject *project = nullptr;
332 
333  private:
334 
335  // Ideally a unique_ptr, but cannot be due to use within QMap. Is cleaned up by QgsProcessingContext.
336  QgsProcessingLayerPostProcessorInterface *mPostProcessor = nullptr;
337 
338  };
339 
347  QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
348  {
349  return mLayersToLoadOnCompletion;
350  }
351 
361  bool willLoadLayerOnCompletion( const QString &layer ) const
362  {
363  return mLayersToLoadOnCompletion.contains( layer );
364  }
365 
373  void setLayersToLoadOnCompletion( const QMap< QString, QgsProcessingContext::LayerDetails > &layers );
374 
383  void addLayerToLoadOnCompletion( const QString &layer, const QgsProcessingContext::LayerDetails &details );
384 
399  {
400  return mLayersToLoadOnCompletion[ layer ];
401  }
402 
407  QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryCheck; }
408 
415  void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck check );
416 
424 #ifndef SIP_RUN
425  void setInvalidGeometryCallback( const std::function< void( const QgsFeature & ) > &callback ) { mInvalidGeometryCallback = callback; mUseDefaultInvalidGeometryCallback = false; }
426 #else
427  void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
428  % MethodCode
429  Py_BEGIN_ALLOW_THREADS
430 
431  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
432  {
433  SIP_BLOCK_THREADS
434  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
435  SIP_UNBLOCK_THREADS
436  } );
437 
438  Py_END_ALLOW_THREADS
439  % End
440 #endif
441 
449  SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback( QgsFeatureSource *source = nullptr ) const;
450 
456  SIP_SKIP std::function< void( const QgsFeature & ) > defaultInvalidGeometryCallbackForCheck( QgsFeatureRequest::InvalidGeometryCheck check, QgsFeatureSource *source = nullptr ) const;
457 
465 #ifndef SIP_RUN
466  void setTransformErrorCallback( const std::function< void( const QgsFeature & ) > &callback ) { mTransformErrorCallback = callback; }
467 #else
468  void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
469  % MethodCode
470  Py_BEGIN_ALLOW_THREADS
471 
472  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
473  {
474  SIP_BLOCK_THREADS
475  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
476  SIP_UNBLOCK_THREADS
477  } );
478 
479  Py_END_ALLOW_THREADS
480  % End
481 #endif
482 
490  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
491 
496  QString defaultEncoding() const { return mDefaultEncoding; }
497 
502  void setDefaultEncoding( const QString &encoding ) { mDefaultEncoding = encoding; }
503 
508  QgsProcessingFeedback *feedback() { return mFeedback; }
509 
518  void setFeedback( QgsProcessingFeedback *feedback ) { mFeedback = feedback; }
519 
524  QThread *thread() { return tempLayerStore.thread(); }
525 
532  void pushToThread( QThread *thread )
533  {
534  // cppcheck-suppress assertWithSideEffect
535  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" );
536  tempLayerStore.moveToThread( thread );
537  }
538 
546  void takeResultsFrom( QgsProcessingContext &context );
547 
558  QgsMapLayer *getMapLayer( const QString &identifier );
559 
568  QgsMapLayer *takeResultLayer( const QString &id ) SIP_TRANSFERBACK;
569 
588  QString preferredVectorFormat() const { return mPreferredVectorFormat; }
589 
605  void setPreferredVectorFormat( const QString &format ) { mPreferredVectorFormat = format; }
606 
625  QString preferredRasterFormat() const { return mPreferredRasterFormat; }
626 
642  void setPreferredRasterFormat( const QString &format ) { mPreferredRasterFormat = format; }
643 
650  LogLevel logLevel() const;
651 
658  void setLogLevel( LogLevel level );
659 
665  QVariantMap exportToMap() const;
666 
672  enum class ProcessArgumentFlag : int
673  {
674  IncludeProjectPath = 1 << 0,
675  };
676  Q_DECLARE_FLAGS( ProcessArgumentFlags, ProcessArgumentFlag )
677 
678 
683  QStringList asQgisProcessArguments( QgsProcessingContext::ProcessArgumentFlags flags = QgsProcessingContext::ProcessArgumentFlags() ) const;
684 
685  private:
686 
687  QgsProcessingContext::Flags mFlags = QgsProcessingContext::Flags();
688  QPointer< QgsProject > mProject;
689  QgsCoordinateTransformContext mTransformContext;
690 
691  QString mEllipsoid;
694 
695  QgsDateTimeRange mCurrentTimeRange;
696 
698  QgsMapLayerStore tempLayerStore;
699  QgsExpressionContext mExpressionContext;
700 
702  bool mUseDefaultInvalidGeometryCallback = true;
703  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
704 
705  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
706  QString mDefaultEncoding;
707  QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
708 
709  QPointer< QgsProcessingFeedback > mFeedback;
710 
711  QString mPreferredVectorFormat;
712  QString mPreferredRasterFormat;
713 
714  LogLevel mLogLevel = DefaultLevel;
715 
716 #ifdef SIP_RUN
718 #endif
719 };
720 
721 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::Flags )
722 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::ProcessArgumentFlags )
723 
724 
725 
738 {
739  public:
740 
742 
757  virtual void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
758 
759 };
760 
761 
762 #endif // QGSPROCESSINGPARAMETERS_H
763 
764 
765 
766 
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.
ProcessArgumentFlag
Flags controlling the results given by asQgisProcessArguments().
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.