[GIS] iface.addVectorLayer on a gml file causes “Select vector layers to add” dialog (pyqgis)

pyqgis

I am trying to load a directory full of zipped GML files (*.gz) into QGIS using pyqgis' iface.addVectorLayer(), but as the GML file contains multiple separate vector layers (Area, Text, Symbol, Point and Line), the "Select vector layers to add" dialog box comes up for each GML in the folder. This means that rather than saving me time by loading all the files in at once, I would still have to click on the dialog box for every layer in the folder.

The ideal situation is that all of the vector layers within each GML are added into QGIS without any manual intervention. How does one select all of the vector layers in the dialog and add them to the gui without manual intervention?

I'm running this from the python console within QGIS.

import os
for r, d, fi in os.walk(path):
    for f in fi:
        iface.addVectorLayer(os.path.join(r, f), f, "ogr")

Best Answer

Thanks to @detlev for the idea of looking at the layer source: iface.activeLayer().dataProvider().dataSourceUri()

gives

u'/vsigzip/J:\\GIS\\Data\\OSMM\\6417514-NH2708-5i6.gz|layername=TopographicArea'

It turns out that GML files are stored in a virtual file system vsigzip (http://erouault.blogspot.co.uk/2012/05/new-gdal-virtual-file-system-to-read.html)

so a (slightly bodged) solution is

import os

names = ["TopographicArea", "CartographicText", "CartographicSymbol",
       "TopographicPoint", "TopographicLine"]

for r, d, fi in os.walk(path):
    for f in fi:
        if os.path.splitext(f)[1] == ".gz":
            for name in names:
                iface.addVectorLayer("/vsigzip/"+os.path.join(r, f)+"|layername="+name, f, "ogr")
Related Question