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")
  • 2
    A general hint: load such layer in QGIS once and save the layer. Then look into the saved file and search the connection string Qgis uses to read the next time without your interaction. Use this connection string instead of the file name. For myself this way is often much easier to go, then understanding OGR documentation
    – Detlev
    Commented Jan 28, 2016 at 22:07

1 Answer 1


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



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")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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