Using the OpenStreetMap Service: QGIS Python Programming CookBook

PACKT Books

Updated:

Learn how to use the OpenStreetMap Service in QGIS using Python from this section as part of this preview chapter, Creating Dynamic Maps from QGIS Python Programming CookBook. With 140 short, reusable recipes to automate geospatial processes in QGIS, the QGIS Python Programming CookBook teaches readers how to use Python and QGIS to create and transform data, produce appealing GIS visualizations, and build complex map layouts.

Using the OpenStreetMap Service

Cloud-based technology is moving more and more data to the Internet and GIS is no exception. QGIS can load web-based data using Open GIS Consortium standards such as Web Map Service (WMS). The easiest way to add WMS layers is using the Geospatial Data Abstraction Library (GDAL) and its virtual file system feature to load a tiled layer.

Getting ready

You don’t need to do any preparation for this recipe other than to open the Python Console plugin within QGIS.

How to do it…



Free weekly newsletter

Fill out your e-mail address to receive our newsletter!
Email:  

By entering your email address you agree to receive our newsletter and agree with our privacy policy.
You may unsubscribe at any time.



We will create an XML template describing the tiled web service from OpenStreetMap we want to import. Then we’ll turn it into a GDAL virtual file and load it as a QGIS raster layer.

  1. First we import the GDAL library;

fromosgeo import gdal

  1. Next we’ll create our XML template describing the OpenStreetMap tiled web service:

xml = “””<GDAL_WMS>

<Service name=”TMS”>

<ServerUrl>http://tile.openstreetmap.org/${z}/${x}/${y}.png</ServerUrl>

</Service>

<DataWindow>

<UpperLeftX>-20037508.34</UpperLeftX>

<UpperLeftY>20037508.34</UpperLeftY>

<LowerRightX>20037508.34</LowerRightX>

<LowerRightY>-20037508.34</LowerRightY>

<TileLevel>18</TileLevel>

<TileCountX>1</TileCountX>

<TileCountY>1</TileCountY>

<YOrigin>top</YOrigin>

</DataWindow>

<Projection>EPSG:900913</Projection>

<BlockSizeX>256</BlockSizeX>

<BlockSizeY>256</BlockSizeY>

<BandsCount>3</BandsCount>

<Cache />

</GDAL_WMS>”””

  1. Now we’ll create the path for our GDAL virtual file system file:

vfn = “/vsimem/osm.xml”

  1. Next we use GDAL to create the virtual file using the path and the XML document:

gdal.FileFromMemBuffer(vfn, xml)

  1. Now can create a raster layer from the virtual file:

rasterLyr = QgsRasterLayer(vfn, “OSM”)

  1. Before we add the layer to the map, we’ll make sure it’s valid:

rasterLyr.isValid()

  1. Finally add the layer to the map:

QgsMapLayerRegistry.instance().addMapLayers([rasterLyr])

How it works…

There are other ways to load tiled map services like OpenStreetMap into QGIS programmatically but GDAL is by far the most robust. The prefix, vsimem, tells GDAL to use a virtual file to manage the tiles. This approach frees you from having to manage downloaded tiles on disk directly and focus on your application’s functionality.


QGIS Python Programming CookBook

<– Previous: Layer Symbols   |     Next: Bing Aerial Image Service –>

Photo of author
About the author
PACKT Books