3

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)

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Ahmed Kamel
  • 429
  • 2
  • 6

2 Answers2

5

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")
Jacqueline
  • 129
  • 1
  • 10
fastest
  • 1,289
  • 7
  • 14
2

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)

MortenSickel
  • 3,734
  • 1
  • 19
  • 40