0

I successfully find coding for calling data from postgresql into QGIS layer panel.

enter image description here

The coding as below;

import qgis
root = qgis.core.QgsProject.instance().layerTreeRoot()
groupName = root.addGroup('Cerun')

uri = QgsDataSourceURI()
uri.setConnection("localhost", "5432", "amsdb", "postgres", "postgres")

uri.setDataSource('public', 'slope', 'geom', '', 'name')
layer = qgis.core.QgsVectorLayer(uri.uri(), 'slope', 'postgres')
print layer.isValid()
QgsMapLayerRegistry.instance().addMapLayer(layer,False)
groupName.addLayer(layer)


uri.setDataSource("public", "slopeinfo", None)
layer = QgsVectorLayer(uri.uri(), "slopeinfo", "postgres")
print layer.isValid()
QgsMapLayerRegistry.instance().addMapLayer(layer,False)
groupName.addLayer(layer)

uri.setDataSource("public", "services_facilities", None)
layer = QgsVectorLayer(uri.uri(), "services_facilities", "postgres")
print layer.isValid()
QgsMapLayerRegistry.instance().addMapLayer(layer,False)
groupName.addLayer(layer)

uri.setDataSource("public", "sign_of_distress", None)
layer = QgsVectorLayer(uri.uri(), "sign_of_distress", "postgres")
print layer.isValid()
QgsMapLayerRegistry.instance().addMapLayer(layer,False)
groupName.addLayer(layer)

uri.setDataSource("public", "surface_subsurface", None)
layer = QgsVectorLayer(uri.uri(), "surface_subsurface", "postgres")
print layer.isValid()
QgsMapLayerRegistry.instance().addMapLayer(layer,False)
groupName.addLayer(layer)

uri.setDataSource("public", "engineering_judgement", None)
layer = QgsVectorLayer(uri.uri(), "engineering_judgement", "postgres")
print layer.isValid()
QgsMapLayerRegistry.instance().addMapLayer(layer,False)
groupName.addLayer(layer)

uri.setDataSource("public", "structure", None)
layer = QgsVectorLayer(uri.uri(), "structure", "postgres")
print layer.isValid()
QgsMapLayerRegistry.instance().addMapLayer(layer,False)
groupName.addLayer(layer)

enter code here

The problem is I want to adding all the table layer under Group>SubGroup>Layer just like picture below. All the Group>SubGroup>Layer already in Layer Panel. Need to find coding to adding the table under Group>SubGroup>Layer.

enter image description here

Can anyone help?

2
  • 2
    Can I first recommend updating your version of QGIS to 3.X and by so moving on to use python 3? – Dror Bogin Mar 9 at 8:17
  • it must be maintain in version 2 because out of maintenance period. only need to add little things – Khadijah Nas Mar 9 at 8:28
0

If your sub group name is unique, you can use

project = QgsMapLayerRegistry.instance()
# project = QgsProject.instance() # If QGIS 3.0
root = project.layerTreeRoot()
mygroup = root.findGroup("CERUN")
project.addMapLayer(layer, False)
mygroup.addLayer(layer)

Or if same name for multiple subgroups, take the parent and then select it child from subgroup name

project = QgsMapLayerRegistry.instance()
# project = QgsProject.instance() # If QGIS 3.0
root = project.layerTreeRoot()
mygroup = root.findGroup("KATEGORI")
mysubgroup = mygroup.findGroup("CERUN")
project.addMapLayer(layer, False)
mysubgroup.addLayer(layer)

You may need to create the subgroup (if not here) with

project = QgsMapLayerRegistry.instance()
# project = QgsProject.instance() # If QGIS 3.0
root = project.layerTreeRoot()
mygroup = root.findGroup("KATEGORI")
mysubgroup = root.addGroup("CERUN")
project.addMapLayer(layer, False)
mysubgroup.addLayer(layer)
1
  • Thanks Thomas with the multiple choice. I have try and alter it based on your ideas. – Khadijah Nas Mar 11 at 2:17
0

Thanks so much Thomas, already working with the script below;

import qgis
root = qgis.core.QgsProject.instance().layerTreeRoot()
groupName = root.findGroup('KATEGORI')
subgroupName = groupName.findGroup("CERUN")
subgroupName = groupName.findGroup("Slope") 

Its run smoothly when run it manually on Python Console window.

The new prob comes when i add all these new codes in plugin python script. After run the plugin, the result turns no tables has been added on the QGIS layer panel.

Can anyone familiar with QGIS customize plugin help me?

1
  • Not sure but why do you use subgroupName = groupName.findGroup("Slope") here? "Slope" is not a group but a layer according to your screenshot. I would expect use of findLayer e.g qgis.org/pyqgis/master/core/… – ThomasG77 Mar 11 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.