A guide to GoogleMaps-like maps with OSM in QGIS

Using OSM data in QGIS is a hot topic but so far, no best practices for downloading, preprocessing and styling the data have been established. There are many potential solutions with all their advantages and disadvantages. To give you a place to start, I thought I’d share a workflow which works for me to create maps like the following one from nothing but OSM:

osm_google_100k

Getting the data

Raw OSM files can be quite huge. That’s why it’s definitely preferable to download the compressed binary .pbf format instead of the XML .osm format.

As a download source, I’d recommend Geofabrik. The area in the example used in this post is part of the region Pays de la Loire, France.

Preparing the data for QGIS

In the preprocessing step, we will extract our area of interest and convert the .pbf into a spatialite database which can be used directly in QGIS.

This can be done in one step using ogr2ogr:

C:\Users\anita_000\Geodata\OSM_Noirmoutier>ogr2ogr -f "SQLite" -dsco SPATIALITE=YES -spat 2.59 46.58 -1.44 47.07 noirmoutier.db noirmoutier.pbf

where the -spat option controls the area of interest to be extracted.

When I first published this post, I suggested a two step approach. You can find it here for future reference:

For the first step: extracting the area of interest, we need Osmosis. (For Windows, you can get osmosis from openstreetmap.org. Unpack to use. Requires Java.)

When you have Osmosis ready, we can extract the area of interest to the .osm format:

C:\Users\anita_000\Geodata\OSM_Noirmoutier>..\bin\osmosis.bat --read-pbf pays-de-la-loire-latest.osm.pbf --bounding-box left=-2.59 bottom=46.58 right=-1.44 top=47.07 --write-xml noirmoutier.osm

While QGIS can also load .osm files, I found that performance and access to attributes is much improved if the .osm file is converted to spatialite. Luckily, that’s easy using ogr2ogr:

C:\Users\anita_000\Geodata\OSM_Noirmoutier>ogr2ogr -f "SQLite" -dsco SPATIALITE=YES noirmoutier.db noirmoutier.osm

Finishing preprocessing in QGIS

In QGIS, we’ll want to load the points, lines, and multipolygons using Add SpatiaLite Layer:

Screenshot 2014-05-31 11.39.40

When we load the spatialite tables, there are a lot of features and some issues:

  • There is no land polygon. Instead, there are “coastline” line features.
  • Most river polygons are missing. Instead there are “riverbank” line features.

Screenshot 2014-05-31 11.59.58

Luckily, creating the missing river polygons is not a big deal:

  1. First, we need to select all the lines where waterway=riverbank.
    Screenshot 2014-05-31 13.14.00
  2. Then, we can use the Polygonize tool from the processing toolbox to automatically create polygons from the areas enclosed by the selected riverbank lines. (Note that Processing by default operates only on the selected features but this setting can be changed in the Processing settings.)
    Screenshot 2014-05-31 13.40.16

Creating the land polygon (or sea polygon if you prefer that for some reason) is a little more involved since most of the time the coastline will not be closed for the simple reason that we are often cutting a piece of land out of the main continent. Therefore, before we can use the Polygonize tools, we have to close the area. To do that, I suggest to first select the coastline using "other_tags" LIKE '%"natural"=>"coastline"%' and create a new layer from this selection (save selection as …) and edit it (don’t forget to enable snapping!) to add lines to close the area. Then polygonize.

Screenshot 2014-05-31 14.38.48

Styling the data

Now that all preprocessing is done, we can focus on the styling.

You can get the styles used in the map from my Github QGIS-resources repository:

  • osm_spatialite_googlemaps_multipolygon.qml … rule-based renderer incl. rules for: water, natural, residential areas and airports
  • osm_spatialite_googlemaps_lines.qml … rule-based renderer incl. rules for roads, rails, and rivers, as well as rules for labels
  • osm_spatialite_googlemaps_roadshields.qml … special label style for road shields
  • osm_spatialite_googlemaps_places.qml … label style for populated places such as cities and towns

qgis_osm_google_100k

