I have a vector file that contains multiple layers, if I open it in QGIS I get the "Select vector layers to add dialog" and I can choose one and it loads fine.
However in a .py script if I load it via
layer = QgsVectorLayer(filename, "mylayer", "ogr")
I just get one of the layers (layer 0)
How do I specify which layer I want to load? Or if it IS actually loading all of them, how do I change to the one I want?
My full script
import os
import sys
import qgis
from qgis.core import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import ogr
qgs = QgsApplication([], False)
QgsApplication.setPrefixPath( r"C:\Program Files\QGIS 2.18\apps\qgis-ltr", True )
QgsApplication.initQgis()
filename = "C:/datafiles/sourcefile"
if os.path.exists(filename):
print('found it')
layer = QgsVectorLayer(filename, "mylayer", "ogr")
if layer.isValid():
print("Layer was loaded successfully!")
else:
print("Layer failed to load!")
sys.exit()
print (layer.type())
print (layer.name())
print (layer.id())
print('finished')
qgs.exitQgis()