QGIS API Documentation 4.1.0-Master (60fea48833c)
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.h"
22#include "qgis_core.h"
27#include "qgsprocessingutils.h"
28#include "qgsproject.h"
29
30#include <QPointer>
31#include <QThread>
32
34
43
44class CORE_EXPORT QgsProcessingContext
45{
46 public:
48 enum Flag SIP_ENUM_BASETYPE( IntFlag )
49 {
50 // For future API flexibility only and to avoid sip issues, remove when real entries are added to flags.
51 Unused = 1 << 0,
52 };
53 Q_DECLARE_FLAGS( Flags, Flag )
54
56
59
61
67 {
68 mFlags = other.mFlags;
69 mProject = other.mProject;
70 mTransformContext = other.mTransformContext;
71 mExpressionContext = other.mExpressionContext;
72 mExpressionContext.setLoadedLayerStore( &tempLayerStore );
73 mInvalidGeometryCallback = other.mInvalidGeometryCallback;
74 mUseDefaultInvalidGeometryCallback = other.mUseDefaultInvalidGeometryCallback;
75 mInvalidGeometryCheck = other.mInvalidGeometryCheck;
76 mTransformErrorCallback = other.mTransformErrorCallback;
77 mDefaultEncoding = other.mDefaultEncoding;
78 mFeedback = other.mFeedback;
79 mPreferredVectorFormat = other.mPreferredVectorFormat;
80 mPreferredRasterFormat = other.mPreferredRasterFormat;
81 mEllipsoid = other.mEllipsoid;
82 mDistanceUnit = other.mDistanceUnit;
83 mAreaUnit = other.mAreaUnit;
84 mLogLevel = other.mLogLevel;
85 mTemporaryFolderOverride = other.mTemporaryFolderOverride;
86 mMaximumThreads = other.mMaximumThreads;
87 mModelResult = other.mModelResult;
88 }
89
90 // clang-format off
96 // clang-format on
97
103
108 QgsProject *project() const SIP_HOLDGIL { return mProject; }
109
119 {
120 mProject = project;
121 if ( mProject )
122 {
123 mTransformContext = mProject->transformContext();
124 if ( mEllipsoid.isEmpty() )
125 mEllipsoid = mProject->ellipsoid();
126 if ( mDistanceUnit == Qgis::DistanceUnit::Unknown )
127 mDistanceUnit = mProject->distanceUnits();
128 if ( mAreaUnit == Qgis::AreaUnit::Unknown )
129 mAreaUnit = mProject->areaUnits();
130 }
131 }
132
136 QgsExpressionContext &expressionContext() SIP_HOLDGIL { return mExpressionContext; }
137
141 SIP_SKIP const QgsExpressionContext &expressionContext() const { return mExpressionContext; }
142
146 void setExpressionContext( const QgsExpressionContext &context );
147
152 QgsCoordinateTransformContext transformContext() const SIP_HOLDGIL { return mTransformContext; }
153
162 void setTransformContext( const QgsCoordinateTransformContext &context ) SIP_HOLDGIL { mTransformContext = context; }
163
170 QString ellipsoid() const SIP_HOLDGIL;
171
180 void setEllipsoid( const QString &ellipsoid ) SIP_HOLDGIL;
181
189 Qgis::DistanceUnit distanceUnit() const SIP_HOLDGIL;
190
200 void setDistanceUnit( Qgis::DistanceUnit unit ) SIP_HOLDGIL;
201
209 Qgis::AreaUnit areaUnit() const SIP_HOLDGIL;
210
220 void setAreaUnit( Qgis::AreaUnit areaUnit ) SIP_HOLDGIL;
221
228 QgsDateTimeRange currentTimeRange() const SIP_HOLDGIL;
229
236 void setCurrentTimeRange( const QgsDateTimeRange &currentTimeRange ) SIP_HOLDGIL;
237
242 QgsMapLayerStore *temporaryLayerStore() SIP_HOLDGIL { return &tempLayerStore; }
243
248 class CORE_EXPORT LayerDetails
249 {
250 public:
251
261
262 LayerDetails() = default;
263
270 QString name;
271
277 bool forceName = false;
278
282 QString outputName;
283
289 QString groupName;
290
299
306
313
323
329 void setOutputLayerName( QgsMapLayer *layer ) const;
330
332 QgsProject *project = nullptr;
333
334 private:
335
336 // Ideally a unique_ptr, but cannot be due to use within QMap. Is cleaned up by QgsProcessingContext.
337 QgsProcessingLayerPostProcessorInterface *mPostProcessor = nullptr;
338
339 };
340
348 QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const SIP_HOLDGIL
349 {
350 return mLayersToLoadOnCompletion;
351 }
352
362 bool willLoadLayerOnCompletion( const QString &layer ) const SIP_HOLDGIL
363 {
364 return mLayersToLoadOnCompletion.contains( layer );
365 }
366
374 void setLayersToLoadOnCompletion( const QMap< QString, QgsProcessingContext::LayerDetails > &layers ) SIP_HOLDGIL;
375
384 void addLayerToLoadOnCompletion( const QString &layer, const QgsProcessingContext::LayerDetails &details ) SIP_HOLDGIL;
385
400 {
401 return mLayersToLoadOnCompletion[ layer ];
402 }
403
408 Qgis::InvalidGeometryCheck invalidGeometryCheck() const SIP_HOLDGIL { return mInvalidGeometryCheck; }
409
416 void setInvalidGeometryCheck( Qgis::InvalidGeometryCheck check );
417
424#ifndef SIP_RUN
425 void setInvalidGeometryCallback( const std::function< void( const QgsFeature & ) > &callback ) { mInvalidGeometryCallback = callback; mUseDefaultInvalidGeometryCallback = false; }
426#else
427// clang-format off
428 void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
429 % MethodCode
430 Py_BEGIN_ALLOW_THREADS
431
432 sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
433 {
434 SIP_BLOCK_THREADS
435 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
436 SIP_UNBLOCK_THREADS
437 } );
438
439 Py_END_ALLOW_THREADS
440 % End
441// clang-format on
442#endif
443
450 SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback( QgsFeatureSource *source = nullptr ) const;
451
457 SIP_SKIP std::function< void( const QgsFeature & ) > defaultInvalidGeometryCallbackForCheck( Qgis::InvalidGeometryCheck check, QgsFeatureSource *source = nullptr ) const;
458
465#ifndef SIP_RUN
466 void setTransformErrorCallback( const std::function< void( const QgsFeature & ) > &callback ) { mTransformErrorCallback = callback; }
467#else
468// clang-format off
469 void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
470 % MethodCode
471 Py_BEGIN_ALLOW_THREADS
472
473 sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
474 {
475 SIP_BLOCK_THREADS
476 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
477 SIP_UNBLOCK_THREADS
478 } );
479
480 Py_END_ALLOW_THREADS
481 % End
482// clang-format on
483#endif
484
491 std::function< void( const QgsFeature & ) > transformErrorCallback() const SIP_SKIP { return mTransformErrorCallback; }
492
497 QString defaultEncoding() const SIP_HOLDGIL { return mDefaultEncoding; }
498
503 void setDefaultEncoding( const QString &encoding ) SIP_HOLDGIL { mDefaultEncoding = encoding; }
504
510
520
525 QThread *thread() SIP_HOLDGIL { return tempLayerStore.thread(); }
526
533 void pushToThread( QThread *thread )
534 {
535 // cppcheck-suppress assertWithSideEffect
536 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" );
537 tempLayerStore.moveToThread( thread );
538 }
539
547 void takeResultsFrom( QgsProcessingContext &context );
548
559 QgsMapLayer *getMapLayer( const QString &identifier );
560
569 QgsMapLayer *takeResultLayer( const QString &id ) SIP_TRANSFERBACK;
570
589 QString preferredVectorFormat() const SIP_HOLDGIL { return mPreferredVectorFormat; }
590
606 void setPreferredVectorFormat( const QString &format ) SIP_HOLDGIL { mPreferredVectorFormat = format; }
607
628 QString preferredRasterFormat() const SIP_HOLDGIL { return mPreferredRasterFormat; }
629
647 void setPreferredRasterFormat( const QString &format ) SIP_HOLDGIL { mPreferredRasterFormat = format; }
648
655 Qgis::ProcessingLogLevel logLevel() const SIP_HOLDGIL;
656
663 void setLogLevel( Qgis::ProcessingLogLevel level ) SIP_HOLDGIL;
664
674 QString temporaryFolder() const SIP_HOLDGIL;
675
685 void setTemporaryFolder( const QString &folder ) SIP_HOLDGIL;
686
698 int maximumThreads() const SIP_HOLDGIL;
699
715 void setMaximumThreads( int threads ) SIP_HOLDGIL;
716
722 QVariantMap exportToMap() const SIP_HOLDGIL;
723
729 enum class ProcessArgumentFlag : int SIP_ENUM_BASETYPE( IntFlag )
730 {
731 IncludeProjectPath = 1 << 0,
732 };
734
735
741
759
773
788
794 QgsProcessingModelResult modelResult() const { return mModelResult; }
795
805
811 void clearModelResult();
812
813 private:
814
816 QPointer< QgsProject > mProject;
817 QgsCoordinateTransformContext mTransformContext;
818
819 QString mEllipsoid;
822
823 QgsDateTimeRange mCurrentTimeRange;
824
826 QgsMapLayerStore tempLayerStore;
827 QgsExpressionContext mExpressionContext;
828
830 bool mUseDefaultInvalidGeometryCallback = true;
831 std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
832
833 std::function< void( const QgsFeature & ) > mTransformErrorCallback;
834 QString mDefaultEncoding;
835 QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
836
837 QPointer< QgsProcessingFeedback > mFeedback;
838
839 QString mPreferredVectorFormat;
840 QString mPreferredRasterFormat;
841
843
844 QString mTemporaryFolderOverride;
845 int mMaximumThreads = QThread::idealThreadCount();
846
847 std::unique_ptr< QgsProcessingModelInitialRunConfig > mModelConfig;
848 QgsProcessingModelResult mModelResult;
849
850#ifdef SIP_RUN
852#endif
853};
854
857
858
859
872{
873 public:
874
876
891 virtual void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
892
893};
894
895
896#endif // QGSPROCESSINGPARAMETERS_H
897
898
899
900
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
DistanceUnit
Units of distance.
Definition qgis.h:5170
@ Unknown
Unknown distance unit.
Definition qgis.h:5220
AreaUnit
Units of area.
Definition qgis.h:5247
@ Unknown
Unknown areal unit.
Definition qgis.h:5260
InvalidGeometryCheck
Methods for handling of features with invalid geometries.
Definition qgis.h:2319
@ NoCheck
No invalid geometry checking.
Definition qgis.h:2320
ProcessingLogLevel
Logging level for algorithms to use when pushing feedback messages.
Definition qgis.h:3771
@ DefaultLevel
Default logging level.
Definition qgis.h:3772
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.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
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:83
Details for layers to load into projects.
int layerSortKey
Optional sorting key for sorting output layers when loading them into a project.
QString groupName
Optional name for a layer tree group under which to place the layer when loading it into a project.
bool forceName
Set to true if LayerDetails::name should always be used as the loaded layer name, regardless of the u...
QgsProcessingLayerPostProcessorInterface * postProcessor() const
Layer post-processor.
QString name
Friendly name for layer, possibly for use when loading layer into project.
QgsProject * project
Destination project.
LayerDetails(const QString &name, QgsProject *project, const QString &outputName=QString(), QgsProcessingUtils::LayerHint layerTypeHint=QgsProcessingUtils::LayerHint::UnknownType)
Constructor for LayerDetails.
void setPostProcessor(QgsProcessingLayerPostProcessorInterface *processor)
Sets the layer post-processor.
QString outputName
Associated output name from algorithm which generated the layer.
QgsProcessingUtils::LayerHint layerTypeHint
Layer type hint.
void setOutputLayerName(QgsMapLayer *layer) const
Sets a layer name to match this output, respecting any local user settings which affect this name.
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 raster outputs.
QgsProcessingContext::Flags flags() const
Returns any flags set in the context.
ProcessArgumentFlag
Flags controlling the results given by asQgisProcessArguments().
QFlags< ProcessArgumentFlag > ProcessArgumentFlags
std::unique_ptr< QgsProcessingModelInitialRunConfig > takeModelInitialRunConfig()
Takes the model initial run configuration from the context.
QgsExpressionContext & expressionContext()
Returns the expression context.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
const QgsExpressionContext & expressionContext() const
Returns the expression context.
QStringList asQgisProcessArguments(QgsProcessingContext::ProcessArgumentFlags flags=QgsProcessingContext::ProcessArgumentFlags()) const
Returns list of the equivalent qgis_process arguments representing the settings from the 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.
QgsProcessingModelResult modelResult() const
Returns the model results, populated when the context is used to run a model algorithm.
void setProject(QgsProject *project)
Sets the project in which the algorithm will be executed.
QgsProcessingModelInitialRunConfig * modelInitialRunConfig()
Returns a reference to the model initial run configuration, used to run a model algorithm.
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.
@ Unused
Temporary unused entry.
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 setModelInitialRunConfig(std::unique_ptr< QgsProcessingModelInitialRunConfig > config)
Sets the model initial run configuration, used to run a model algorithm.
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
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 raster 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.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:113
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:274
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_TRANSFERBACK
Definition qgis_sip.h:47
#define SIP_HOLDGIL
Definition qgis_sip.h:178
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)
QgsTemporalRange< QDateTime > QgsDateTimeRange
QgsRange which stores a range of date times.
Definition qgsrange.h:705