Aller au contenu | Aller au menu | Aller à la recherche

OpenStreetMap Tiles in QGIS

How to display OpenStreetMap data tiles with no plugin inside Qgis

TMS (Tiled Map Service : http://en.wikipedia.org/wiki/Tile_Map_Service) is the protocol used by OpenStreetMap server to provide access to the tiles. It is also possible to use this protocol to retrieve Google Maps or Mapquest tiles too. TMS is much more simple as WMS, and is a widly used in web applications via OpenLayers or Leaflet. This is a common protocol for webmappers.

I was a bit frustrated not to be able to simply load OpenStreetMap tiles in my prefered GIS desktop application, Qgis. There is a plugin which helps to get tiles from OpenStreetMap, but I do not find it simple enough for me. So I started looking at Gdal. Qgis uses Gdal as a generic driver to read and write raster. Since Gdal 1.7.0, support for TMS has been added. Every Qgis compiled with Gdal 1.7 or higher can therefore read this format directly. The Gdal documentation ( http://www.gdal.org/frmt_wms.html ) is very helpful. Basically, the only thing you have to do is to create a simple XML file with the following content :

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

and save it as openstreetmap_mapnik.xml somewhere in your file system.

You can now simply open Qgis, then use the menu Add a raster layer, and choose the XML file you have just saved. And this is it ! Gdal gets the tiles for you and Qgis display them smoothly. If you have problems to zoom to the correct extent, you can set your project spatial system reference to EPSG:900913, activate on-the-fly reprojection, and add a vector file you know well. The zoom to your vector extent, and then open the XML file as a raster.

Some small issues remain:

  • OSM tiles are shiped in Mercator, so I usually ask Qgis to dynamically reproject my data into Mercator whenever I want to use OSM as a base layer.
  • Gdal creates a gdalwmscache folder in your home directory . Downloaded tiles are stored here and Gdal uses this folder as a cache (which is good, because you won't ask the same tile twice, which will give OSM server some rest). So if you want to refresh old OSM tiles, you have to manually remove the content of this folder some time to time.
  • TMS is a tiles service. So the tiles come in predefined scales. The scales are defined by the provider (Google as its own, OSM too, etc.). If you do not want to see OSM data with a blur effect, you must zoom to one of OSM predefined scale. Otherwise Qgis will show warped OSM tiles. Here are the approximated scales you need to use to display your data correctly :
0	591659030
1	295829515
2	147914757
3	73957378
4	36978689
5	18489344
6	9244672
7	4622336
8	2311168
9	1155584
10	577792
11	288896
12	144448
13	72224
14	36112
15	18056
16	9028
17	4514
18	2257

Since Qgis 1.8, there is a scale combo box in the right bottom of the map, alowing to use predefined scales. It could be great to have the above list of OSM scales instead of the Qgis scales. This is why I proposed a patch to let the use defined a scales list for each project. See : http://hub.qgis.org/issues/5561 . This feature is beeing completed by Alexander Bruy and will be in Qgis master branch soon. This means the next version of Qgis will give you the ability to define and save OSM scales, which will help a lot.

Last hint : in Qgis, you can define the zoom factor to use when zooming in or out with the mouse wheel. If you use the number 2, you will be able to zoom in or out and stay in OSM scales (approximatively).

Now you can easily use OpenStreetMap as a base layer for your Qgis projects ! Happy opensource mapping !

Michael Douchin aka kimaidou