QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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;
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 
223  QgsMapLayerStore *temporaryLayerStore() { return &tempLayerStore; }
224 
230  class CORE_EXPORT LayerDetails
231  {
232  public:
233 
237  LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType )
238  : name( name )
239  , outputName( outputName )
240  , layerTypeHint( layerTypeHint )
241  , project( project )
242  {}
243 
245  LayerDetails() = default;
246 
253  QString name;
254 
260  bool forceName = false;
261 
265  QString outputName;
266 
273 
279  QgsProcessingLayerPostProcessorInterface *postProcessor() const;
280 
289  void setPostProcessor( QgsProcessingLayerPostProcessorInterface *processor SIP_TRANSFER );
290 
296  void setOutputLayerName( QgsMapLayer *layer ) const;
297 
299  QgsProject *project = nullptr;
300 
301  private:
302 
303  // Ideally a unique_ptr, but cannot be due to use within QMap. Is cleaned up by QgsProcessingContext.
304  QgsProcessingLayerPostProcessorInterface *mPostProcessor = nullptr;
305 
306  };
307 
315  QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
316  {
317  return mLayersToLoadOnCompletion;
318  }
319 
329  bool willLoadLayerOnCompletion( const QString &layer ) const
330  {
331  return mLayersToLoadOnCompletion.contains( layer );
332  }
333 
341  void setLayersToLoadOnCompletion( const QMap< QString, QgsProcessingContext::LayerDetails > &layers );
342 
351  void addLayerToLoadOnCompletion( const QString &layer, const QgsProcessingContext::LayerDetails &details );
352 
367  {
368  return mLayersToLoadOnCompletion[ layer ];
369  }
370 
375  QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryCheck; }
376 
383  void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck check );
384 
392 #ifndef SIP_RUN
393  void setInvalidGeometryCallback( const std::function< void( const QgsFeature & ) > &callback ) { mInvalidGeometryCallback = callback; mUseDefaultInvalidGeometryCallback = false; }
394 #else
395  void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
396  % MethodCode
397  Py_BEGIN_ALLOW_THREADS
398 
399  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
400  {
401  SIP_BLOCK_THREADS
402  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
403  SIP_UNBLOCK_THREADS
404  } );
405 
406  Py_END_ALLOW_THREADS
407  % End
408 #endif
409 
417  SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback( QgsFeatureSource *source = nullptr ) const;
418 
424  SIP_SKIP std::function< void( const QgsFeature & ) > defaultInvalidGeometryCallbackForCheck( QgsFeatureRequest::InvalidGeometryCheck check, QgsFeatureSource *source = nullptr ) const;
425 
433 #ifndef SIP_RUN
434  void setTransformErrorCallback( const std::function< void( const QgsFeature & ) > &callback ) { mTransformErrorCallback = callback; }
435 #else
436  void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
437  % MethodCode
438  Py_BEGIN_ALLOW_THREADS
439 
440  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
441  {
442  SIP_BLOCK_THREADS
443  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
444  SIP_UNBLOCK_THREADS
445  } );
446 
447  Py_END_ALLOW_THREADS
448  % End
449 #endif
450 
458  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
459 
464  QString defaultEncoding() const { return mDefaultEncoding; }
465 
470  void setDefaultEncoding( const QString &encoding ) { mDefaultEncoding = encoding; }
471 
476  QgsProcessingFeedback *feedback() { return mFeedback; }
477 
486  void setFeedback( QgsProcessingFeedback *feedback ) { mFeedback = feedback; }
487 
492  QThread *thread() { return tempLayerStore.thread(); }
493 
500  void pushToThread( QThread *thread )
501  {
502  // cppcheck-suppress assertWithSideEffect
503  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" );
504  tempLayerStore.moveToThread( thread );
505  }
506 
514  void takeResultsFrom( QgsProcessingContext &context );
515 
526  QgsMapLayer *getMapLayer( const QString &identifier );
527 
536  QgsMapLayer *takeResultLayer( const QString &id ) SIP_TRANSFERBACK;
537 
556  QString preferredVectorFormat() const { return mPreferredVectorFormat; }
557 
573  void setPreferredVectorFormat( const QString &format ) { mPreferredVectorFormat = format; }
574 
593  QString preferredRasterFormat() const { return mPreferredRasterFormat; }
594 
610  void setPreferredRasterFormat( const QString &format ) { mPreferredRasterFormat = format; }
611 
612  private:
613 
614  QgsProcessingContext::Flags mFlags = QgsProcessingContext::Flags();
615  QPointer< QgsProject > mProject;
616  QgsCoordinateTransformContext mTransformContext;
617 
618  QString mEllipsoid;
621 
623  QgsMapLayerStore tempLayerStore;
624  QgsExpressionContext mExpressionContext;
625 
627  bool mUseDefaultInvalidGeometryCallback = true;
628  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
629 
630  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
631  QString mDefaultEncoding;
632  QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
633 
634  QPointer< QgsProcessingFeedback > mFeedback;
635 
636  QString mPreferredVectorFormat;
637  QString mPreferredRasterFormat;
638 
639 #ifdef SIP_RUN
641 #endif
642 };
643 
644 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::Flags )
645 
646 
647 
660 {
661  public:
662 
664 
679  virtual void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
680 
681 };
682 
683 
684 #endif // QGSPROCESSINGPARAMETERS_H
685 
686 
687 
688 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsProcessingContext::QgsProcessingContext
QgsProcessingContext(const QgsProcessingContext &other)=delete
QgsProcessingContext cannot be copied.
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:315
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:58
qgsfeaturerequest.h
QgsProcessingContext::LayerDetails::LayerDetails
LayerDetails()=default
Default constructor.
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:329
QgsProcessingContext::project
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Definition: qgsprocessingcontext.h:105
QgsProcessingContext::preferredVectorFormat
QString preferredVectorFormat() const
Returns the preferred vector format to use for vector outputs.
Definition: qgsprocessingcontext.h:556
QgsUnitTypes::DistanceUnknownUnit
@ DistanceUnknownUnit
Unknown distance unit.
Definition: qgsunittypes.h:78
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:38
QgsProject::transformContext
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:101
QgsFeatureSource
An interface for objects which provide features via a getFeatures method.
Definition: qgsfeaturesource.h:38
qgis.h
QgsProcessingContext::thread
QThread * thread()
Returns the thread in which the context lives.
Definition: qgsprocessingcontext.h:492
QgsProcessingUtils::LayerHint
LayerHint
Layer type hints.
Definition: qgsprocessingutils.h:137
QgsFeatureRequest::InvalidGeometryCheck
InvalidGeometryCheck
Handling of features with invalid geometries.
Definition: qgsfeaturerequest.h:100
QgsUnitTypes::DistanceUnit
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:68
QgsProcessingLayerPostProcessorInterface::~QgsProcessingLayerPostProcessorInterface
virtual ~QgsProcessingLayerPostProcessorInterface()=default
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:95
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:434
QgsMapLayerStore
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
Definition: qgsmaplayerstore.h:36
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:500
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:237
QgsProcessingContext::invalidGeometryCheck
QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const
Returns the behavior used for checking invalid geometries in input layers.
Definition: qgsprocessingcontext.h:375
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:44
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:458
QgsProcessingContext::defaultEncoding
QString defaultEncoding() const
Returns the default encoding to use for newly created files.
Definition: qgsprocessingcontext.h:464
QgsProcessingContext::setDefaultEncoding
void setDefaultEncoding(const QString &encoding)
Sets the default encoding to use for newly created files.
Definition: qgsprocessingcontext.h:470
QgsProcessingContext::LayerDetails::name
QString name
Friendly name for layer, possibly for use when loading layer into project.
Definition: qgsprocessingcontext.h:253
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:70
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:393
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:593
qgsprocessingfeedback.h
QgsProcessingContext::setExpressionContext
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
Definition: qgsprocessingcontext.h:143
QgsUnitTypes::AreaUnit
AreaUnit
Units of area.
Definition: qgsunittypes.h:94
QgsProcessingContext::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Definition: qgsprocessingcontext.h:149
QgsProcessingLayerPostProcessorInterface
An interface for layer post-processing handlers for execution following a processing algorithm operat...
Definition: qgsprocessingcontext.h:660
QgsProcessingContext::setTransformContext
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context.
Definition: qgsprocessingcontext.h:159
QgsProcessingContext::Flag
Flag
Flags that affect how processing algorithms are run.
Definition: qgsprocessingcontext.h:49
QgsProcessingContext::LayerDetails
Details for layers to load into projects.
Definition: qgsprocessingcontext.h:231
QgsProcessingContext::expressionContext
const QgsExpressionContext & expressionContext() const
Returns the expression context.
Definition: qgsprocessingcontext.h:138
QgsProcessingContext::temporaryLayerStore
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
Definition: qgsprocessingcontext.h:223
QgsProcessingContext::feedback
QgsProcessingFeedback * feedback()
Returns the associated feedback object.
Definition: qgsprocessingcontext.h:476
QgsProcessingContext::setFeedback
void setFeedback(QgsProcessingFeedback *feedback)
Sets an associated feedback object.
Definition: qgsprocessingcontext.h:486
qgsprocessingutils.h
QgsProcessingContext::LayerDetails::outputName
QString outputName
Associated output name from algorithm which generated the layer.
Definition: qgsprocessingcontext.h:265
QgsProcessingContext::operator=
QgsProcessingContext & operator=(const QgsProcessingContext &other)=delete
QgsProcessingContext cannot be copied.
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
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:366
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:133
QgsProcessingLayerPostProcessorInterface::postProcessLayer
virtual void postProcessLayer(QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback)=0
Post-processes the specified layer, following successful execution of a processing algorithm.
QgsProcessingContext::flags
QgsProcessingContext::Flags flags() const
Returns any flags set in the context.
Definition: qgsprocessingcontext.h:93
QgsFeatureRequest::GeometryNoCheck
@ GeometryNoCheck
No invalid geometry checking.
Definition: qgsfeaturerequest.h:101
QgsProcessingContext::setPreferredVectorFormat
void setPreferredVectorFormat(const QString &format)
Sets the preferred vector format to use for vector outputs.
Definition: qgsprocessingcontext.h:573
qgsexception.h
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
qgsmaplayerlistutils.h
QgsProcessingContext::setProject
void setProject(QgsProject *project)
Sets the project in which the algorithm will be executed.
Definition: qgsprocessingcontext.h:115
QgsProcessingContext::setPreferredRasterFormat
void setPreferredRasterFormat(const QString &format)
Sets the preferred raster format to use for vector outputs.
Definition: qgsprocessingcontext.h:610
qgsproject.h
QgsProcessingContext::setFlags
void setFlags(QgsProcessingContext::Flags flags)
Sets flags for the context.
Definition: qgsprocessingcontext.h:99
QgsUnitTypes::AreaUnknownUnit
@ AreaUnknownUnit
Unknown areal unit.
Definition: qgsunittypes.h:106
QgsProcessingUtils::LayerHint::UnknownType
@ UnknownType
Unknown layer type.