QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
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,
67 };
68
73
78
80
86 {
87 mFlags = other.mFlags;
88 mProject = other.mProject;
89 mTransformContext = other.mTransformContext;
90 mExpressionContext = other.mExpressionContext;
91 mExpressionContext.setLoadedLayerStore( &tempLayerStore );
92 mInvalidGeometryCallback = other.mInvalidGeometryCallback;
93 mUseDefaultInvalidGeometryCallback = other.mUseDefaultInvalidGeometryCallback;
94 mInvalidGeometryCheck = other.mInvalidGeometryCheck;
95 mTransformErrorCallback = other.mTransformErrorCallback;
96 mDefaultEncoding = other.mDefaultEncoding;
97 mFeedback = other.mFeedback;
98 mPreferredVectorFormat = other.mPreferredVectorFormat;
99 mPreferredRasterFormat = other.mPreferredRasterFormat;
100 mEllipsoid = other.mEllipsoid;
101 mDistanceUnit = other.mDistanceUnit;
102 mAreaUnit = other.mAreaUnit;
103 mLogLevel = other.mLogLevel;
104 mTemporaryFolderOverride = other.mTemporaryFolderOverride;
105 mMaximumThreads = other.mMaximumThreads;
106 }
107
112 QgsProcessingContext::Flags flags() const { return mFlags; }
113
118 void setFlags( QgsProcessingContext::Flags flags ) { mFlags = flags; }
119
124 QgsProject *project() const { return mProject; }
125
134 void setProject( QgsProject *project )
135 {
136 mProject = project;
137 if ( mProject )
138 {
139 mTransformContext = mProject->transformContext();
140 if ( mEllipsoid.isEmpty() )
141 mEllipsoid = mProject->ellipsoid();
142 if ( mDistanceUnit == Qgis::DistanceUnit::Unknown )
143 mDistanceUnit = mProject->distanceUnits();
144 if ( mAreaUnit == Qgis::AreaUnit::Unknown )
145 mAreaUnit = mProject->areaUnits();
146 }
147 }
148
152 QgsExpressionContext &expressionContext() { return mExpressionContext; }
153
157 SIP_SKIP const QgsExpressionContext &expressionContext() const { return mExpressionContext; }
158
162 void setExpressionContext( const QgsExpressionContext &context );
163
168 QgsCoordinateTransformContext transformContext() const { return mTransformContext; }
169
178 void setTransformContext( const QgsCoordinateTransformContext &context ) { mTransformContext = context; }
179
186 QString ellipsoid() const;
187
196 void setEllipsoid( const QString &ellipsoid );
197
205 Qgis::DistanceUnit distanceUnit() const;
206
216 void setDistanceUnit( Qgis::DistanceUnit unit );
217
225 Qgis::AreaUnit areaUnit() const;
226
236 void setAreaUnit( Qgis::AreaUnit areaUnit );
237
244 QgsDateTimeRange currentTimeRange() const;
245
252 void setCurrentTimeRange( const QgsDateTimeRange &currentTimeRange );
253
258 QgsMapLayerStore *temporaryLayerStore() { return &tempLayerStore; }
259
265 class CORE_EXPORT LayerDetails
266 {
267 public:
268
272 LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType )
273 : name( name )
274 , outputName( outputName )
275 , layerTypeHint( layerTypeHint )
276 , project( project )
277 {}
278
280 LayerDetails() = default;
281
288 QString name;
289
295 bool forceName = false;
296
300 QString outputName;
301
307 QString groupName;
308
316 int layerSortKey = 0;
317
324
330 QgsProcessingLayerPostProcessorInterface *postProcessor() const;
331
340 void setPostProcessor( QgsProcessingLayerPostProcessorInterface *processor SIP_TRANSFER );
341
347 void setOutputLayerName( QgsMapLayer *layer ) const;
348
350 QgsProject *project = nullptr;
351
352 private:
353
354 // Ideally a unique_ptr, but cannot be due to use within QMap. Is cleaned up by QgsProcessingContext.
355 QgsProcessingLayerPostProcessorInterface *mPostProcessor = nullptr;
356
357 };
358
366 QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
367 {
368 return mLayersToLoadOnCompletion;
369 }
370
380 bool willLoadLayerOnCompletion( const QString &layer ) const
381 {
382 return mLayersToLoadOnCompletion.contains( layer );
383 }
384
392 void setLayersToLoadOnCompletion( const QMap< QString, QgsProcessingContext::LayerDetails > &layers );
393
402 void addLayerToLoadOnCompletion( const QString &layer, const QgsProcessingContext::LayerDetails &details );
403
418 {
419 return mLayersToLoadOnCompletion[ layer ];
420 }
421
426 QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryCheck; }
427
434 void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck check );
435
443#ifndef SIP_RUN
444 void setInvalidGeometryCallback( const std::function< void( const QgsFeature & ) > &callback ) { mInvalidGeometryCallback = callback; mUseDefaultInvalidGeometryCallback = false; }
445#else
446 void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
447 % MethodCode
448 Py_BEGIN_ALLOW_THREADS
449
450 sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
451 {
452 SIP_BLOCK_THREADS
453 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
454 SIP_UNBLOCK_THREADS
455 } );
456
457 Py_END_ALLOW_THREADS
458 % End
459#endif
460
468 SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback( QgsFeatureSource *source = nullptr ) const;
469
475 SIP_SKIP std::function< void( const QgsFeature & ) > defaultInvalidGeometryCallbackForCheck( QgsFeatureRequest::InvalidGeometryCheck check, QgsFeatureSource *source = nullptr ) const;
476
484#ifndef SIP_RUN
485 void setTransformErrorCallback( const std::function< void( const QgsFeature & ) > &callback ) { mTransformErrorCallback = callback; }
486#else
487 void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
488 % MethodCode
489 Py_BEGIN_ALLOW_THREADS
490
491 sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
492 {
493 SIP_BLOCK_THREADS
494 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
495 SIP_UNBLOCK_THREADS
496 } );
497
498 Py_END_ALLOW_THREADS
499 % End
500#endif
501
509 std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
510
515 QString defaultEncoding() const { return mDefaultEncoding; }
516
521 void setDefaultEncoding( const QString &encoding ) { mDefaultEncoding = encoding; }
522
527 QgsProcessingFeedback *feedback() { return mFeedback; }
528
537 void setFeedback( QgsProcessingFeedback *feedback ) { mFeedback = feedback; }
538
543 QThread *thread() { return tempLayerStore.thread(); }
544
551 void pushToThread( QThread *thread )
552 {
553 // cppcheck-suppress assertWithSideEffect
554 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" );
555 tempLayerStore.moveToThread( thread );
556 }
557
565 void takeResultsFrom( QgsProcessingContext &context );
566
577 QgsMapLayer *getMapLayer( const QString &identifier );
578
587 QgsMapLayer *takeResultLayer( const QString &id ) SIP_TRANSFERBACK;
588
607 QString preferredVectorFormat() const { return mPreferredVectorFormat; }
608
624 void setPreferredVectorFormat( const QString &format ) { mPreferredVectorFormat = format; }
625
644 QString preferredRasterFormat() const { return mPreferredRasterFormat; }
645
661 void setPreferredRasterFormat( const QString &format ) { mPreferredRasterFormat = format; }
662
669 LogLevel logLevel() const;
670
677 void setLogLevel( LogLevel level );
678
688 QString temporaryFolder() const;
689
699 void setTemporaryFolder( const QString &folder );
700
712 int maximumThreads() const;
713
729 void setMaximumThreads( int threads );
730
736 QVariantMap exportToMap() const;
737
743 enum class ProcessArgumentFlag : int
744 {
745 IncludeProjectPath = 1 << 0,
746 };
747 Q_DECLARE_FLAGS( ProcessArgumentFlags, ProcessArgumentFlag )
748
749
754 QStringList asQgisProcessArguments( QgsProcessingContext::ProcessArgumentFlags flags = QgsProcessingContext::ProcessArgumentFlags() ) const;
755
756 private:
757
758 QgsProcessingContext::Flags mFlags = QgsProcessingContext::Flags();
759 QPointer< QgsProject > mProject;
760 QgsCoordinateTransformContext mTransformContext;
761
762 QString mEllipsoid;
765
766 QgsDateTimeRange mCurrentTimeRange;
767
769 QgsMapLayerStore tempLayerStore;
770 QgsExpressionContext mExpressionContext;
771
773 bool mUseDefaultInvalidGeometryCallback = true;
774 std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
775
776 std::function< void( const QgsFeature & ) > mTransformErrorCallback;
777 QString mDefaultEncoding;
778 QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
779
780 QPointer< QgsProcessingFeedback > mFeedback;
781
782 QString mPreferredVectorFormat;
783 QString mPreferredRasterFormat;
784
785 LogLevel mLogLevel = DefaultLevel;
786
787 QString mTemporaryFolderOverride;
788 int mMaximumThreads = QThread::idealThreadCount();
789
790#ifdef SIP_RUN
792#endif
793};
794
795Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::Flags )
796Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::ProcessArgumentFlags )
797
798
799
812{
813 public:
814
816
831 virtual void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
832
833};
834
835
836#endif // QGSPROCESSINGPARAMETERS_H
837
838
839
840
DistanceUnit
Units of distance.
Definition qgis.h:3496
@ Unknown
Unknown distance unit.
AreaUnit
Units of area.
Definition qgis.h:3534
@ Unknown
Unknown areal unit.
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:74
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.
@ ModelDebug
Model debug level logging. Includes verbose logging and other outputs useful for debugging models (si...
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(QgsTextRendererUtils::CurvedTextFlags)