I try to load a .osm file into my QGIS project using python.

So I've write this code:

import processing
from qgis.core import *

# Creation of the .osm file, the QuickOSM plugin is needed.
bbox = '795078.7779580103, 795572.1303964029, 5908035.195114954, 5908380.196820124'

alg_params = {
    'EXTENT': bbox,
    'KEY': 'highway',
    'SERVER': 'https://lz4.overpass-api.de/api/interpreter',
    'TIMEOUT': 25,
    'VALUE': ''
query = processing.run('quickosm:buildqueryextent', alg_params)
file = processing.run("native:filedownloader", {'URL':query['OUTPUT_URL'], 'OUTPUT':'TEMPORARY_OUTPUT'})

# Now I try to display the osm data:
vlayer = iface.addVectorLayer(file['OUTPUT'], "lines", "ogr")

The code do not crash, but since an .osm file can contains several different geometry the iface.addVectorLayer() function open this windows:

enter image description here

And I need to manually select the geometry that I want to display (lines, multipolygons...). Is there a way to bypass this windows ? Is there a way to either load all the different geometry or to select the geometry that I'm interested in directly in the iface.addVectorLayer() function ?

2 Answers 2


I've found a solution based on this answer

It's possible to specify the geometry type by adding |layername=my_geometry_type to the file path.

So for example instead of using:

iface.addVectorLayer(r'C:/myfile.osm', "mylines", "ogr")

I can use

iface.addVectorLayer(r'C:/myfile.osm'+'|layername=lines', "mylines", "ogr")

To only load the lines.

  • Yes, this is correct. You need to append the layer name to the file path. There is also in QuickOSM which will open all sublayers in your OSM file.
    – etrimaille
    Commented Jan 26, 2020 at 19:09

your 'file' is the .osm file that is the result of your query.

shp = processing.run('quickosm:openosmfile', {"FILE":file["OUTPUT"]})

shp will be a dictionary with the following values:

enter image description here

then we can store individual layers in different variables. example, here I'm storing the multipolygons of osm buildings in buildingLyr

buildingLyr = shp['OUTPUT_MULTIPOLYGONS']

and finally add that layer to QGIS

QgsProject().instance().addMapLayer(buildingLyr, 'OSM Buildings')

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.