Using OpenStreetMap's points of interest in QGIS

OpenStreetMap has an API called Overpass that lets you access OSM data dynamically. In this recipe, we'll add some OSM tourism points of interest to a map.

Getting ready

You will need to use the QGIS Plugin Manager to install the Quick OSM plugin.

You will also need to download the following shapefile and unzip it to your qgis_data/ms directory:

https://geospatialpython.googlecode.com/svn/MSCoast_geo.zip

How to do it...

We will load our base layer that defines the area of interest. Then, we'll use the Processing Toolbox to build a query for OSM, download the data, and add it to the map. To do this, we need to perform the following steps:

  1. First, we need to import the processing module:
    import processing
    
  2. Next, we need to load the base layer:
    lyr = QgsVectorLayer("/qgis_data/ms/MSCoast_geo.shp", "MS Coast", "ogr")
    
  3. Then, we'll need the layer's extents for the processing algorithms:
    ext = lyr.extent()
    w =  ext.xMinimum()
    s =  ext.yMinimum()
    e =  ext.xMaximum()
    n =  ext.yMaximum()
    
  4. Next, we create the query:
    factory = processing.runalg("quickosm:queryfactory",
    "tourism","","%s,%s,%s,%s" % (w,e,s,n),"",25)
    q = factory["OUTPUT_QUERY"]
    
  5. The Quick OSM algorithm has a bug in its output, so we'll create a properly formatted XML tag and perform a string replace:
    bbox_query = """<bbox-query e="%s" n="%s" s="%s"  w="%s"/>""" % (e,n,s,w)
    bad_xml = """<bbox-query %s,%s,%s,%s/>""" % (w,e,s,n)
    good_query = q.replace(bad_xml, bbox_query)
    
  6. Now, we download the OSM data using our query:
    results = processing.runalg("quickosm:queryoverpassapiwithastring","http://overpass-api.de/api/",good_query,"0,0,0,0","",None)
    osm = results["OUTPUT_FILE"]
    
  7. We define the names of the shapefiles we will create from the OSM output:
    poly = "/qgis_data/ms/tourism_poly.shp"
    multiline = "/qgis_data/ms/tourism_multil.shp"
    line = "/qgis_data/ms/tourism_lines.shp"
    points = "/qgis_data/ms/tourism_points.shp"
    
  8. Now, we convert the OSM data to shapefiles:
    processing.runalg("quickosm:ogrdefault",osm,"","","","",poly,multiline,line,points)
    
  9. We place the points as a layer:
    tourism_points = QgsVectorLayer(points, "Points of Interest", "ogr")
    
  10. Finally, we can add them to a map:
    QgsMapLayerRegistry.instance().addMapLayers([tourism_points, lyr]
    

How it works...

The Quick OSM plugin manages the Overpass API. What's interesting about this plugin is that it provides processing algorithms in addition to a GUI interface. The processing algorithm that creates the query unfortunately formats the bbox-query tag improperly, so we need to work around this issue with the string replace. The API returns an OSM XML file that we must convert to shapefiles for use in QGIS.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset