QGIS API Documentation 3.31.0-Master (9f23a2c1dc)
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"
25#include "qgsfeaturerequest.h"
27#include "qgsprocessingutils.h"
28
29
30#include <QThread>
31#include <QPointer>
32
34
45class CORE_EXPORT QgsProcessingContext
46{
47 public:
48
50 enum Flag
51 {
52 // For future API flexibility only and to avoid sip issues, remove when real entries are added to flags.
53 Unused = 1 << 0,
54 };
55 Q_DECLARE_FLAGS( Flags, Flag )
56
57
63 {
64 DefaultLevel = 0,
66 };
67
72
77
79
85 {
86 mFlags = other.mFlags;
87 mProject = other.mProject;
88 mTransformContext = other.mTransformContext;
89 mExpressionContext = other.mExpressionContext;
90 mExpressionContext.setLoadedLayerStore( &tempLayerStore );
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 mTemporaryFolderOverride = other.mTemporaryFolderOverride;
104 mMaximumThreads = other.mMaximumThreads;
105 }
106
111 QgsProcessingContext::Flags flags() const { return mFlags; }
112
117 void setFlags( QgsProcessingContext::Flags flags ) { mFlags = flags; }
118
123 QgsProject *project() const { return mProject; }
124
133 void setProject( QgsProject *project )
134 {
135 mProject = project;
136 if ( mProject )
137 {
138 mTransformContext = mProject->transformContext();
139 if ( mEllipsoid.isEmpty() )
140 mEllipsoid = mProject->ellipsoid();
141 if ( mDistanceUnit == Qgis::DistanceUnit::Unknown )
142 mDistanceUnit = mProject->distanceUnits();
143 if ( mAreaUnit == Qgis::AreaUnit::Unknown )
144 mAreaUnit = mProject->areaUnits();
145 }
146 }
147
151 QgsExpressionContext &expressionContext() { return mExpressionContext; }
152
156 SIP_SKIP const QgsExpressionContext &expressionContext() const { return mExpressionContext; }
157
161 void setExpressionContext( const QgsExpressionContext &context );
162
167 QgsCoordinateTransformContext transformContext() const { return mTransformContext; }
168
177 void setTransformContext( const QgsCoordinateTransformContext &context ) { mTransformContext = context; }
178
185 QString ellipsoid() const;
186
195 void setEllipsoid( const QString &ellipsoid );
196
204 Qgis::DistanceUnit distanceUnit() const;
205
215 void setDistanceUnit( Qgis::DistanceUnit unit );
216
224 Qgis::AreaUnit areaUnit() const;
225
235 void setAreaUnit( Qgis::AreaUnit areaUnit );
236
243 QgsDateTimeRange currentTimeRange() const;
244
251 void setCurrentTimeRange( const QgsDateTimeRange &currentTimeRange );
252
257 QgsMapLayerStore *temporaryLayerStore() { return &tempLayerStore; }
258
264 class CORE_EXPORT LayerDetails
265 {
266 public:
267
271 LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType )
272 : name( name )
273 , outputName( outputName )
274 , layerTypeHint( layerTypeHint )
275 , project( project )
276 {}
277
279 LayerDetails() = default;
280
287 QString name;
288
294 bool forceName = false;
295
299 QString outputName;
300
306 QString groupName;
307
315 int layerSortKey = 0;
316
323
329 QgsProcessingLayerPostProcessorInterface *postProcessor() const;
330
339 void setPostProcessor( QgsProcessingLayerPostProcessorInterface *processor SIP_TRANSFER );
340
346 void setOutputLayerName( QgsMapLayer *layer ) const;
347
349 QgsProject *project = nullptr;
350
351 private:
352
353 // Ideally a unique_ptr, but cannot be due to use within QMap. Is cleaned up by QgsProcessingContext.
354 QgsProcessingLayerPostProcessorInterface *mPostProcessor = nullptr;
355
356 };
357
365 QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
366 {
367 return mLayersToLoadOnCompletion;
368 }
369
379 bool willLoadLayerOnCompletion( const QString &layer ) const
380 {
381 return mLayersToLoadOnCompletion.contains( layer );
382 }
383
391 void setLayersToLoadOnCompletion( const QMap< QString, QgsProcessingContext::LayerDetails > &layers );
392
401 void addLayerToLoadOnCompletion( const QString &layer, const QgsProcessingContext::LayerDetails &details );
402
417 {
418 return mLayersToLoadOnCompletion[ layer ];
419 }
420
425 QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryCheck; }
426
433 void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck check );
434
442#ifndef SIP_RUN
443 void setInvalidGeometryCallback( const std::function< void( const QgsFeature & ) > &callback ) { mInvalidGeometryCallback = callback; mUseDefaultInvalidGeometryCallback = false; }
444#else
445 void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
446 % MethodCode
447 Py_BEGIN_ALLOW_THREADS
448
449 sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
450 {
451 SIP_BLOCK_THREADS
452 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
453 SIP_UNBLOCK_THREADS
454 } );
455
456 Py_END_ALLOW_THREADS
457 % End
458#endif
459
467 SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback( QgsFeatureSource *source = nullptr ) const;
468
474 SIP_SKIP std::function< void( const QgsFeature & ) > defaultInvalidGeometryCallbackForCheck( QgsFeatureRequest::InvalidGeometryCheck check, QgsFeatureSource *source = nullptr ) const;
475
483#ifndef SIP_RUN
484 void setTransformErrorCallback( const std::function< void( const QgsFeature & ) > &callback ) { mTransformErrorCallback = callback; }
485#else
486 void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
487 % MethodCode
488 Py_BEGIN_ALLOW_THREADS
489
490 sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
491 {
492 SIP_BLOCK_THREADS
493 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
494 SIP_UNBLOCK_THREADS
495 } );
496
497 Py_END_ALLOW_THREADS
498 % End
499#endif
500
508 std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
509
514 QString defaultEncoding() const { return mDefaultEncoding; }
515
520 void setDefaultEncoding( const QString &encoding ) { mDefaultEncoding = encoding; }
521
526 QgsProcessingFeedback *feedback() { return mFeedback; }
527
536 void setFeedback( QgsProcessingFeedback *feedback ) { mFeedback = feedback; }
537
542 QThread *thread() { return tempLayerStore.thread(); }
543
550 void pushToThread( QThread *thread )
551 {
552 // cppcheck-suppress assertWithSideEffect
553 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" );
554 tempLayerStore.moveToThread( thread );
555 }
556
564 void takeResultsFrom( QgsProcessingContext &context );
565
576 QgsMapLayer *getMapLayer( const QString &identifier );
577
586 QgsMapLayer *takeResultLayer( const QString &id ) SIP_TRANSFERBACK;
587
606 QString preferredVectorFormat() const { return mPreferredVectorFormat; }
607
623 void setPreferredVectorFormat( const QString &format ) { mPreferredVectorFormat = format; }
624
643 QString preferredRasterFormat() const { return mPreferredRasterFormat; }
644
660 void setPreferredRasterFormat( const QString &format ) { mPreferredRasterFormat = format; }
661
668 LogLevel logLevel() const;
669
676 void setLogLevel( LogLevel level );
677
687 QString temporaryFolder() const;
688
698 void setTemporaryFolder( const QString &folder );
699
711 int maximumThreads() const;
712
728 void setMaximumThreads( int threads );
729
735 QVariantMap exportToMap() const;
736
742 enum class ProcessArgumentFlag : int
743 {
744 IncludeProjectPath = 1 << 0,
745 };
746 Q_DECLARE_FLAGS( ProcessArgumentFlags, ProcessArgumentFlag )
747
748
753 QStringList asQgisProcessArguments( QgsProcessingContext::ProcessArgumentFlags flags = QgsProcessingContext::ProcessArgumentFlags() ) const;
754
755 private:
756
757 QgsProcessingContext::Flags mFlags = QgsProcessingContext::Flags();
758 QPointer< QgsProject > mProject;
759 QgsCoordinateTransformContext mTransformContext;
760
761 QString mEllipsoid;
762 Qgis::DistanceUnit mDistanceUnit = Qgis::DistanceUnit::Unknown;
763 Qgis::AreaUnit mAreaUnit = Qgis::AreaUnit::Unknown;
764
765 QgsDateTimeRange mCurrentTimeRange;
766
768 QgsMapLayerStore tempLayerStore;
769 QgsExpressionContext mExpressionContext;
770
772 bool mUseDefaultInvalidGeometryCallback = true;
773 std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
774
775 std::function< void( const QgsFeature & ) > mTransformErrorCallback;
776 QString mDefaultEncoding;
777 QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
778
779 QPointer< QgsProcessingFeedback > mFeedback;
780
781 QString mPreferredVectorFormat;
782 QString mPreferredRasterFormat;
783
784 LogLevel mLogLevel = DefaultLevel;
785
786 QString mTemporaryFolderOverride;
787 int mMaximumThreads = QThread::idealThreadCount();
788
789#ifdef SIP_RUN
791#endif
792};
793
794Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::Flags )
795Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::ProcessArgumentFlags )
796
797
798
811{
812 public:
813
815
830 virtual void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
831
832};
833
834
835#endif // QGSPROCESSINGPARAMETERS_H
836
837
838
839
DistanceUnit
Units of distance.
Definition: qgis.h:3225
AreaUnit
Units of area.
Definition: qgis.h:3263
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.
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.
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.
QThread * thread()
Returns the thread in which the context lives.
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...
QgsProcessingFeedback * feedback()
Returns the associated feedback object.
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.
QgsExpressionContext & expressionContext()
Returns the expression context.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
const QgsExpressionContext & expressionContext() const
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.
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.
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...
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
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.
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.
QgsProcessingContext & operator=(const QgsProcessingContext &other)=delete
QgsProcessingContext cannot be copied.
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.
std::function< void(const QgsFeature &) > transformErrorCallback() const
Returns the callback function to use when encountering a transform error when iterating features.
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
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:113
#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.