QGIS API Documentation 3.41.0-Master (af5edcb665c)
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
57
60
62
68 {
69 mFlags = other.mFlags;
70 mProject = other.mProject;
71 mTransformContext = other.mTransformContext;
72 mExpressionContext = other.mExpressionContext;
73 mExpressionContext.setLoadedLayerStore( &tempLayerStore );
74 mInvalidGeometryCallback = other.mInvalidGeometryCallback;
75 mUseDefaultInvalidGeometryCallback = other.mUseDefaultInvalidGeometryCallback;
76 mInvalidGeometryCheck = other.mInvalidGeometryCheck;
77 mTransformErrorCallback = other.mTransformErrorCallback;
78 mDefaultEncoding = other.mDefaultEncoding;
79 mFeedback = other.mFeedback;
80 mPreferredVectorFormat = other.mPreferredVectorFormat;
81 mPreferredRasterFormat = other.mPreferredRasterFormat;
82 mEllipsoid = other.mEllipsoid;
83 mDistanceUnit = other.mDistanceUnit;
84 mAreaUnit = other.mAreaUnit;
85 mLogLevel = other.mLogLevel;
86 mTemporaryFolderOverride = other.mTemporaryFolderOverride;
87 mMaximumThreads = other.mMaximumThreads;
88 mModelResult = other.mModelResult;
89 }
90
96
101 void setFlags( QgsProcessingContext::Flags flags ) SIP_HOLDGIL { mFlags = flags; }
102
107 QgsProject *project() const SIP_HOLDGIL { return mProject; }
108
118 {
119 mProject = project;
120 if ( mProject )
121 {
122 mTransformContext = mProject->transformContext();
123 if ( mEllipsoid.isEmpty() )
124 mEllipsoid = mProject->ellipsoid();
125 if ( mDistanceUnit == Qgis::DistanceUnit::Unknown )
126 mDistanceUnit = mProject->distanceUnits();
127 if ( mAreaUnit == Qgis::AreaUnit::Unknown )
128 mAreaUnit = mProject->areaUnits();
129 }
130 }
131
135 QgsExpressionContext &expressionContext() SIP_HOLDGIL { return mExpressionContext; }
136
140 SIP_SKIP const QgsExpressionContext &expressionContext() const { return mExpressionContext; }
141
145 void setExpressionContext( const QgsExpressionContext &context );
146
151 QgsCoordinateTransformContext transformContext() const SIP_HOLDGIL { return mTransformContext; }
152
161 void setTransformContext( const QgsCoordinateTransformContext &context ) SIP_HOLDGIL { mTransformContext = context; }
162
169 QString ellipsoid() const SIP_HOLDGIL;
170
179 void setEllipsoid( const QString &ellipsoid ) SIP_HOLDGIL;
180
188 Qgis::DistanceUnit distanceUnit() const SIP_HOLDGIL;
189
199 void setDistanceUnit( Qgis::DistanceUnit unit ) SIP_HOLDGIL;
200
208 Qgis::AreaUnit areaUnit() const SIP_HOLDGIL;
209
219 void setAreaUnit( Qgis::AreaUnit areaUnit ) SIP_HOLDGIL;
220
227 QgsDateTimeRange currentTimeRange() const SIP_HOLDGIL;
228
235 void setCurrentTimeRange( const QgsDateTimeRange &currentTimeRange ) SIP_HOLDGIL;
236
241 QgsMapLayerStore *temporaryLayerStore() SIP_HOLDGIL { return &tempLayerStore; }
242
247 class CORE_EXPORT LayerDetails
248 {
249 public:
250
254 LayerDetails( const QString &name, QgsProject *project, const QString &outputName = QString(), QgsProcessingUtils::LayerHint layerTypeHint = QgsProcessingUtils::LayerHint::UnknownType )
255 : name( name )
256 , outputName( outputName )
257 , layerTypeHint( layerTypeHint )
258 , project( project )
259 {}
260
261 LayerDetails() = default;
262
269 QString name;
270
276 bool forceName = false;
277
281 QString outputName;
282
288 QString groupName;
289
297 int layerSortKey = 0;
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 SIP_HOLDGIL
348 {
349 return mLayersToLoadOnCompletion;
350 }
351
361 bool willLoadLayerOnCompletion( const QString &layer ) const SIP_HOLDGIL
362 {
363 return mLayersToLoadOnCompletion.contains( layer );
364 }
365
373 void setLayersToLoadOnCompletion( const QMap< QString, QgsProcessingContext::LayerDetails > &layers ) SIP_HOLDGIL;
374
383 void addLayerToLoadOnCompletion( const QString &layer, const QgsProcessingContext::LayerDetails &details ) SIP_HOLDGIL;
384
399 {
400 return mLayersToLoadOnCompletion[ layer ];
401 }
402
407 Qgis::InvalidGeometryCheck invalidGeometryCheck() const SIP_HOLDGIL { return mInvalidGeometryCheck; }
408
415 void setInvalidGeometryCheck( Qgis::InvalidGeometryCheck check );
416
423#ifndef SIP_RUN
424 void setInvalidGeometryCallback( const std::function< void( const QgsFeature & ) > &callback ) { mInvalidGeometryCallback = callback; mUseDefaultInvalidGeometryCallback = false; }
425#else
426 void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
427 % MethodCode
428 Py_BEGIN_ALLOW_THREADS
429
430 sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
431 {
432 SIP_BLOCK_THREADS
433 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
434 SIP_UNBLOCK_THREADS
435 } );
436
437 Py_END_ALLOW_THREADS
438 % End
439#endif
440
447 SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback( QgsFeatureSource *source = nullptr ) const;
448
454 SIP_SKIP std::function< void( const QgsFeature & ) > defaultInvalidGeometryCallbackForCheck( Qgis::InvalidGeometryCheck check, QgsFeatureSource *source = nullptr ) const;
455
462#ifndef SIP_RUN
463 void setTransformErrorCallback( const std::function< void( const QgsFeature & ) > &callback ) { mTransformErrorCallback = callback; }
464#else
465 void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
466 % MethodCode
467 Py_BEGIN_ALLOW_THREADS
468
469 sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
470 {
471 SIP_BLOCK_THREADS
472 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
473 SIP_UNBLOCK_THREADS
474 } );
475
476 Py_END_ALLOW_THREADS
477 % End
478#endif
479
486 std::function< void( const QgsFeature & ) > transformErrorCallback() const SIP_SKIP { return mTransformErrorCallback; }
487
492 QString defaultEncoding() const SIP_HOLDGIL { return mDefaultEncoding; }
493
498 void setDefaultEncoding( const QString &encoding ) SIP_HOLDGIL { mDefaultEncoding = encoding; }
499
505
514 void setFeedback( QgsProcessingFeedback *feedback ) SIP_HOLDGIL { mFeedback = feedback; }
515
520 QThread *thread() SIP_HOLDGIL { return tempLayerStore.thread(); }
521
528 void pushToThread( QThread *thread )
529 {
530 // cppcheck-suppress assertWithSideEffect
531 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" );
532 tempLayerStore.moveToThread( thread );
533 }
534
542 void takeResultsFrom( QgsProcessingContext &context );
543
554 QgsMapLayer *getMapLayer( const QString &identifier );
555
564 QgsMapLayer *takeResultLayer( const QString &id ) SIP_TRANSFERBACK;
565
584 QString preferredVectorFormat() const SIP_HOLDGIL { return mPreferredVectorFormat; }
585
601 void setPreferredVectorFormat( const QString &format ) SIP_HOLDGIL { mPreferredVectorFormat = format; }
602
621 QString preferredRasterFormat() const SIP_HOLDGIL { return mPreferredRasterFormat; }
622
638 void setPreferredRasterFormat( const QString &format ) SIP_HOLDGIL { mPreferredRasterFormat = format; }
639
646 Qgis::ProcessingLogLevel logLevel() const SIP_HOLDGIL;
647
654 void setLogLevel( Qgis::ProcessingLogLevel level ) SIP_HOLDGIL;
655
665 QString temporaryFolder() const SIP_HOLDGIL;
666
676 void setTemporaryFolder( const QString &folder ) SIP_HOLDGIL;
677
689 int maximumThreads() const SIP_HOLDGIL;
690
706 void setMaximumThreads( int threads ) SIP_HOLDGIL;
707
713 QVariantMap exportToMap() const SIP_HOLDGIL;
714
720 enum class ProcessArgumentFlag : int SIP_ENUM_BASETYPE( IntFlag )
721 {
722 IncludeProjectPath = 1 << 0,
723 };
725
726
731 QStringList asQgisProcessArguments( QgsProcessingContext::ProcessArgumentFlags flags = QgsProcessingContext::ProcessArgumentFlags() ) const;
732
749 QgsProcessingModelInitialRunConfig *modelInitialRunConfig() SIP_SKIP;
750
763 std::unique_ptr< QgsProcessingModelInitialRunConfig > takeModelInitialRunConfig() SIP_SKIP;
764
778 void setModelInitialRunConfig( std::unique_ptr< QgsProcessingModelInitialRunConfig > config ) SIP_SKIP;
779
785 QgsProcessingModelResult modelResult() const { return mModelResult; }
786
796
797 private:
798
800 QPointer< QgsProject > mProject;
801 QgsCoordinateTransformContext mTransformContext;
802
803 QString mEllipsoid;
806
807 QgsDateTimeRange mCurrentTimeRange;
808
810 QgsMapLayerStore tempLayerStore;
811 QgsExpressionContext mExpressionContext;
812
814 bool mUseDefaultInvalidGeometryCallback = true;
815 std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
816
817 std::function< void( const QgsFeature & ) > mTransformErrorCallback;
818 QString mDefaultEncoding;
819 QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
820
821 QPointer< QgsProcessingFeedback > mFeedback;
822
823 QString mPreferredVectorFormat;
824 QString mPreferredRasterFormat;
825
827
828 QString mTemporaryFolderOverride;
829 int mMaximumThreads = QThread::idealThreadCount();
830
831 std::unique_ptr< QgsProcessingModelInitialRunConfig > mModelConfig;
832 QgsProcessingModelResult mModelResult;
833
834#ifdef SIP_RUN
836#endif
837};
838
841
842
843
856{
857 public:
858
860
875 virtual void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
876
877};
878
879
880#endif // QGSPROCESSINGPARAMETERS_H
881
882
883
884
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
DistanceUnit
Units of distance.
Definition qgis.h:4740
@ Unknown
Unknown distance unit.
AreaUnit
Units of area.
Definition qgis.h:4817
@ Unknown
Unknown areal unit.
InvalidGeometryCheck
Methods for handling of features with invalid geometries.
Definition qgis.h:2135
@ NoCheck
No invalid geometry checking.
ProcessingLogLevel
Logging level for algorithms to use when pushing feedback messages.
Definition qgis.h:3456
@ 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:76
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.
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
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
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)