The script below is to merge number of layers in a specific group, the output layer name is Merged
. How can I change the name? I tried to change the output parameters but it's not working.
root = QgsProject.instance().layerTreeRoot()
group = root.findGroup("RL")
layers = [layer.layer()
for layer in group.children()]
merged = processing.run("native:mergevectorlayers",\
{'LAYERS': layers, 'OUTPUT':"TEMPORARY_OUTPUT"})["OUTPUT"]
QgsProject.instance().addMapLayer(merged, False)
root.insertLayer(0, merged)
root = QgsProject.instance().layerTreeRoot()
group = root.findGroup("SL")
layers = [layer.layer()
for layer in group.children()]
merged = processing.run("native:mergevectorlayers",\
{'LAYERS': layers, 'OUTPUT':"TEMPORARY_OUTPUT"})["OUTPUT"]
QgsProject.instance().addMapLayer(merged, False)
root.insertLayer(0, merged)
2 Answers 2
You can use the QgsMapLayer.setName()
function. Based on your script, you can do something along the lines of:
merged.setName("This is the new layer name")
-
I write it at the end of the script but it gives me errorAhmed Kamel– Ahmed Kamel2022年01月23日 11:02:02 +00:00Commented Jan 23, 2022 at 11:02
-
What is the error message? Otherwise, I would suggest to put the command right after you have executed the processing algorithm (i.e., before calling
addMapLayer()
)fastest– fastest2022年01月23日 11:05:57 +00:00Commented Jan 23, 2022 at 11:05 -
what is the error? it works.Jacqueline– Jacqueline2022年01月23日 16:39:40 +00:00Commented Jan 23, 2022 at 16:39
-
I tried this and it's working ``` blockLayer = QgsProject.instance().mapLayersByName("Merged")[0] blockLayer.setName("SL Dozers")Ahmed Kamel– Ahmed Kamel2022年01月23日 16:54:59 +00:00Commented Jan 23, 2022 at 16:54
In a comment you said "I tried this and it's working"
blockLayer = QgsProject.instance().mapLayersByName("Merged")[0]
blockLayer.setName("SL Dozers")
This may not work if there should be more than one layer with the same name in your project. See my answer to a similar question for a failsafe way: https://gis.stackexchange.com/a/352267/10123
output = processing.run("native:mergevectorlayers",\
{'LAYERS': layers, 'OUTPUT':"TEMPORARY_OUTPUT"})["OUTPUT"]
createdlayer = QgsProject.instance().mapLayer(output)
createdlayer.setName('My shiny new beautiful layer')
(Explanation: What is returned by the algorithm is a dictionary where you can find an id for the layer, then you can get and rename the layer using the unique id rather than the name)
-
there is an error with "parmas" in the first line you typedAhmed Kamel– Ahmed Kamel2022年01月23日 20:17:03 +00:00Commented Jan 23, 2022 at 20:17
-
That is just whatever parameters you put onto the function, I rewrote it to use your example, hope this worksMortenSickel– MortenSickel2022年01月23日 21:00:00 +00:00Commented Jan 23, 2022 at 21:00
;
with Python