2

I am trying to use the QGIS python API to read out the groups and layers in a QGIS (2.4) project. Here is a code snippet from my script, which is run from a shell, not from the QGIS python console!

from PyQt4.QtCore import QFileInfo
from qgis.core import *

proj=QgsProject.instance()
proj.read(QFileInfo("/var/qgis-projects/testproject.qgs"))

print "QGis version:"+QGis.QGIS_VERSION
print "Project title: "+proj.title()
print "Project tree root children:"
root=proj.layerTreeRoot()
for node in root.children():
    print "- "+node.name()+" ("+str(type(node))+")"

It prints the following when I run it:

QGis version:2.4.0-Chugiak
Project title: This is my project title
Project tree root children:
- Field Data (<class 'qgis._core.QgsLayerTreeGroup'>)
- Aerial Photos (<class 'qgis._core.QgsLayerTreeGroup'>)
- Other Maps (<class 'qgis._core.QgsLayerTreeGroup'>)
- Model Results (<class 'qgis._core.QgsLayerTreeGroup'>)

Only QgsLayerTreeGroup objects are returned but no layers (QgsLayerTreeLayer objects), even though I also have two layers in the project root. When I open the python console in QGIS while I have my project open and run:

QgsProject.instance().layerTreeRoot().children()
[<qgis._core.QgsLayerTreeGroup object at 0xa3f9170>, <qgis._core.QgsLayerTreeGroup object at 0xa3f9200>, <qgis._core.QgsLayerTreeGroup object at 0xa3f9290>, <qgis._core.QgsLayerTreeGroup object at 0xa3f9320>, <qgis._core.QgsLayerTreeLayer object at 0xa3fe320>, <qgis._core.QgsLayerTreeLayer object at 0xa3fe830>]

This does return the QgsLayerTreeLayer objects for my layers that I'm after. How come there is a difference?

Edit: I'm trying to run my python script from a shell, not from the QGIS python console.

user39235
  • 81
  • 1
  • 5

2 Answers2

2

It turned out I did not properly initialize QGis like you're supposed to when writing standalone scripts. Adding the following lines made everything work:

QgsApplication([], True) #a path was not needed
QgsApplication.initQgis()

#do all the things with your QgsProject here

QgsApplication.exitQgis()

This question and the PyQGIS Developer Cookbook got me on the right track.

user39235
  • 81
  • 1
  • 5
1

look at Martin Dobias's QGIS Layer Tree API (Part 1)

from PyQt4.QtCore import QFileInfo # no need of from qgis.core import * in the console)
root=proj.layerTreeRoot()
proj.read(QFileInfo("/var/qgis-projects/testproject.qgs"))
root=proj.layerTreeRoot()
for child in root.children():
    if isinstance(child, QgsLayerTreeGroup):
         print "- group: " ,child.name()
    elif isinstance(child, QgsLayerTreeLayer):
         print "-layer: ", child.layerName()
gene
  • 54,868
  • 3
  • 110
  • 187
  • Thanks for your answer. Sorry, I meant to say I'm trying to run my script from a shell, not from the QGIS console. I think I understand how the code works (I found Martin's posts as well) but for some reason in my case root.children() only returns QgsLayerTreeGroup objects and no QgsLayerTreeLayer objects, even though I clearly also have layers in my project. – user39235 Oct 16 '14 at 11:08
  • No problem for me from the shell. What is your QGIS version ? – gene Oct 16 '14 at 18:25