QGIS API Documentation 3.39.0-Master (be2050b798e)
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"
26#include "qgsprocessingutils.h"
29
30#include <QThread>
31#include <QPointer>
32
34
44class CORE_EXPORT QgsProcessingContext
45{
46 public:
47
49 enum Flag SIP_ENUM_BASETYPE( IntFlag )
50 {
51 // For future API flexibility only and to avoid sip issues, remove when real entries are added to flags.
52 Unused = 1 << 0,
53 };
54 Q_DECLARE_FLAGS( Flags, Flag )
55
56
60
65
67
73 {
74 mFlags = other.mFlags;
75 mProject = other.mProject;
76 mTransformContext = other.mTransformContext;
77 mExpressionContext = other.mExpressionContext;
78 mExpressionContext.setLoadedLayerStore( &tempLayerStore );
79 mInvalidGeometryCallback = other.mInvalidGeometryCallback;
80 mUseDefaultInvalidGeometryCallback = other.mUseDefaultInvalidGeometryCallback;
81 mInvalidGeometryCheck = other.mInvalidGeometryCheck;
82 mTransformErrorCallback = other.mTransformErrorCallback;
83 mDefaultEncoding = other.mDefaultEncoding;
84 mFeedback = other.mFeedback;
85 mPreferredVectorFormat = other.mPreferredVectorFormat;
86 mPreferredRasterFormat = other.mPreferredRasterFormat;
87 mEllipsoid = other.mEllipsoid;
88 mDistanceUnit = other.mDistanceUnit;
89 mAreaUnit = other.mAreaUnit;
90 mLogLevel = other.mLogLevel;
91 mTemporaryFolderOverride = other.mTemporaryFolderOverride;
92 mMaximumThreads = other.mMaximumThreads;
93 mModelResult = other.mModelResult;
94 }
95
101
106 void setFlags( QgsProcessingContext::Flags flags ) SIP_HOLDGIL { mFlags = flags; }
107
112 QgsProject *project() const SIP_HOLDGIL { return mProject; }
113
123 {
124 mProject = project;
125 if ( mProject )
126 {
127 mTransformContext = mProject->transformContext();
128 if ( mEllipsoid.isEmpty() )
129 mEllipsoid = mProject->ellipsoid();
130 if ( mDistanceUnit == Qgis::DistanceUnit::Unknown )
131 mDistanceUnit = mProject->distanceUnits();
132 if ( mAreaUnit == Qgis::AreaUnit::Unknown )
133 mAreaUnit = mProject->areaUnits();
134 }
135 }
136
140 QgsExpressionContext &expressionContext() SIP_HOLDGIL { return mExpressionContext; }
141
145 SIP_SKIP const QgsExpressionContext &expressionContext() const { return mExpressionContext; }
146
150 void setExpressionContext( const QgsExpressionContext &context );
151
156 QgsCoordinateTransformContext transformContext() const SIP_HOLDGIL { return mTransformContext; }
157
166 void setTransformContext( const QgsCoordinateTransformContext &context ) SIP_HOLDGIL { mTransformContext = context; }
167
174 QString ellipsoid() const SIP_HOLDGIL;
175
184 void setEllipsoid( const QString &ellipsoid ) SIP_HOLDGIL;
185
193 Qgis::DistanceUnit distanceUnit() const SIP_HOLDGIL;
194
204 void setDistanceUnit( Qgis::DistanceUnit unit ) SIP_HOLDGIL;
205
213 Qgis::AreaUnit areaUnit() const SIP_HOLDGIL;
214
224 void setAreaUnit( Qgis::AreaUnit areaUnit ) SIP_HOLDGIL;
225
232 QgsDateTimeRange currentTimeRange() const SIP_HOLDGIL;
233
240 void setCurrentTimeRange( const QgsDateTimeRange &currentTimeRange ) SIP_HOLDGIL;
241
246 QgsMapLayerStore *temporaryLayerStore() SIP_HOLDGIL { return &tempLayerStore; }
247
252 class CORE_EXPORT LayerDetails
253 {
254 public:
255
259 LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType )
260 : name( name )
261 , outputName( outputName )
262 , layerTypeHint( layerTypeHint )
263 , project( project )
264 {}
265
267 LayerDetails() = default;
268
275 QString name;
276
282 bool forceName = false;
283
287 QString outputName;
288
294 QString groupName;
295
303 int layerSortKey = 0;
304
311
317 QgsProcessingLayerPostProcessorInterface *postProcessor() const;
318
327 void setPostProcessor( QgsProcessingLayerPostProcessorInterface *processor SIP_TRANSFER );
328
334 void setOutputLayerName( QgsMapLayer *layer ) const;
335
337 QgsProject *project = nullptr;
338
339 private:
340
341 // Ideally a unique_ptr, but cannot be due to use within QMap. Is cleaned up by QgsProcessingContext.
342 QgsProcessingLayerPostProcessorInterface *mPostProcessor = nullptr;
343
344 };
345
353 QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const SIP_HOLDGIL
354 {
355 return mLayersToLoadOnCompletion;
356 }
357
367 bool willLoadLayerOnCompletion( const QString &layer ) const SIP_HOLDGIL
368 {
369 return mLayersToLoadOnCompletion.contains( layer );
370 }
371
379 void setLayersToLoadOnCompletion( const QMap< QString, QgsProcessingContext::LayerDetails > &layers ) SIP_HOLDGIL;
380
389 void addLayerToLoadOnCompletion( const QString &layer, const QgsProcessingContext::LayerDetails &details ) SIP_HOLDGIL;
390
405 {
406 return mLayersToLoadOnCompletion[ layer ];
407 }
408
413 Qgis::InvalidGeometryCheck invalidGeometryCheck() const SIP_HOLDGIL { return mInvalidGeometryCheck; }
414
421 void setInvalidGeometryCheck( Qgis::InvalidGeometryCheck check );
422
429#ifndef SIP_RUN
430 void setInvalidGeometryCallback( const std::function< void( const QgsFeature & ) > &callback ) { mInvalidGeometryCallback = callback; mUseDefaultInvalidGeometryCallback = false; }
431#else
432 void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
433 % MethodCode
434 Py_BEGIN_ALLOW_THREADS
435
436 sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
437 {
438 SIP_BLOCK_THREADS
439 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
440 SIP_UNBLOCK_THREADS
441 } );
442
443 Py_END_ALLOW_THREADS
444 % End
445#endif
446
453 SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback( QgsFeatureSource *source = nullptr ) const;
454
460 SIP_SKIP std::function< void( const QgsFeature & ) > defaultInvalidGeometryCallbackForCheck( Qgis::InvalidGeometryCheck check, QgsFeatureSource *source = nullptr ) const;
461
468#ifndef SIP_RUN
469 void setTransformErrorCallback( const std::function< void( const QgsFeature & ) > &callback ) { mTransformErrorCallback = callback; }
470#else
471 void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
472 % MethodCode
473 Py_BEGIN_ALLOW_THREADS
474
475 sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
476 {
477 SIP_BLOCK_THREADS
478 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
479 SIP_UNBLOCK_THREADS
480 } );
481
482 Py_END_ALLOW_THREADS
483 % End
484#endif
485
492 std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
493
498 QString defaultEncoding() const SIP_HOLDGIL { return mDefaultEncoding; }
499
504 void setDefaultEncoding( const QString &encoding ) SIP_HOLDGIL { mDefaultEncoding = encoding; }
505
511
520 void setFeedback( QgsProcessingFeedback *feedback ) SIP_HOLDGIL { mFeedback = feedback; }
521
526 QThread *thread() SIP_HOLDGIL { return tempLayerStore.thread(); }
527
534 void pushToThread( QThread *thread )
535 {
536 // cppcheck-suppress assertWithSideEffect
537 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" );
538 tempLayerStore.moveToThread( thread );
539 }
540
548 void takeResultsFrom( QgsProcessingContext &context );
549
560 QgsMapLayer *getMapLayer( const QString &identifier );
561
570 QgsMapLayer *takeResultLayer( const QString &id ) SIP_TRANSFERBACK;
571
590 QString preferredVectorFormat() const SIP_HOLDGIL { return mPreferredVectorFormat; }
591
607 void setPreferredVectorFormat( const QString &format ) SIP_HOLDGIL { mPreferredVectorFormat = format; }
608
627 QString preferredRasterFormat() const SIP_HOLDGIL { return mPreferredRasterFormat; }
628
644 void setPreferredRasterFormat( const QString &format ) SIP_HOLDGIL { mPreferredRasterFormat = format; }
645
652 Qgis::ProcessingLogLevel logLevel() const SIP_HOLDGIL;
653
660 void setLogLevel( Qgis::ProcessingLogLevel level ) SIP_HOLDGIL;
661
671 QString temporaryFolder() const SIP_HOLDGIL;
672
682 void setTemporaryFolder( const QString &folder ) SIP_HOLDGIL;
683
695 int maximumThreads() const SIP_HOLDGIL;
696
712 void setMaximumThreads( int threads ) SIP_HOLDGIL;
713
719 QVariantMap exportToMap() const SIP_HOLDGIL;
720
726 enum class ProcessArgumentFlag : int SIP_ENUM_BASETYPE( IntFlag )
727 {
728 IncludeProjectPath = 1 << 0,
729 };
731
732
737 QStringList asQgisProcessArguments( QgsProcessingContext::ProcessArgumentFlags flags = QgsProcessingContext::ProcessArgumentFlags() ) const;
738
755 QgsProcessingModelInitialRunConfig *modelInitialRunConfig() SIP_SKIP;
756
769 std::unique_ptr< QgsProcessingModelInitialRunConfig > takeModelInitialRunConfig() SIP_SKIP;
770
784 void setModelInitialRunConfig( std::unique_ptr< QgsProcessingModelInitialRunConfig > config ) SIP_SKIP;
785
791 QgsProcessingModelResult modelResult() const { return mModelResult; }
792
802
803 private:
804
806 QPointer< QgsProject > mProject;
807 QgsCoordinateTransformContext mTransformContext;
808
809 QString mEllipsoid;
812
813 QgsDateTimeRange mCurrentTimeRange;
814
816 QgsMapLayerStore tempLayerStore;
817 QgsExpressionContext mExpressionContext;
818
820 bool mUseDefaultInvalidGeometryCallback = true;
821 std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
822
823 std::function< void( const QgsFeature & ) > mTransformErrorCallback;
824 QString mDefaultEncoding;
825 QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
826
827 QPointer< QgsProcessingFeedback > mFeedback;
828
829 QString mPreferredVectorFormat;
830 QString mPreferredRasterFormat;
831
833
834 QString mTemporaryFolderOverride;
835 int mMaximumThreads = QThread::idealThreadCount();
836
837 std::unique_ptr< QgsProcessingModelInitialRunConfig > mModelConfig;
838 QgsProcessingModelResult mModelResult;
839
840#ifdef SIP_RUN
842#endif
843};
844
847
848
849
862{
863 public:
864
866
881 virtual void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
882
883};
884
885
886#endif // QGSPROCESSINGPARAMETERS_H
887
888
889
890
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
DistanceUnit
Units of distance.
Definition qgis.h:4463
@ Unknown
Unknown distance unit.
AreaUnit
Units of area.
Definition qgis.h:4501
@ Unknown
Unknown areal unit.
InvalidGeometryCheck
Methods for handling of features with invalid geometries.
Definition qgis.h:1955
@ NoCheck
No invalid geometry checking.
ProcessingLogLevel
Logging level for algorithms to use when pushing feedback messages.
Definition qgis.h:3202
@ 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:58
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.
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...
QgsProcessingModelResult & modelResult()
Returns a reference to the model results, populated when the context is used to run a model algorithm...
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().
QFlags< ProcessArgumentFlag > ProcessArgumentFlags
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.
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...
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.
Configuration settings which control how a Processing model is executed.
Encapsulates the results of running a Processing model.
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)