When calling a QgsAlgorithm
from within another, the result will be named after this algorithm output regardless of what is setup as the output of the main one.
For instance, taking the code from the answer to this question, although the output is set with the name 'OUTPUT'
and the returned dictionary
is the following:
{'OUTPUT': 'clipped_xxxxxxxxxxxxxxxxxxxxx'}
the output layer is actually named 'clipped'
and I cannot seem to be able to change this.
I've tried the setName()
method on the QgsVectorLayer
retrieved with QgsProcessingUtils.mapLayerFromString()
but to no avail.
-
You should look at this post: Getting the output layer reference returned by Processing tool.Kadir Şahbaz– Kadir Şahbaz2020年02月27日 17:57:41 +00:00Commented Feb 27, 2020 at 17:57
-
@KadirŞahbaz Thanks but as I said below, I'd rather have my algorithm applying a proper name to its output, than to have to run some more code afterwards.YeO– YeO2020年02月28日 02:40:51 +00:00Commented Feb 28, 2020 at 2:40
2 Answers 2
You can get the layer from the layer id:
output=processing.runAndLoadResults("qgis:intersection",params)
createdlayer=QgsProject.instance().mapLayer(output['OUTPUT'])
createdlayer.setName('My shiny new beautiful layer')
-
So I'd need to run this code after having run my algorithm, right ? I'd really like to have the layer properly named by the algorithm itself rather than having to apply cosmetics afterwards. Or do I understand wrong ?YeO– YeO2020年02月28日 02:39:16 +00:00Commented Feb 28, 2020 at 2:39
-
You understand right. - at least I have not found any other way of doing it - rather than even more kludgy things like searching for the layer by name. Just to be clear, the first step runs the algorithm (Which in my case is qgis:intersection), this is what I do in a plugin, it may not apply at all to what you are doing...MortenSickel– MortenSickel2020年02月28日 04:41:29 +00:00Commented Feb 28, 2020 at 4:41
-
Makes sense in the context of a plugin. With my simple algorithm script it seems I'm kind of stuck though. Thanks anywayYeO– YeO2020年02月28日 05:17:50 +00:00Commented Feb 28, 2020 at 5:17
-
This is actually exactly what I was looking for, thanks!WFL.GIS– WFL.GIS2021年08月19日 01:50:48 +00:00Commented Aug 19, 2021 at 1:50
It needs to be an actual path on your machine.
For example
{'OUTPUT': '/root/data/clipped.gpkg'}
-
Ok but the algorithm returns a layer id, not a pathYeO– YeO2019年08月08日 10:18:26 +00:00Commented Aug 8, 2019 at 10:18
Explore related questions
See similar questions with these tags.