[QGIS-Developer] Processing Feature Sink Questions

Nyall Dawson nyall.dawson at gmail.com
Wed Aug 29 15:50:27 PDT 2018

On Thu, 30 Aug 2018 at 00:01, C Hamilton <adenaculture at gmail.com> wrote:
> > 3) I see no 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.
> This almost did what was needed. It looks like calling
>          (sinkPt, dest_id_pt) = self.parameterAsSink(parameters,
>              self.PrmPointOutputLayer, context, fields,
>              QgsWkbTypes.Point, epsg4326)
> is what determines what layers are added to QGIS and not the dictionary I return. I guess I'm going to need to call parameterAsSink the first time I have data to be added. This is going to make the code more messy, but I don't know another way to prevent empty layers from being added.

Sorry - I gave bad advice. Test if the parameter value *itself* is None. i.e.

   if parameters[self.PrmPointOutputLayer] is None:
      # output as been skipped

For safety you may want to check if the parameter is in included in
the dictionary too - otherwise callers who just "leave out" the
parameter will cause an exception. So:

   if self.PrmPointOutputLayer not in parameters or
parameters[self.PrmPointOutputLayer] is None:
      # output as been skipped


> Calvin

More information about the QGIS-Developer mailing list