31 comments
  1. Even Rouault said:

    Anita, you don’t necessarily need to use Osmosis. You could directly use ogr2ogr with the -spat option to convert an area of interest of a pbf file to spatialite.

  2. Personally I run my OSM data into PostGIS with osm2pgsql and then pull down layers for rendering in QGIS, osm2pgsql does a fair bit of useful pre-processing of the data (creating polygons from waterway=riverbank etc., handling multipolygons, and so on). Ensure the hstore (-k) option is set and you have access to all OSM tags. I certainly think its a less involved workflow than directly loading OSM XML files and manipulating them in SQLlite, and it’s more flexible in accessing OSM’s quirkier attributes than pushing everything to a shape file.

    Some slides which lightly touch on this here: http://www.slideshare.net/SK53/jerry-clough-osm-and-qgis-v10-seqgis-20140402

    • Thanks for your comment! Could you add if osm2pgsql can digest OSM data of any size? I’m asking because of memory issues I ran into with using osm2pgrouting. hstore is great when working with OSM data in PostGIS for sure.

      I think there are use cases which lend themselves to using PostGIS while for others it will be preferable to have a Spatialite file which you can hand out to others together with e.g. a QGIS project file.

      • I usually import region-sized extracts on a virtual machine with less than 2GB of memory assigned to it.. It’s also quite fast..

  3. Markus Straub said:

    Thanks for the guide!

    Also it is very helpful to configure GDAL which attributes should be included in the spatialite DB. This is possible by configuring the GDAL’s OSM driver (http://www.gdal.org/ogr/drv_osm.html). The config file is /usr/share/gdal/1.10/osmconf.ini (in Ubuntu Trusty)

    • Thanks for the pointer towards Osmosis + ogr2ogr, Markus!

  4. Hi Anita, another great article. With regards to using land features, I highly recommend keeping from creating anything and instead using a more accurate data like the GSHHG (http://www.ngdc.noaa.gov/mgg/shorelines/gshhs.html) from NOAA or local data like the Ordnance Survey UK vector (1m accuracy) in the UK.

    • Hi Nicholas,
      Thanks for your comment! Do you have an particular reason to recommend against using the OSM coastline? I find it kind of neat not to have to use any other data source.

      • Hi Anita,
        Just a personal preference, where I work with a lot of detail I find that the OSM coastline isn’t as accurate as the GSHHG, that said one needs to be careful as there are islands missing in the GSHHG!!
        For work that isn’t going to be legally challenged (very little of my work!) OSM is good enough, or as us consultants say “Fit for purpose” :)

  5. Thanks great article!
    Any reason to not use the QGIS 2.2 OSM download/layer exporter plugin?

    • When I tried it there were a lot of features getting lost somewhere along the way. Also I found it less than intuitive.

  6. Tom said:

    I’m trying to find ogr2ogr, and trust that you have to load FWTools. I’m running on Windows (64 bit), and can’t find an FWTools download that works. Can anyone point me in the right direction, or recommend another way to convert the osm file to spatialite?

    • How did you install QGIS? If you used the OSGeo4W installer, start the OSGeo Shell and you will be able to call ogr2ogr.

      • Tom said:

        Yes, I downloaded the OSGeo4W installer. How do I “call” in the OSGeo Shell? Sorry for all the questions, and thank you so much for all your help!

      • If you installed QGIS using the OSGeo4W installer, you usually find a shortcut to the OSGeo4W Shell on the desktop. Otherwise check the folder where you installed OSGeo4W, usually C:\OSGeo4W64, and double-click on OSGeo4W.bat to start the Shell. Then type the ogr2ogr command.

  7. Kristy Dahl said:

    Thank you so much for this tutorial and, especially, for sharing your qml files. So helpful!

  8. Hi Anita! Do you know if there is a difference in the data if it is in the osm2pgsql shapefiles format from say Metro Extracts vs. the method you’ve described above? I keep getting ” unexpected character at line 31 column 71 ” errors applying the style files on the GitHub site. Thanks!!!

    • Hi! Yes there most certainly are differences which unfortunately limit the use of the styles.

  9. Hi again, Anita!

    Got ogr2ogr to create the .db file as per instructions above, the only thing was I left out the -spat option

    First question is why the .db extension is used rather than the .sqlite extension? I ask this because when I try to add a SpatiaLite layer via QGIS 2.4, and point to the .db created by the command above, I get the error:

    Unexpected error when working with: /home/~~~/OSM_Detroit.db

    obsolete libspatialite: connecting to this DB requires using v.4.0 (or any subsequent)

    So then when I try to add the files from the browser window in QGIS (double-click the .db file, then prompted to select the layers I want to add), then try to apply the style files, I get the same error I mentioned before:

    unexpected character at line 31 column 71

    ogr2ogr –version shows GDAL 1.10.0, released 2013/04/24 – is that a problem here?

    Thanks again!

    • Ah! That was the problem – the files I downloaded weren’t correct. I had to go ‘raw’ files then save those as the QML files… great stuff, thanks for your help as usual, Anita!!! And thanks for your work on these styles!

  10. Eric Wenger said:

    Anita, how do you create the road shield layer in QGIS?

    • The road shield layer is just a duplicate of the normal line layer with the special road shield style applied to it.

  11. caccola said:

    Really usefull thanks!

  12. scruss said:

    If you’re running a fairly reliable computer, and you want the ogr2ogr stage to complete before the next ice age, add –config OGR_SQLITE_SYNCHRONOUS OFF

    • Thanks, that sounds like it’s worth a try. How much faster is it in your experience?

  13. Duarte said:

    Hi Anita.

    As usual, very nice post!

    Would it be possible to share your qgis project and data too? I’m looking at getting some kind of benchmark norm for qgis, and this project would be a nice candidate. I’m thinking of scripting a few zooms/scales and random extents. This would then allow us to mantain a hardware reference table that people could look up when deciding what pc to buy.

    • Hi Duarte,
      I can’t share the data anymore but you could download OSM data for a small country and follow the steps above to create your benchmark dataset. The styles are in my Github repo, freely available.

  14. Hello, i’m getting an “unable to open datasource” using the following drivers ….
    I have a portugal-latest.osm.pbf and ogr2ogr that is bundled with Qgis 2.6. Any ideas? Thanks