Skip to content

Commit ffce9c9

Browse files
committedJun 5, 2017
Add direct method to retrieve QgsFeatureSink from parameter
1 parent f41eb41 commit ffce9c9

File tree

9 files changed

+180
-52
lines changed

9 files changed

+180
-52
lines changed
 

‎python/core/processing/qgsprocessingalgorithm.sip

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212

13+
1314
class QgsProcessingAlgorithm
1415
{
1516
%Docstring
@@ -306,6 +307,25 @@ class QgsProcessingAlgorithm
306307
:rtype: bool
307308
%End
308309

310+
QgsFeatureSink *parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
311+
const QString &encoding, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs,
312+
QString &destinationIdentifier /Out/ ) const /Factory/;
313+
%Docstring
314+
Evaluates the parameter with matching ``name`` to a feature sink.
315+
316+
Sinks will either be taken from ``context``'s active project, or created from external
317+
providers and stored temporarily in the ``context``.
318+
319+
The ``encoding``, ``fields``, ``geometryType`` and ``crs`` parameters dictate the properties
320+
of the resulting feature sink.
321+
322+
The ``destinationIdentifier`` argument will be set to a string which can be used to retrieve the layer corresponding
323+
to the sink, e.g. via calling QgsProcessingUtils.mapLayerFromString().
324+
325+
This function creates a new object and the caller takes responsibility for deleting the returned object.
326+
:rtype: QgsFeatureSink
327+
%End
328+
309329
QgsMapLayer *parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
310330
%Docstring
311331
Evaluates the parameter with matching ``name`` to a map layer.

‎python/core/processing/qgsprocessingparameters.sip

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313

1414

