[Qgis-user] PyGQIS output a layer? v3.16
Richard Duivenvoorde
rdmailings at duif.net
Tue Aug 24 05:01:06 PDT 2021
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
More information about the Qgis-user
mailing list