[QGIS-Developer] Processing Feature Sink Questions

Nyall Dawson nyall.dawson at gmail.com
Tue Aug 28 21:19:31 PDT 2018

On Wed, 29 Aug 2018 at 06:03, C Hamilton <adenaculture at gmail.com> wrote:
> I am working on a processing routine that can take a source that potentially has points, lines and polygons. I define 3 feature sinks for the 3 geometries and they are optional. This example is for the points.
>         self.addParameter(
>             QgsProcessingParameterFeatureSink(
>                 self.PrmPointOutputLayer,
>                 tr('Output point layer'),
>                 optional=True)
>             )
> 1) How doe I know in the processAlgorithm whether the user has this set a feature sink to "Skip Output"?

Check if the result of your call to self.parameterAsSink is None. If
so, the parameter has been skipped.

> 2) I really don't know whether I have points, lines, and/or polygons until I process the file and from the 3 feature sinks I just add the data to the appropriate feature sink as I receive it . How do a get a count of the number of features once I have processed the file from sinkPt or dest_id_pt?
>         (sinkPt, dest_id_pt) = self.parameterAsSink(parameters,
>             self.PrmPointOutputLayer, context, fields,
>             QgsWkbTypes.Point, epsg4326)
> "sinkPt" seems to only have the ability to add features and not count them.

That's correct - sinks don't maintain a count of features. You'll need
to record the counts manually yourself, as you add each feature to the

> 3) I see to reason to return a layer to QGIS if it has no data which is the reason to check the feature count. In the processAlgorithm return statement do I only return those layers that have data?
>         return {
>             self.PrmPointOutputLayer: dest_id_pt,
>             self.PrmLineOutputLayer: dest_id_line,
>             self.PrmPolygonOutputLayer: dest_id_poly
>             }

You can omit these from the returned dictionary in this case - so if
you're recording the counts of features added to each sink, only add
entries to the results dictionary where the count is > 0.


More information about the QGIS-Developer mailing list