15+
1516
class QgsProcessingParameterDefinition
1617
{
1718
%Docstring
@@ -222,124 +223,142 @@ class QgsProcessingParameters
222223
:rtype: bool
223224
%End
224225

225-
static QString parameterAsString( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context );
226+
static QString parameterAsString( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
226227
%Docstring
227-
Evaluates the parameter with matching ``name`` to a static string value.
228+
Evaluates the parameter with matching ``definition`` to a static string value.
228229
:rtype: str
229230
%End
230231

231-
static QString parameterAsExpression( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context );
232+
static QString parameterAsExpression( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
232233
%Docstring
233-
Evaluates the parameter with matching ``name`` to an expression.
234+
Evaluates the parameter with matching ``definition`` to an expression.
234235
:rtype: str
235236
%End
236237

237-
static double parameterAsDouble( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context );
238+
static double parameterAsDouble( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
238239
%Docstring
239-
Evaluates the parameter with matching ``name`` to a static double value.
240+
Evaluates the parameter with matching ``definition`` to a static double value.
240241
:rtype: float
241242
%End
242243

243-
static int parameterAsInt( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context );
244+
static int parameterAsInt( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
244245
%Docstring
245-
Evaluates the parameter with matching ``name`` to a static integer value.
246+
Evaluates the parameter with matching ``definition`` to a static integer value.
246247
:rtype: int
247248
%End
248249

249-
static int parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context );
250+
static int parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
250251
%Docstring
251-
Evaluates the parameter with matching ``name`` to a enum value.
252+
Evaluates the parameter with matching ``definition`` to a enum value.
252253
:rtype: int
253254
%End
254255

255-
static QList<int> parameterAsEnums( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context );
256+
static QList<int> parameterAsEnums( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
256257
%Docstring
257-
Evaluates the parameter with matching ``name`` to list of enum values.
258+
Evaluates the parameter with matching ``definition`` to list of enum values.
258259
:rtype: list of int
259260
%End
260261

261-
static bool parameterAsBool( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context );
262+
static bool parameterAsBool( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
262263
%Docstring
263-
Evaluates the parameter with matching ``name`` to a static boolean value.
264+
Evaluates the parameter with matching ``definition`` to a static boolean value.
264265
:rtype: bool
265266
%End
266267

267-
static QgsMapLayer *parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
268+
static QgsFeatureSink *parameterAsSink( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters,
269+
const QString &encoding, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs,
270+
QgsProcessingContext &context, QString &destinationIdentifier /Out/ ) /Factory/;
271+
%Docstring
272+
Evaluates the parameter with matching ``definition`` to a feature sink.
273+
274+
The ``encoding``, ``fields``, ``geometryType`` and ``crs`` parameters dictate the properties
275+
of the resulting feature sink.
276+
277+
Sinks will either be taken from ``context``'s active project, or created from external
278+
providers and stored temporarily in the ``context``. The ``destinationIdentifier``
279+
argument will be set to a string which can be used to retrieve the layer corresponding
280+
to the sink, e.g. via calling QgsProcessingUtils.mapLayerFromString().
281+
282+
This function creates a new object and the caller takes responsibility for deleting the returned object.
283+
:rtype: QgsFeatureSink
284+
%End
285+
286+
static QgsMapLayer *parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
268287
%Docstring
269-
Evaluates the parameter with matching ``name`` to a map layer.
288+
Evaluates the parameter with matching ``definition`` to a map layer.
270289

271290
Layers will either be taken from ``context``'s active project, or loaded from external
272291
sources and stored temporarily in the ``context``. In either case, callers do not
273292
need to handle deletion of the returned layer.
274293
:rtype: QgsMapLayer
275294
%End
276295

277-
static QgsRasterLayer *parameterAsRasterLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
296+
static QgsRasterLayer *parameterAsRasterLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
278297
%Docstring
279-
Evaluates the parameter with matching ``name`` to a raster layer.
298+
Evaluates the parameter with matching ``definition`` to a raster layer.
280299

281300
Layers will either be taken from ``context``'s active project, or loaded from external
282301
sources and stored temporarily in the ``context``. In either case, callers do not
283302
need to handle deletion of the returned layer.
284303
:rtype: QgsRasterLayer
285304
%End
286305

287-
static QgsVectorLayer *parameterAsVectorLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
306+
static QgsVectorLayer *parameterAsVectorLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
288307
%Docstring
289-
Evaluates the parameter with matching ``name`` to a vector layer.
308+
Evaluates the parameter with matching ``definition`` to a vector layer.
290309

291310
Layers will either be taken from ``context``'s active project, or loaded from external
292311
sources and stored temporarily in the ``context``. In either case, callers do not
293312
need to handle deletion of the returned layer.
294313
:rtype: QgsVectorLayer
295314
%End
296315

297-
static QgsCoordinateReferenceSystem parameterAsCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
316+
static QgsCoordinateReferenceSystem parameterAsCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
298317
%Docstring
299-
Evaluates the parameter with matching ``name`` to a coordinate reference system.
318+
Evaluates the parameter with matching ``definition`` to a coordinate reference system.
300319
:rtype: QgsCoordinateReferenceSystem
301320
%End
302321

303-
static QgsRectangle parameterAsExtent( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
322+
static QgsRectangle parameterAsExtent( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
304323
%Docstring
305-
Evaluates the parameter with matching ``name`` to a rectangular extent.
324+
Evaluates the parameter with matching ``definition`` to a rectangular extent.
306325
:rtype: QgsRectangle
307326
%End
308327

309-
static QgsPointXY parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
328+
static QgsPointXY parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
310329
%Docstring
311-
Evaluates the parameter with matching ``name`` to a point.
330+
Evaluates the parameter with matching ``definition`` to a point.
312331
:rtype: QgsPointXY
313332
%End
314333

315-
static QString parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
334+
static QString parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
316335
%Docstring
317-
Evaluates the parameter with matching ``name`` to a file/folder name.
336+
Evaluates the parameter with matching ``definition`` to a file/folder name.
318337
:rtype: str
319338
%End
320339

321-
static QVariantList parameterAsMatrix( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
340+
static QVariantList parameterAsMatrix( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
322341
%Docstring
323-
Evaluates the parameter with matching ``name`` to a matrix/table of values.
342+
Evaluates the parameter with matching ``definition`` to a matrix/table of values.
324343
Tables are collapsed to a 1 dimensional list.
325344
:rtype: QVariantList
326345
%End
327346

328-
static QList< QgsMapLayer *> parameterAsLayerList( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
347+
static QList< QgsMapLayer *> parameterAsLayerList( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
329348
%Docstring
330-
Evaluates the parameter with matching ``name`` to a list of map layers.
349+
Evaluates the parameter with matching ``definition`` to a list of map layers.
331350
:rtype: list of QgsMapLayer
332351
%End
333352

334-
static QList<double> parameterAsRange( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
353+
static QList<double> parameterAsRange( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
335354
%Docstring
336-
Evaluates the parameter with matching ``name`` to a range of values.
355+
Evaluates the parameter with matching ``definition`` to a range of values.
337356
:rtype: list of float
338357
%End
339358

340-
static QStringList parameterAsFields( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
359+
static QStringList parameterAsFields( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
341360
%Docstring
342-
Evaluates the parameter with matching ``name`` to a list of fields.
361+
Evaluates the parameter with matching ``definition`` to a list of fields.
343362
:rtype: list of str
344363
%End
345364

‎python/plugins/processing/algs/qgis/Boundary.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,15 @@ def processAlgorithm(self, parameters, context, feedback):
8282
if QgsWkbTypes.hasM(input_wkb):
8383
output_wkb = QgsWkbTypes.addM(output_wkb)
8484

85-
dest = self.parameterAsString(parameters, self.OUTPUT_LAYER, context)
86-
(writer, dest_layer) = QgsProcessingUtils.createFeatureSink(dest, '', layer.fields(), output_wkb, layer.crs(), context)
85+
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT_LAYER, context, '',
86+
layer.fields(), output_wkb, layer.crs())
8787

8888
features = QgsProcessingUtils.getFeatures(layer, context)
8989
total = 100.0 / QgsProcessingUtils.featureCount(layer, context)
9090

9191
for current, input_feature in enumerate(features):
92+
if feedback.isCanceled():
93+
break
9294
output_feature = input_feature
9395
input_geometry = input_feature.geometry()
9496
if input_geometry:
@@ -99,7 +101,7 @@ def processAlgorithm(self, parameters, context, feedback):
99101

100102
output_feature.setGeometry(output_geometry)
101103

102-
writer.addFeature(output_feature)
104+
sink.addFeature(output_feature)
103105
feedback.setProgress(int(current * total))
104106

105-
return {self.OUTPUT_LAYER: dest_layer}
107+
return {self.OUTPUT_LAYER: dest_id}

‎src/core/processing/qgsnativealgorithms.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ QString QgsCentroidAlgorithm::shortHelpString() const
7878

7979
QVariantMap QgsCentroidAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
8080
{
81-
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context ) );
81+
QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
8282
if ( !layer )
8383
return QVariantMap();
8484

85-
QString dest = parameterAsString( parameters, QStringLiteral( "OUTPUT_LAYER" ), context );
86-
std::unique_ptr< QgsFeatureSink > writer( QgsProcessingUtils::createFeatureSink( dest, QString(), layer->fields(), QgsWkbTypes::Point, layer->crs(), context ) );
85+
QString dest;
86+
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT_LAYER" ), context, QString(), layer->fields(), QgsWkbTypes::Point, layer->crs(), dest ) );
8787

8888
long count = QgsProcessingUtils::featureCount( layer, context );
8989
if ( count <= 0 )
@@ -110,7 +110,7 @@ QVariantMap QgsCentroidAlgorithm::processAlgorithm( const QVariantMap &parameter
110110
QgsMessageLog::logMessage( QObject::tr( "Error calculating centroid for feature %1" ).arg( f.id() ), QObject::tr( "Processing" ), QgsMessageLog::WARNING );
111111
}
112112
}
113-
writer->addFeature( out );
113+
sink->addFeature( out );
114114

115115
feedback->setProgress( current * step );
116116
current++;
@@ -151,12 +151,12 @@ QString QgsBufferAlgorithm::shortHelpString() const
151151

152152
QVariantMap QgsBufferAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
153153
{
154-
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context ) );
154+
QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
155155
if ( !layer )
156156
return QVariantMap();
157157

158-
QString dest = parameterAsString( parameters, QStringLiteral( "OUTPUT_LAYER" ), context );
159-
std::unique_ptr< QgsFeatureSink > writer( QgsProcessingUtils::createFeatureSink( dest, QString(), layer->fields(), QgsWkbTypes::Polygon, layer->crs(), context ) );
158+
QString dest;
159+
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT_LAYER" ), context, QString(), layer->fields(), QgsWkbTypes::Polygon, layer->crs(), dest ) );
160160

161161
// fixed parameters
162162
//bool dissolve = QgsProcessingParameters::parameterAsBool( parameters, QStringLiteral( "DISSOLVE" ), context );
@@ -199,7 +199,7 @@ QVariantMap QgsBufferAlgorithm::processAlgorithm( const QVariantMap &parameters,
199199
QgsMessageLog::logMessage( QObject::tr( "Error calculating buffer for feature %1" ).arg( f.id() ), QObject::tr( "Processing" ), QgsMessageLog::WARNING );
200200
}
201201
}
202-
writer->addFeature( out );
202+
sink->addFeature( out );
203203

204204
feedback->setProgress( current * step );
205205
current++;

‎src/core/processing/qgsprocessingalgorithm.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ bool QgsProcessingAlgorithm::parameterAsBool( const QVariantMap &parameters, con
210210
return QgsProcessingParameters::parameterAsBool( parameterDefinition( name ), parameters, context );
211211
}
212212

213+
QgsFeatureSink *QgsProcessingAlgorithm::parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QString &encoding, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, QString &destinationIdentifier ) const
214+
{
215+
return QgsProcessingParameters::parameterAsSink( parameterDefinition( name ), parameters, encoding, fields, geometryType, crs, context, destinationIdentifier );
216+
}
217+
213218
QgsMapLayer *QgsProcessingAlgorithm::parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
214219
{
215220
return QgsProcessingParameters::parameterAsLayer( parameterDefinition( name ), parameters, context );

‎src/core/processing/qgsprocessingalgorithm.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
class QgsProcessingProvider;
3030
class QgsProcessingContext;
3131
class QgsProcessingFeedback;
32-
class QWidget;
32+
class QgsFeatureSink;
33+
3334

3435
/**
3536
* \class QgsProcessingAlgorithm
@@ -297,6 +298,24 @@ class CORE_EXPORT QgsProcessingAlgorithm
297298
*/
298299
bool parameterAsBool( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
299300

301+
/**
302+
* Evaluates the parameter with matching \a name to a feature sink.
303+
*
304+
* Sinks will either be taken from \a context's active project, or created from external
305+
* providers and stored temporarily in the \a context.
306+
*
307+
* The \a encoding, \a fields, \a geometryType and \a crs parameters dictate the properties
308+
* of the resulting feature sink.
309+
*
310+
* The \a destinationIdentifier argument will be set to a string which can be used to retrieve the layer corresponding
311+
* to the sink, e.g. via calling QgsProcessingUtils::mapLayerFromString().
312+
*
313+
* This function creates a new object and the caller takes responsibility for deleting the returned object.
314+
*/
315+
QgsFeatureSink *parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
316+
const QString &encoding, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs,
317+
QString &destinationIdentifier SIP_OUT ) const SIP_FACTORY;
318+
300319
/**
301320
* Evaluates the parameter with matching \a name to a map layer.
302321
*

‎src/core/processing/qgsprocessingparameters.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,17 @@ bool QgsProcessingParameters::parameterAsBool( const QgsProcessingParameterDefin
202202
return def.toBool();
203203
}
204204

205+
QgsFeatureSink *QgsProcessingParameters::parameterAsSink( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &encoding, const QgsFields &fields,
206+
QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs,
207+
QgsProcessingContext &context, QString &destinationIdentifier )
208+
{
209+
QString dest = parameterAsString( definition, parameters, context );
210+
std::unique_ptr< QgsFeatureSink > sink( QgsProcessingUtils::createFeatureSink( dest, encoding, fields, geometryType, crs, context ) );
211+
destinationIdentifier = dest;
212+
213+
return sink.release();
214+
}
215+
205216
QgsMapLayer *QgsProcessingParameters::parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
206217
{
207218
if ( !definition )
@@ -290,10 +301,10 @@ QgsRectangle QgsProcessingParameters::parameterAsExtent( const QgsProcessingPara
290301
return QgsRectangle();
291302
}
292303

293-
QgsPoint QgsProcessingParameters::parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
304+
QgsPointXY QgsProcessingParameters::parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
294305
{
295306
if ( !definition )
296-
return QgsPoint();
307+
return QgsPointXY();
297308

298309
QString pointText = parameterAsString( definition, parameters, context );
299310
if ( pointText.isEmpty() )

‎src/core/processing/qgsprocessingparameters.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
class QgsProcessingContext;
2929
class QgsRasterLayer;
3030
class QgsVectorLayer;
31+
class QgsFeatureSink;
32+
3133

3234
//
3335
// Parameter definitions
@@ -280,6 +282,23 @@ class CORE_EXPORT QgsProcessingParameters
280282
*/
281283
static bool parameterAsBool( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
282284

285+
/**
286+
* Evaluates the parameter with matching \a definition to a feature sink.
287+
*
288+
* The \a encoding, \a fields, \a geometryType and \a crs parameters dictate the properties
289+
* of the resulting feature sink.
290+
*
291+
* Sinks will either be taken from \a context's active project, or created from external
292+
* providers and stored temporarily in the \a context. The \a destinationIdentifier
293+
* argument will be set to a string which can be used to retrieve the layer corresponding
294+
* to the sink, e.g. via calling QgsProcessingUtils::mapLayerFromString().
295+
*
296+
* This function creates a new object and the caller takes responsibility for deleting the returned object.
297+
*/
298+
static QgsFeatureSink *parameterAsSink( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters,
299+
const QString &encoding, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs,
300+
QgsProcessingContext &context, QString &destinationIdentifier SIP_OUT ) SIP_FACTORY;
301+
283302
/**
284303
* Evaluates the parameter with matching \a definition to a map layer.
285304
*

‎tests/src/core/testqgsprocessing.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ void TestQgsProcessing::parameters()
10031003

10041004
// correctly setup feature
10051005
QgsFields fields;
1006-
fields.append( QgsField( "a_field", QVariant::String ) );
1006+
fields.append( QgsField( "a_field", QVariant::String, QString(), 30 ) );
10071007
QgsFeature f( fields );
10081008
f.setAttribute( 0, QStringLiteral( "field value" ) );
10091009
context.expressionContext().setFeature( f );
@@ -1061,6 +1061,39 @@ void TestQgsProcessing::parameters()
10611061
QVERIFY( QgsProcessingParameters::parameterAsLayer( def, params, context ) );
10621062
// make sure layer was loaded
10631063
QVERIFY( !context.temporaryLayerStore()->mapLayers().isEmpty() );
1064+
1065+
// as sink
1066+
QString encoding;
1067+
QgsWkbTypes::Type wkbType = QgsWkbTypes::PolygonM;
1068+
QgsCoordinateReferenceSystem crs = QgsCoordinateReferenceSystem( "epsg:3111" );
1069+
QString destId;
1070+
def->setName( QStringLiteral( "string" ) );
1071+
params.insert( QStringLiteral( "string" ), QStringLiteral( "memory:mem" ) );
1072+
std::unique_ptr< QgsFeatureSink > sink;
1073+
sink.reset( QgsProcessingParameters::parameterAsSink( def, params, encoding, fields, wkbType, crs, context, destId ) );
1074+
QVERIFY( sink.get() );
1075+
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( destId, context ) );
1076+
QVERIFY( layer );
1077+
QVERIFY( layer->isValid() );
1078+
QCOMPARE( layer->fields().count(), 1 );
1079+
QCOMPARE( layer->fields().at( 0 ).name(), QStringLiteral( "a_field" ) );
1080+
QCOMPARE( layer->wkbType(), wkbType );
1081+
QCOMPARE( layer->crs(), crs );
1082+
1083+
// property defined sink destination
1084+
params.insert( QStringLiteral( "prop" ), QgsProperty::fromExpression( "'memory:mem2'" ) );
1085+
def->setName( QStringLiteral( "prop" ) );
1086+
crs = QgsCoordinateReferenceSystem( "epsg:3113" );
1087+
sink.reset( QgsProcessingParameters::parameterAsSink( def, params, encoding, fields, wkbType, crs, context, destId ) );
1088+
QVERIFY( sink.get() );
1089+
layer = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( destId, context ) );
1090+
QVERIFY( layer );
1091+
QVERIFY( layer->isValid() );
1092+
QCOMPARE( layer->fields().count(), 1 );
1093+
QCOMPARE( layer->fields().at( 0 ).name(), QStringLiteral( "a_field" ) );
1094+
QCOMPARE( layer->wkbType(), wkbType );
1095+
QCOMPARE( layer->crs(), crs );
1096+
10641097
delete def;
10651098
}
10661099

0 commit comments

Comments
 (0)
Please sign in to comment.