[QGIS-Developer] [Info][Howto] Python Processing script to create an output without input

Nyall Dawson nyall.dawson at gmail.com
Wed Jul 31 15:45:13 PDT 2019


On Thu, 1 Aug 2019 at 01:30, João Gaspar <joao.f.r.gaspar at gmail.com> wrote:
>
> Hi devs
> I'm trying to do a Python Script in Processing without an input layer and generate an output layer based on a number of fields that respects an order, name, type of field, precision.
>
> The problem: I have some standard procedures that use a template shapefile. Most of the time users create a new template or truncate existing layers.
>
> To save some clicks what I'm trying to do is create instantly the templates without user lose time. I'm using processing to get the advantage of batch mode.
>
> This is my actual code but crashes when I run (see .py in attach)
>
> I think the problem is in the moment of writing the output file.

Ouch - never, EVER use "iface" in Processing scripts!

The problem is in the line:

layer = iface.addVectorLayer(self.OUTPUT, 'layer1', 'ogr')

You should remove this line, and replace the return dict with the path
to your created layer, e.g.:

return {self.OUTPUT: '/home/me/my.shp'}

Processing will automatically load this as a layer when required.

Nyall



>
> Kind regards
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the QGIS-Developer mailing list