[Qgis-user] PyGQIS output a layer? v3.16

Etienne Trimaille etienne.trimaille at gmail.com
Tue Aug 24 23:43:22 PDT 2021


You can output a QgsVectorLayer :
https://qgis.org/api/classQgsProcessingOutputVectorLayer.html
The sink is not required and you would avoid a loop to insert all features.

I think you should also return sink ID or layer ID, not the object itself.

Le mar. 24 août 2021 à 14:01, Richard Duivenvoorde <rdmailings at duif.net> a
écrit :

> On 8/24/21 12:57 PM, Andrew Hughes wrote:
> > Hi All,
> >
> > I am trying to create a python script that takes a URL as input and
> creates/outputs an ArcGIS Feature Service Vector Layer (used for further
> processing downstream). However,  I believe I am doing something wrong with
> how I am trying to return the layer.
> >
> > The script in question can be found here:
> >
> >
> https://github.com/ahhughes/pyqgisorama/blob/main/CreateArcGISFeatureLayer.py#L132
> <
> https://github.com/ahhughes/pyqgisorama/blob/main/CreateArcGISFeatureLayer.py#L132
> >
>
> Hi Andrew,
>
> I'm not so experienced either in writing algorithms, but if I am right,
> you are not supposed to return a layer, but you need to return a 'sink' as
> it is possible that it will be used as input for another algorithm.
> So I used:
> self.parameterAsSink
>
> Note there is also:
> self.parameterAsFileOutput
>
> Did some googling with 'pyqgis layer sink output' and came up with this
> (and the rest of your GIST):
>
>
>     def processAlgorithm(self, parameters, context, feedback):
>         """
>         Here is where the processing itself takes place.
>         """
>
>         url = parameters[self.INPUT].strip()
>
>         #out = self.parameterAsFileOutput(parameters, self.OUTPUT, context)
>
>         lyr = QgsVectorLayer("crs='EPSG:3857' url='"+url+"'", "RT",
> "arcgisfeatureserver")
>
>         feedback.pushInfo("Layer has been created: "+str(lyr))
>         feedback.pushInfo("Is the layer valid?: "+str(lyr.isValid()))
>
>         (sink, dest_id) = self.parameterAsSink(
>             parameters,
>             self.OUTPUT,
>             context,
>             lyr.fields(),
>             lyr.wkbType(),
>             lyr.sourceCrs()
>         )
>
>         for f in lyr.getFeatures():
>             sink.addFeature(f)
>
>         return {self.OUTPUT: sink}
>
>
> Which at least for me loads some lines :-)
>
> I hope that others can maybe more context about the differences between
> parameterAsSink and parameterAsFileOutput.
> If not, this would be maybe more a question for the dev list?
>
> Regards,
>
> Richard Duivenvoorde
>
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20210825/a2c92863/attachment.html>


More information about the Qgis-user mailing list