Skip to content

Commit 6b4ddb3

Browse files
committed
Add temporary layer store to processing context
This temporary layer store (a QgsProject) is used as a store for layers that are added if a parameter that is evaluated to a layer requires that a new, non-active-project layer is loaded. It means that these layers will remain accessible for the duration of the algorithm's execution (or models execution if an algorithm is run as part of a model), before being automatically discarded when the QgsProcessingContext used to run the algorithm/model goes out of scope. This approach has several benefits: - it means that algorithms (including c++ algorithms) are able to use both project and non-project layers without needing to handle any memory management themselves. - it means that layers are guaranteed to last for the duration of a model execution. This is currently an issue where models which use memory layers as intermediate outputs do not function correctly as the memory layers are destroyed before the model has finished executing - there should be no leakage of layers remaining open after an algorithm exits
1 parent 5169e0d commit 6b4ddb3

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

python/core/processing/qgsprocessingcontext.sip

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class QgsProcessingContext
3737
Constructor for QgsProcessingContext.
3838
%End
3939

40+
// QgsProcessingContext &operator=( const QgsProcessingContext &other ) = delete;
4041
QgsProcessingContext::Flags flags() const;
4142
%Docstring
4243
Returns any flags set in the context.
@@ -74,6 +75,9 @@ class QgsProcessingContext
7475
Sets the expression ``context``.
7576
%End
7677

78+
79+
80+
7781
QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const;
7882
%Docstring
7983
Returns the behavior used for checking invalid geometries in input layers.
@@ -110,6 +114,8 @@ class QgsProcessingContext
110114
%End
111115

112116

117+
private:
118+
QgsProcessingContext( const QgsProcessingContext &other );
113119
};
114120
QFlags<QgsProcessingContext::Flag> operator|(QgsProcessingContext::Flag f1, QFlags<QgsProcessingContext::Flag> f2);
115121

src/core/processing/qgsprocessingcontext.h

+19
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class CORE_EXPORT QgsProcessingContext
5050
*/
5151
QgsProcessingContext() = default;
5252

53+
QgsProcessingContext( const QgsProcessingContext &other ) = delete;
54+
QgsProcessingContext &operator=( const QgsProcessingContext &other ) = delete;
55+
5356
/**
5457
* Returns any flags set in the context.
5558
* \see setFlags()
@@ -84,6 +87,17 @@ class CORE_EXPORT QgsProcessingContext
8487
*/
8588
void setExpressionContext( const QgsExpressionContext &context ) { mExpressionContext = context; }
8689

90+
///@cond NOT_STABLE_API
91+
92+
/**
93+
* Returns a reference to the project used for storing temporary layers during
94+
* algorithm execution.
95+
* \note not available in Python bindings
96+
*/
97+
SIP_SKIP QgsProject &temporaryLayerStore() { return tempProject; }
98+
99+
///@endcond
100+
87101
/**
88102
* Returns the behavior used for checking invalid geometries in input layers.
89103
* \see setInvalidGeometryCheck()
@@ -135,10 +149,15 @@ class CORE_EXPORT QgsProcessingContext
135149

136150
QgsProcessingContext::Flags mFlags = 0;
137151
QPointer< QgsProject > mProject;
152+
//! Temporary project owned by the context, used for storing temporarily loaded map layers
153+
QgsProject tempProject;
138154
QgsExpressionContext mExpressionContext;
139155
QgsFeatureRequest::InvalidGeometryCheck mInvalidGeometryCheck = QgsFeatureRequest::GeometryNoCheck;
140156
std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
141157

158+
#ifdef SIP_RUN
159+
QgsProcessingContext( const QgsProcessingContext &other );
160+
#endif
142161
};
143162
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::Flags )
144163

0 commit comments

Comments
 (0)