Skip to main content
added 29 characters in body
Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180

[Updated to QGIS v3.x]

With the new layer list widget (aka legend, ToC or layer tree) added by Martin Dobias since QGIS v.2.4, you can follow this procedure from the QGIS Python console in order to add layers to a specific group (you won't need group indices anymore):

  1. Get the reference of the layer tree.

    root = QgsProject.instance().layerTreeRoot()

  2. Find the desired group (which could be a subgroup).

    mygroup = root.findGroup("streets_group") # We assume the group exists

  3. Create the layer object.

    mylayer = QgsVectorLayer("/Path/to/your/data.shp", "my layer", "ogr")

  4. Load it to the QgsProject (set the second parameter to False since you want to define a custom position for the layer).

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

  5. Add the layer to the desired group.

    mygroup.addLayer(mylayer)

Source: QGIS Layer Tree API, Part 1 and Part 2, by Martin Dobias

[Updated to QGIS v3.x]

With the new layer list widget (aka legend, ToC or layer tree) added by Martin Dobias since QGIS v.2.4, you can follow this procedure from the QGIS Python console in order to add layers to a specific group (you won't need group indices anymore):

  1. Get the reference of the layer tree.

    root = QgsProject.instance().layerTreeRoot()

  2. Find the desired group.

    mygroup = root.findGroup("streets_group") # We assume the group exists

  3. Create the layer object.

    mylayer = QgsVectorLayer("/Path/to/your/data.shp", "my layer", "ogr")

  4. Load it to the QgsProject (set the second parameter to False since you want to define a custom position for the layer).

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

  5. Add the layer to the desired group.

    mygroup.addLayer(mylayer)

Source: QGIS Layer Tree API, Part 1 and Part 2, by Martin Dobias

[Updated to QGIS v3.x]

With the new layer list widget (aka legend, ToC or layer tree) added by Martin Dobias since QGIS v.2.4, you can follow this procedure from the QGIS Python console in order to add layers to a specific group (you won't need group indices anymore):

  1. Get the reference of the layer tree.

    root = QgsProject.instance().layerTreeRoot()

  2. Find the desired group (which could be a subgroup).

    mygroup = root.findGroup("streets_group") # We assume the group exists

  3. Create the layer object.

    mylayer = QgsVectorLayer("/Path/to/your/data.shp", "my layer", "ogr")

  4. Load it to the QgsProject (set the second parameter to False since you want to define a custom position for the layer).

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

  5. Add the layer to the desired group.

    mygroup.addLayer(mylayer)

Source: QGIS Layer Tree API, Part 1 and Part 2, by Martin Dobias

deleted 18 characters in body
Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180

[Updated to QGIS v3.x]

With the new layer list widget (aka legend, ToC or layer tree) added by Martin Dobias since QGIS v.2.4, you can follow this procedure from the QGIS Python console in order to add layers to a specific group (you won't need group indices anymore):

  1. Get the reference of the layer tree.

    root = QgsProject.instance().layerTreeRoot()

  2. Find the desired group.

    mygroup = root.findGroup("streets_group") # We assume the group exists

  3. Create the layer object.

    mylayer = QgsVectorLayer("/Path/to/your/data.shp", "my layer", "ogr")

  4. Load it to the QgsProject (set the second parameter to False since you want to define a custom position for the layer).

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

  5. Add the layer to the desired group.

    mygroup.addLayer(mylayer)

Source: QGIS Layer Tree API, Part 1 and Part 2, by Martin Dobias

With the new layer list widget (aka legend, ToC or layer tree) added by Martin Dobias since QGIS v.2.4, you can follow this procedure from the QGIS Python console in order to add layers to a specific group (you won't need group indices anymore):

  1. Get the reference of the layer tree.

    root = QgsProject.instance().layerTreeRoot()

  2. Find the desired group.

    mygroup = root.findGroup("streets_group")

  3. Create the layer object.

    mylayer = QgsVectorLayer("/Path/to/your/data.shp", "my layer", "ogr")

  4. Load it to the QgsProject (set the second parameter to False since you want to define a custom position for the layer).

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

  5. Add the layer to the desired group.

    mygroup.addLayer(mylayer)

Source: QGIS Layer Tree API, Part 1 and Part 2, by Martin Dobias

[Updated to QGIS v3.x]

With the new layer list widget (aka legend, ToC or layer tree) added by Martin Dobias since QGIS v.2.4, you can follow this procedure from the QGIS Python console in order to add layers to a specific group (you won't need group indices anymore):

  1. Get the reference of the layer tree.

    root = QgsProject.instance().layerTreeRoot()

  2. Find the desired group.

    mygroup = root.findGroup("streets_group") # We assume the group exists

  3. Create the layer object.

    mylayer = QgsVectorLayer("/Path/to/your/data.shp", "my layer", "ogr")

  4. Load it to the QgsProject (set the second parameter to False since you want to define a custom position for the layer).

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

  5. Add the layer to the desired group.

    mygroup.addLayer(mylayer)

Source: QGIS Layer Tree API, Part 1 and Part 2, by Martin Dobias

deleted 18 characters in body
Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180

With the new layer list widget (aka legend, ToC or layer tree) added by Martin Dobias since QGIS v.2.4, you can follow this procedure from the QGIS Python console in order to add layers to a specific group (you won't need group indices anymore):

  1. Get the reference of the layer tree.

    root = QgsProject.instance().layerTreeRoot()

  2. Find the desired group.

    mygroup = root.findGroup("streets_group")

  3. Create the layer object.

    mylayer = QgsVectorLayer("/Path/to/your/data.shp", "my layer", "ogr")

  4. Load it to the QgsMapLayerRegistryQgsProject (set the second parameter to False since you want to define a custom position for the layer).

    QgsMapLayerRegistryQgsProject.instance().addMapLayer(mylayer, False)

  5. Add the layer to the desired group.

    mygroup.addLayer(mylayer)

Source: QGIS Layer Tree API, Part 1 and Part 2, by Martin Dobias

With the new layer list widget (aka legend, ToC or layer tree) added by Martin Dobias since QGIS v.2.4, you can follow this procedure from the QGIS Python console in order to add layers to a specific group (you won't need group indices anymore):

  1. Get the reference of the layer tree.

    root = QgsProject.instance().layerTreeRoot()

  2. Find the desired group.

    mygroup = root.findGroup("streets_group")

  3. Create the layer object.

    mylayer = QgsVectorLayer("/Path/to/your/data.shp", "my layer", "ogr")

  4. Load it to the QgsMapLayerRegistry (set the second parameter to False since you want to define a custom position for the layer).

    QgsMapLayerRegistry.instance().addMapLayer(mylayer, False)

  5. Add the layer to the desired group.

    mygroup.addLayer(mylayer)

Source: QGIS Layer Tree API, Part 1 and Part 2, by Martin Dobias

With the new layer list widget (aka legend, ToC or layer tree) added by Martin Dobias since QGIS v.2.4, you can follow this procedure from the QGIS Python console in order to add layers to a specific group (you won't need group indices anymore):

  1. Get the reference of the layer tree.

    root = QgsProject.instance().layerTreeRoot()

  2. Find the desired group.

    mygroup = root.findGroup("streets_group")

  3. Create the layer object.

    mylayer = QgsVectorLayer("/Path/to/your/data.shp", "my layer", "ogr")

  4. Load it to the QgsProject (set the second parameter to False since you want to define a custom position for the layer).

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

  5. Add the layer to the desired group.

    mygroup.addLayer(mylayer)

Source: QGIS Layer Tree API, Part 1 and Part 2, by Martin Dobias

added 81 characters in body
Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180
Loading
Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180
Loading