QGIS API Documentation  3.37.0-Master (a5b4d9743e8)
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 "qgsprocessingfeedback.h"
26 #include "qgsprocessingutils.h"
27 
28 
29 #include <QThread>
30 #include <QPointer>
31 
33 
43 class CORE_EXPORT QgsProcessingContext
44 {
45  public:
46 
48  enum Flag SIP_ENUM_BASETYPE( IntFlag )
49  {
50  // For future API flexibility only and to avoid sip issues, remove when real entries are added to flags.
51  Unused = 1 << 0,
52  };
53  Q_DECLARE_FLAGS( Flags, Flag )
54 
55 
59 
61  QgsProcessingContext( const QgsProcessingContext &other ) = delete;
64 
66 
72  {
73  mFlags = other.mFlags;
74  mProject = other.mProject;
75  mTransformContext = other.mTransformContext;
76  mExpressionContext = other.mExpressionContext;
77  mExpressionContext.setLoadedLayerStore( &tempLayerStore );
78  mInvalidGeometryCallback = other.mInvalidGeometryCallback;
79  mUseDefaultInvalidGeometryCallback = other.mUseDefaultInvalidGeometryCallback;
80  mInvalidGeometryCheck = other.mInvalidGeometryCheck;
81  mTransformErrorCallback = other.mTransformErrorCallback;
82  mDefaultEncoding = other.mDefaultEncoding;
83  mFeedback = other.mFeedback;
84  mPreferredVectorFormat = other.mPreferredVectorFormat;
85  mPreferredRasterFormat = other.mPreferredRasterFormat;
86  mEllipsoid = other.mEllipsoid;
87  mDistanceUnit = other.mDistanceUnit;
88  mAreaUnit = other.mAreaUnit;
89  mLogLevel = other.mLogLevel;
90  mTemporaryFolderOverride = other.mTemporaryFolderOverride;
91  mMaximumThreads = other.mMaximumThreads;
92  }
93 
98  QgsProcessingContext::Flags flags() const SIP_HOLDGIL { return mFlags; }
99 
104  void setFlags( QgsProcessingContext::Flags flags ) SIP_HOLDGIL { mFlags = flags; }
105 
110  QgsProject *project() const SIP_HOLDGIL { return mProject; }
111 
121  {
122  mProject = project;
123  if ( mProject )
124  {
125  mTransformContext = mProject->transformContext();
126  if ( mEllipsoid.isEmpty() )
127  mEllipsoid = mProject->ellipsoid();
128  if ( mDistanceUnit == Qgis::DistanceUnit::Unknown )
129  mDistanceUnit = mProject->distanceUnits();
130  if ( mAreaUnit == Qgis::AreaUnit::Unknown )
131  mAreaUnit = mProject->areaUnits();
132  }
133  }
134 
138  QgsExpressionContext &expressionContext() SIP_HOLDGIL { return mExpressionContext; }
139 
143  SIP_SKIP const QgsExpressionContext &expressionContext() const { return mExpressionContext; }
144 
148  void setExpressionContext( const QgsExpressionContext &context );
149 
154  QgsCoordinateTransformContext transformContext() const SIP_HOLDGIL { return mTransformContext; }
155 
164  void setTransformContext( const QgsCoordinateTransformContext &context ) SIP_HOLDGIL { mTransformContext = context; }
165 
172  QString ellipsoid() const SIP_HOLDGIL;
173 
182  void setEllipsoid( const QString &ellipsoid ) SIP_HOLDGIL;
183 
191  Qgis::DistanceUnit distanceUnit() const SIP_HOLDGIL;
192 
202  void setDistanceUnit( Qgis::DistanceUnit unit ) SIP_HOLDGIL;
203 
211  Qgis::AreaUnit areaUnit() const SIP_HOLDGIL;
212 
222  void setAreaUnit( Qgis::AreaUnit areaUnit ) SIP_HOLDGIL;
223 
230  QgsDateTimeRange currentTimeRange() const SIP_HOLDGIL;
231 
238  void setCurrentTimeRange( const QgsDateTimeRange &currentTimeRange ) SIP_HOLDGIL;
239 
244  QgsMapLayerStore *temporaryLayerStore() SIP_HOLDGIL { return &tempLayerStore; }
245 
250  class CORE_EXPORT LayerDetails
251  {
252  public:
253 
257  LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType )
258  : name( name )
259  , outputName( outputName )
260  , layerTypeHint( layerTypeHint )
261  , project( project )
262  {}
263 
265  LayerDetails() = default;
266 
273  QString name;
274 
280  bool forceName = false;
281 
285  QString outputName;
286 
292  QString groupName;
293 
301  int layerSortKey = 0;
302 
309 
315  QgsProcessingLayerPostProcessorInterface *postProcessor() const;
316 
325  void setPostProcessor( QgsProcessingLayerPostProcessorInterface *processor SIP_TRANSFER );
326 
332  void setOutputLayerName( QgsMapLayer *layer ) const;
333 
335  QgsProject *project = nullptr;
336 
337  private:
338 
339  // Ideally a unique_ptr, but cannot be due to use within QMap. Is cleaned up by QgsProcessingContext.
340  QgsProcessingLayerPostProcessorInterface *mPostProcessor = nullptr;
341 
342  };
343 
351  QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const SIP_HOLDGIL
352  {
353  return mLayersToLoadOnCompletion;
354  }
355 
365  bool willLoadLayerOnCompletion( const QString &layer ) const SIP_HOLDGIL
366  {
367  return mLayersToLoadOnCompletion.contains( layer );
368  }
369 
377  void setLayersToLoadOnCompletion( const QMap< QString, QgsProcessingContext::LayerDetails > &layers ) SIP_HOLDGIL;
378 
387  void addLayerToLoadOnCompletion( const QString &layer, const QgsProcessingContext::LayerDetails &details ) SIP_HOLDGIL;
388 
403  {
404  return mLayersToLoadOnCompletion[ layer ];
405  }
406 
411  Qgis::InvalidGeometryCheck invalidGeometryCheck() const SIP_HOLDGIL { return mInvalidGeometryCheck; }
412 
419  void setInvalidGeometryCheck( Qgis::InvalidGeometryCheck check );
420 
427 #ifndef SIP_RUN
428  void setInvalidGeometryCallback( const std::function< void( const QgsFeature & ) > &callback ) { mInvalidGeometryCallback = callback; mUseDefaultInvalidGeometryCallback = false; }
429 #else
430  void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
431  % MethodCode
432  Py_BEGIN_ALLOW_THREADS
433 
434  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
435  {
436  SIP_BLOCK_THREADS
437  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
438  SIP_UNBLOCK_THREADS
439  } );
440 
441  Py_END_ALLOW_THREADS
442  % End
443 #endif
444 
451  SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback( QgsFeatureSource *source = nullptr ) const;
452 
458  SIP_SKIP std::function< void( const QgsFeature & ) > defaultInvalidGeometryCallbackForCheck( Qgis::InvalidGeometryCheck check, QgsFeatureSource *source = nullptr ) const;
459 
466 #ifndef SIP_RUN
467  void setTransformErrorCallback( const std::function< void( const QgsFeature & ) > &callback ) { mTransformErrorCallback = callback; }
468 #else
469  void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
470  % MethodCode
471  Py_BEGIN_ALLOW_THREADS
472 
473  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
474  {
475  SIP_BLOCK_THREADS
476  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
477  SIP_UNBLOCK_THREADS
478  } );
479 
480  Py_END_ALLOW_THREADS
481  % End
482 #endif
483 
490  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
491 
496  QString defaultEncoding() const SIP_HOLDGIL { return mDefaultEncoding; }
497 
502  void setDefaultEncoding( const QString &encoding ) SIP_HOLDGIL { mDefaultEncoding = encoding; }
503 
508  QgsProcessingFeedback *feedback() SIP_HOLDGIL { return mFeedback; }
509 
518  void setFeedback( QgsProcessingFeedback *feedback ) SIP_HOLDGIL { mFeedback = feedback; }
519 
524  QThread *thread() SIP_HOLDGIL { 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 SIP_HOLDGIL { return mPreferredVectorFormat; }
589 
605  void setPreferredVectorFormat( const QString &format ) SIP_HOLDGIL { mPreferredVectorFormat = format; }
606 
625  QString preferredRasterFormat() const SIP_HOLDGIL { return mPreferredRasterFormat; }
626 
642  void setPreferredRasterFormat( const QString &format ) SIP_HOLDGIL { mPreferredRasterFormat = format; }
643 
650  Qgis::ProcessingLogLevel logLevel() const SIP_HOLDGIL;
651 
658  void setLogLevel( Qgis::ProcessingLogLevel level ) SIP_HOLDGIL;
659 
669  QString temporaryFolder() const SIP_HOLDGIL;
670 
680  void setTemporaryFolder( const QString &folder ) SIP_HOLDGIL;
681 
693  int maximumThreads() const SIP_HOLDGIL;
694 
710  void setMaximumThreads( int threads ) SIP_HOLDGIL;
711 
717  QVariantMap exportToMap() const SIP_HOLDGIL;
718 
724  enum class ProcessArgumentFlag : int SIP_ENUM_BASETYPE( IntFlag )
725  {
726  IncludeProjectPath = 1 << 0,
727  };
729 
730 
735  QStringList asQgisProcessArguments( QgsProcessingContext::ProcessArgumentFlags flags = QgsProcessingContext::ProcessArgumentFlags() ) const;
736 
737  private:
738 
740  QPointer< QgsProject > mProject;
741  QgsCoordinateTransformContext mTransformContext;
742 
743  QString mEllipsoid;
746 
747  QgsDateTimeRange mCurrentTimeRange;
748 
750  QgsMapLayerStore tempLayerStore;
751  QgsExpressionContext mExpressionContext;
752 
754  bool mUseDefaultInvalidGeometryCallback = true;
755  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
756 
757  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
758  QString mDefaultEncoding;
759  QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
760 
761  QPointer< QgsProcessingFeedback > mFeedback;
762 
763  QString mPreferredVectorFormat;
764  QString mPreferredRasterFormat;
765 
767 
768  QString mTemporaryFolderOverride;
769  int mMaximumThreads = QThread::idealThreadCount();
770 
771 #ifdef SIP_RUN
773 #endif
774 };
775 
778 
779 
780 
793 {
794  public:
795 
797 
812  virtual void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
813 
814 };
815 
816 
817 #endif // QGSPROCESSINGPARAMETERS_H
818 
819 
820 
821 
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:54
DistanceUnit
Units of distance.
Definition: qgis.h:4090
@ Unknown
Unknown distance unit.
AreaUnit
Units of area.
Definition: qgis.h:4128
@ Unknown
Unknown areal unit.
InvalidGeometryCheck
Methods for handling of features with invalid geometries.
Definition: qgis.h:1759
@ NoCheck
No invalid geometry checking.
ProcessingLogLevel
Logging level for algorithms to use when pushing feedback messages.
Definition: qgis.h:2940
@ DefaultLevel
Default logging level.
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...
void setLoadedLayerStore(QgsMapLayerStore *store)
Sets the destination layer store for any layers loaded during expression evaluation.
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:75
Details for layers to load into projects.
QString groupName
Optional name for a layer tree group under which to place the layer when loading it into a project.
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.
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().
QFlags< ProcessArgumentFlag > ProcessArgumentFlags
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 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.
Qgis::InvalidGeometryCheck invalidGeometryCheck() const
Returns the behavior used for checking invalid geometries in input layers.
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.
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:107
#define SIP_ENUM_BASETYPE(type)
Definition: qgis_sip.h:278
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
#define SIP_HOLDGIL
Definition: qgis_sip.h:171
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)