Skip to main content
added 306 characters in body
Source Link
Fran Raga
  • 7.9k
  • 3
  • 26
  • 48

If you look at the API, the findGroup() method need a String "name value" but findLayer() need a Layer Id not the name.

Then to make it work you need this:

from qgis.core import QgsLayerTreeGroup, QgsLayerTreeLayer
root = QgsProject.instance().layerTreeRoot()

for child in root.children():
    if isinstance(child, QgsLayerTreeGroup):
        print ("- group: " + child.name())
    elif isinstance(child, QgsLayerTreeLayer):
        print ("- layer: " + child.layerId())

Move Loaded layer:

layer = QgsProject.instance().mapLayersByName(<layer_name>)[0]
root = QgsProject.instance().layerTreeRoot()

mylayer = root.findLayer(layer.id())
myClone = mylayer.clone()
parent = mylayer.parent()

group = root.findGroup(<group_name>)
group.insertChildNode(0, myClone)

parent.removeChildNode(mylayer)

Load layer in a specific group

layer = QgsVectorLayer(<layer_path>, "airports", "ogr")

QgsProject.instance().addMapLayer(layer, False)

root = QgsProject.instance().layerTreeRoot()
g = root.findGroup(<group_name>)
g.insertChildNode(0, QgsLayerTreeLayer(layer))

If you look at the API, the findGroup() method need a String "name value" but findLayer() need a Layer Id not the name.

Then to make it work you need this:

from qgis.core import QgsLayerTreeGroup, QgsLayerTreeLayer
root = QgsProject.instance().layerTreeRoot()

for child in root.children():
    if isinstance(child, QgsLayerTreeGroup):
        print ("- group: " + child.name())
    elif isinstance(child, QgsLayerTreeLayer):
        print ("- layer: " + child.layerId())

Move Loaded layer:

layer = QgsProject.instance().mapLayersByName(<layer_name>)[0]
root = QgsProject.instance().layerTreeRoot()

mylayer = root.findLayer(layer.id())
myClone = mylayer.clone()
parent = mylayer.parent()

group = root.findGroup(<group_name>)
group.insertChildNode(0, myClone)

parent.removeChildNode(mylayer)

If you look at the API, the findGroup() method need a String "name value" but findLayer() need a Layer Id not the name.

Then to make it work you need this:

from qgis.core import QgsLayerTreeGroup, QgsLayerTreeLayer
root = QgsProject.instance().layerTreeRoot()

for child in root.children():
    if isinstance(child, QgsLayerTreeGroup):
        print ("- group: " + child.name())
    elif isinstance(child, QgsLayerTreeLayer):
        print ("- layer: " + child.layerId())

Move Loaded layer:

layer = QgsProject.instance().mapLayersByName(<layer_name>)[0]
root = QgsProject.instance().layerTreeRoot()

mylayer = root.findLayer(layer.id())
myClone = mylayer.clone()
parent = mylayer.parent()

group = root.findGroup(<group_name>)
group.insertChildNode(0, myClone)

parent.removeChildNode(mylayer)

Load layer in a specific group

layer = QgsVectorLayer(<layer_path>, "airports", "ogr")

QgsProject.instance().addMapLayer(layer, False)

root = QgsProject.instance().layerTreeRoot()
g = root.findGroup(<group_name>)
g.insertChildNode(0, QgsLayerTreeLayer(layer))
added 386 characters in body
Source Link
Fran Raga
  • 7.9k
  • 3
  • 26
  • 48

If you look at the API, the findGroup() method need a String "name value" but findLayer() need a Layer Id not the name.

Then to make it work you need this:

from qgis.core import QgsLayerTreeGroup, QgsLayerTreeLayer
root = QgsProject.instance().layerTreeRoot()

for child in root.children():
    if isinstance(child, QgsLayerTreeGroup):
        print ("- group: " + child.name())
    elif isinstance(child, QgsLayerTreeLayer):
        print ("- layer: " + child.layerId())

Move Loaded layer:

layer = QgsProject.instance().mapLayersByName(<layer_name>)[0]
root = QgsProject.instance().layerTreeRoot()

mylayer = root.findLayer(layer.id())
myClone = mylayer.clone()
parent = mylayer.parent()

group = root.findGroup(<group_name>)
group.insertChildNode(0, myClone)

parent.removeChildNode(mylayer)

If you look at the API, the findGroup() method need a String "name value" but findLayer() need a Layer Id not the name.

Then to make it work you need this:

from qgis.core import QgsLayerTreeGroup, QgsLayerTreeLayer
root = QgsProject.instance().layerTreeRoot()

for child in root.children():
    if isinstance(child, QgsLayerTreeGroup):
        print ("- group: " + child.name())
    elif isinstance(child, QgsLayerTreeLayer):
        print ("- layer: " + child.layerId())

If you look at the API, the findGroup() method need a String "name value" but findLayer() need a Layer Id not the name.

Then to make it work you need this:

from qgis.core import QgsLayerTreeGroup, QgsLayerTreeLayer
root = QgsProject.instance().layerTreeRoot()

for child in root.children():
    if isinstance(child, QgsLayerTreeGroup):
        print ("- group: " + child.name())
    elif isinstance(child, QgsLayerTreeLayer):
        print ("- layer: " + child.layerId())

Move Loaded layer:

layer = QgsProject.instance().mapLayersByName(<layer_name>)[0]
root = QgsProject.instance().layerTreeRoot()

mylayer = root.findLayer(layer.id())
myClone = mylayer.clone()
parent = mylayer.parent()

group = root.findGroup(<group_name>)
group.insertChildNode(0, myClone)

parent.removeChildNode(mylayer)
Source Link
Fran Raga
  • 7.9k
  • 3
  • 26
  • 48

If you look at the API, the findGroup() method need a String "name value" but findLayer() need a Layer Id not the name.

Then to make it work you need this:

from qgis.core import QgsLayerTreeGroup, QgsLayerTreeLayer
root = QgsProject.instance().layerTreeRoot()

for child in root.children():
    if isinstance(child, QgsLayerTreeGroup):
        print ("- group: " + child.name())
    elif isinstance(child, QgsLayerTreeLayer):
        print ("- layer: " + child.layerId())