QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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;
77  QgsProcessingContext &operator=( 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 
741  virtual ~QgsProcessingLayerPostProcessorInterface() = default;
742 
757  virtual void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
758 
759 };
760 
761 
762 #endif // QGSPROCESSINGPARAMETERS_H
763 
764 
765 
766 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsProcessingContext::layersToLoadOnCompletion
QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
Returns a map of layers (by ID or datasource) to LayerDetails, to load into the canvas upon completio...
Definition: qgsprocessingcontext.h:347
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:57
qgsfeaturerequest.h
QgsProcessingContext::willLoadLayerOnCompletion
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...
Definition: qgsprocessingcontext.h:361
QgsProcessingContext::project
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Definition: qgsprocessingcontext.h:121
QgsProcessingContext::preferredVectorFormat
QString preferredVectorFormat() const
Returns the preferred vector format to use for vector outputs.
Definition: qgsprocessingcontext.h:588
QgsUnitTypes::DistanceUnknownUnit
@ DistanceUnknownUnit
Unknown distance unit.
Definition: qgsunittypes.h:78
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsProject::transformContext
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:110
QgsFeatureSource
An interface for objects which provide features via a getFeatures method.
Definition: qgsfeaturesource.h:37
qgis.h
QgsProcessingContext::LogLevel
LogLevel
Logging level for algorithms to use when pushing feedback messages.
Definition: qgsprocessingcontext.h:63
QgsProcessingContext::thread
QThread * thread()
Returns the thread in which the context lives.
Definition: qgsprocessingcontext.h:524
QgsProcessingUtils::LayerHint
LayerHint
Layer type hints.
Definition: qgsprocessingutils.h:202
QgsFeatureRequest::InvalidGeometryCheck
InvalidGeometryCheck
Handling of features with invalid geometries.
Definition: qgsfeaturerequest.h:122
QgsUnitTypes::DistanceUnit
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:67
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:103
SIP_TRANSFERBACK
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
QgsProcessingContext::setTransformErrorCallback
void setTransformErrorCallback(const std::function< void(const QgsFeature &) > &callback)
Sets a callback function to use when encountering a transform error when iterating features.
Definition: qgsprocessingcontext.h:466
QgsMapLayerStore
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
Definition: qgsmaplayerstore.h:35
qgsexpressioncontext.h
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsProcessingContext::pushToThread
void pushToThread(QThread *thread)
Pushes the thread affinity for the context (including all layers contained in the temporaryLayerStore...
Definition: qgsprocessingcontext.h:532
QgsProcessingContext::LayerDetails::LayerDetails
LayerDetails(const QString &name, QgsProject *project, const QString &outputName=QString(), QgsProcessingUtils::LayerHint layerTypeHint=QgsProcessingUtils::LayerHint::UnknownType)
Constructor for LayerDetails.
Definition: qgsprocessingcontext.h:269
QgsProcessingContext::invalidGeometryCheck
QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const
Returns the behavior used for checking invalid geometries in input layers.
Definition: qgsprocessingcontext.h:407
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsProcessingContext::transformErrorCallback
std::function< void(const QgsFeature &) > transformErrorCallback() const
Returns the callback function to use when encountering a transform error when iterating features.
Definition: qgsprocessingcontext.h:490
QgsProcessingContext::defaultEncoding
QString defaultEncoding() const
Returns the default encoding to use for newly created files.
Definition: qgsprocessingcontext.h:496
QgsProcessingContext::setDefaultEncoding
void setDefaultEncoding(const QString &encoding)
Sets the default encoding to use for newly created files.
Definition: qgsprocessingcontext.h:502
QgsProcessingContext::LayerDetails::name
QString name
Friendly name for layer, possibly for use when loading layer into project.
Definition: qgsprocessingcontext.h:285
QgsProcessingContext::copyThreadSafeSettings
void copyThreadSafeSettings(const QgsProcessingContext &other)
Copies all settings which are safe for use across different threads from other to this context.
Definition: qgsprocessingcontext.h:85
QgsProcessingContext::setInvalidGeometryCallback
void setInvalidGeometryCallback(const std::function< void(const QgsFeature &) > &callback)
Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is s...
Definition: qgsprocessingcontext.h:425
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Q_DECLARE_OPERATORS_FOR_FLAGS
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.
QgsProcessingContext::preferredRasterFormat
QString preferredRasterFormat() const
Returns the preferred raster format to use for vector outputs.
Definition: qgsprocessingcontext.h:625
qgsprocessingfeedback.h
QgsProcessingContext::setExpressionContext
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
Definition: qgsprocessingcontext.h:159
QgsUnitTypes::AreaUnit
AreaUnit
Units of area.
Definition: qgsunittypes.h:93
QgsProcessingContext::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Definition: qgsprocessingcontext.h:165
QgsProcessingLayerPostProcessorInterface
An interface for layer post-processing handlers for execution following a processing algorithm operat...
Definition: qgsprocessingcontext.h:737
QgsProcessingContext::setTransformContext
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context.
Definition: qgsprocessingcontext.h:175
QgsProcessingContext::Flag
Flag
Flags that affect how processing algorithms are run.
Definition: qgsprocessingcontext.h:51
QgsProcessingContext::LayerDetails
Details for layers to load into projects.
Definition: qgsprocessingcontext.h:262
QgsProcessingContext::expressionContext
const QgsExpressionContext & expressionContext() const
Returns the expression context.
Definition: qgsprocessingcontext.h:154
QgsProcessingContext::temporaryLayerStore
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
Definition: qgsprocessingcontext.h:255
QgsProcessingContext::feedback
QgsProcessingFeedback * feedback()
Returns the associated feedback object.
Definition: qgsprocessingcontext.h:508
QgsProcessingContext::setFeedback
void setFeedback(QgsProcessingFeedback *feedback)
Sets an associated feedback object.
Definition: qgsprocessingcontext.h:518
qgsprocessingutils.h
QgsProcessingContext::LayerDetails::outputName
QString outputName
Associated output name from algorithm which generated the layer.
Definition: qgsprocessingcontext.h:297
QgsProcessingContext::ProcessArgumentFlag
ProcessArgumentFlag
Flags controlling the results given by asQgisProcessArguments().
Definition: qgsprocessingcontext.h:672
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsProcessingContext::layerToLoadOnCompletionDetails
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...
Definition: qgsprocessingcontext.h:398
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:149
QgsProcessingContext::flags
QgsProcessingContext::Flags flags() const
Returns any flags set in the context.
Definition: qgsprocessingcontext.h:109
QgsFeatureRequest::GeometryNoCheck
@ GeometryNoCheck
No invalid geometry checking.
Definition: qgsfeaturerequest.h:124
QgsProcessingContext::setPreferredVectorFormat
void setPreferredVectorFormat(const QString &format)
Sets the preferred vector format to use for vector outputs.
Definition: qgsprocessingcontext.h:605
qgsexception.h
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsProcessingContext::Verbose
@ Verbose
Verbose logging.
Definition: qgsprocessingcontext.h:66
QgsProcessingContext::setProject
void setProject(QgsProject *project)
Sets the project in which the algorithm will be executed.
Definition: qgsprocessingcontext.h:131
QgsProcessingContext::setPreferredRasterFormat
void setPreferredRasterFormat(const QString &format)
Sets the preferred raster format to use for vector outputs.
Definition: qgsprocessingcontext.h:642
qgsproject.h
QgsProcessingContext::setFlags
void setFlags(QgsProcessingContext::Flags flags)
Sets flags for the context.
Definition: qgsprocessingcontext.h:115
QgsUnitTypes::AreaUnknownUnit
@ AreaUnknownUnit
Unknown areal unit.
Definition: qgsunittypes.h:106
QgsProcessingUtils::LayerHint::UnknownType
@ UnknownType
Unknown layer type.