Skip to content

Commit 280ca31

Browse files
authored
Merge pull request #4401 from nyalldawson/processing_params
c++ framework for parameters and running algorithms
2 parents 7af73b3 + 1006aa9 commit 280ca31

13 files changed

+4151
-4
lines changed

python/core/core.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@
284284
%Include processing/qgsprocessingalgorithm.sip
285285
%Include processing/qgsprocessingcontext.sip
286286
%Include processing/qgsprocessingfeedback.sip
287+
%Include processing/qgsprocessingparameters.sip
287288
%Include processing/qgsprocessingprovider.sip
288289
%Include processing/qgsprocessingregistry.sip
289290
%Include processing/qgsprocessingutils.sip

python/core/processing/qgsprocessingalgorithm.sip

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,171 @@ class QgsProcessingAlgorithm
112112

113113
void setProvider( QgsProcessingProvider *provider );
114114

115+
QgsProcessingParameterDefinitions parameterDefinitions() const;
116+
%Docstring
117+
Returns an ordered list of parameter definitions utilized by the algorithm.
118+
.. seealso:: addParameter()
119+
.. seealso:: parameterDefinition()
120+
:rtype: QgsProcessingParameterDefinitions
121+
%End
122+
123+
const QgsProcessingParameterDefinition *parameterDefinition( const QString &name ) const;
124+
%Docstring
125+
Returns a matching parameter by ``name``. Matching is done in a case-insensitive
126+
manner.
127+
.. seealso:: parameterDefinitions()
128+
:rtype: QgsProcessingParameterDefinition
129+
%End
130+
131+
virtual QVariantMap run( const QVariantMap &parameters,
132+
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;
133+
%Docstring
134+
Runs the algorithm using the specified ``parameters``. Algorithms should implement
135+
their custom processing logic here.
136+
137+
The ``context`` argument specifies the context in which the algorithm is being run.
138+
139+
Algorithm progress should be reported using the supplied ``feedback`` object. Additionally,
140+
well-behaved algorithms should periodically check ``feedback`` to determine whether the
141+
algorithm should be canceled and exited early.
142+
143+
:return: A map of algorithm outputs. These may be output layer references, or calculated
144+
values such as statistical calculations.
145+
:rtype: QVariantMap
146+
%End
147+
148+
protected:
149+
150+
bool addParameter( QgsProcessingParameterDefinition *parameterDefinition /Transfer/ );
151+
%Docstring
152+
Adds a parameter ``definition`` to the algorithm. Ownership of the definition is transferred to the algorithm.
153+
Returns true if parameter could be successfully added, or false if the parameter could not be added (e.g.
154+
as a result of a duplicate name).
155+
:rtype: bool
156+
%End
157+
158+
QString parameterAsString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
159+
%Docstring
160+
Evaluates the parameter with matching ``name`` to a static string value.
161+
:rtype: str
162+
%End
163+
164+
QString parameterAsExpression( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
165+
%Docstring
166+
Evaluates the parameter with matching ``name`` to an expression.
167+
:rtype: str
168+
%End
169+
170+
double parameterAsDouble( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
171+
%Docstring
172+
Evaluates the parameter with matching ``name`` to a static double value.
173+
:rtype: float
174+
%End
175+
176+
int parameterAsInt( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
177+
%Docstring
178+
Evaluates the parameter with matching ``name`` to a static integer value.
179+
:rtype: int
180+
%End
181+
182+
int parameterAsEnum( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
183+
%Docstring
184+
Evaluates the parameter with matching ``name`` to a enum value.
185+
:rtype: int
186+
%End
187+
188+
QList<int> parameterAsEnums( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
189+
%Docstring
190+
Evaluates the parameter with matching ``name`` to list of enum values.
191+
:rtype: list of int
192+
%End
193+
194+
bool parameterAsBool( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
195+
%Docstring
196+
Evaluates the parameter with matching ``name`` to a static boolean value.
197+
:rtype: bool
198+
%End
199+
200+
QgsMapLayer *parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
201+
%Docstring
202+
Evaluates the parameter with matching ``name`` to a map layer.
203+
204+
Layers will either be taken from ``context``'s active project, or loaded from external
205+
sources and stored temporarily in the ``context``. In either case, callers do not
206+
need to handle deletion of the returned layer.
207+
:rtype: QgsMapLayer
208+
%End
209+
210+
QgsRasterLayer *parameterAsRasterLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
211+
%Docstring
212+
Evaluates the parameter with matching ``name`` to a raster layer.
213+
214+
Layers will either be taken from ``context``'s active project, or loaded from external
215+
sources and stored temporarily in the ``context``. In either case, callers do not
216+
need to handle deletion of the returned layer.
217+
:rtype: QgsRasterLayer
218+
%End
219+
220+
QgsVectorLayer *parameterAsVectorLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
221+
%Docstring
222+
Evaluates the parameter with matching ``name`` to a vector layer.
223+
224+
Layers will either be taken from ``context``'s active project, or loaded from external
225+
sources and stored temporarily in the ``context``. In either case, callers do not
226+
need to handle deletion of the returned layer.
227+
:rtype: QgsVectorLayer
228+
%End
229+
230+
QgsCoordinateReferenceSystem parameterAsCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
231+
%Docstring
232+
Evaluates the parameter with matching ``name`` to a coordinate reference system.
233+
:rtype: QgsCoordinateReferenceSystem
234+
%End
235+
236+
QgsRectangle parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
237+
%Docstring
238+
Evaluates the parameter with matching ``name`` to a rectangular extent.
239+
:rtype: QgsRectangle
240+
%End
241+
242+
QgsPoint parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
243+
%Docstring
244+
Evaluates the parameter with matching ``name`` to a point.
245+
:rtype: QgsPoint
246+
%End
247+
248+
QString parameterAsFile( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
249+
%Docstring
250+
Evaluates the parameter with matching ``name`` to a file/folder name.
251+
:rtype: str
252+
%End
253+
254+
QVariantList parameterAsMatrix( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
255+
%Docstring
256+
Evaluates the parameter with matching ``name`` to a matrix/table of values.
257+
Tables are collapsed to a 1 dimensional list.
258+
:rtype: QVariantList
259+
%End
260+
261+
QList< QgsMapLayer *> parameterAsLayerList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
262+
%Docstring
263+
Evaluates the parameter with matching ``name`` to a list of map layers.
264+
:rtype: list of QgsMapLayer
265+
%End
266+
267+
QList<double> parameterAsRange( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
268+
%Docstring
269+
Evaluates the parameter with matching ``name`` to a range of values.
270+
:rtype: list of float
271+
%End
272+
273+
QStringList parameterAsFields( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
274+
%Docstring
275+
Evaluates the parameter with matching ``name`` to a list of fields.
276+
:rtype: list of str
277+
%End
278+
279+
115280
private:
116281
QgsProcessingAlgorithm( const QgsProcessingAlgorithm &other );
117282
};

python/core/processing/qgsprocessingcontext.sip

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ class QgsProcessingContext
6464
.. seealso:: project()
6565
%End
6666

67-
QgsExpressionContext expressionContext() const;
67+
QgsExpressionContext &expressionContext();
6868
%Docstring
6969
Returns the expression context.
7070
:rtype: QgsExpressionContext
7171
%End
7272

73+
7374
void setExpressionContext( const QgsExpressionContext &context );
7475
%Docstring
7576
Sets the expression ``context``.
@@ -82,6 +83,7 @@ class QgsProcessingContext
8283
:rtype: QgsMapLayerStore
8384
%End
8485

86+
8587
QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const;
8688
%Docstring
8789
Returns the behavior used for checking invalid geometries in input layers.

0 commit comments

Comments
 (0)