Digital Geography

21. June 2015

How far can you travel? Answers in QGIS with OSM route.

My latest posts on routing in QGIS were read from some guys at the University Heidelberg who provide the API for the routing. The contacted me with the simple call: “Let’s create a plugin”. And here it is: OSM route…

OSM route

OSM route is more or less a possibility to interact with the service behind the domain openrouteservice.org. It lets you use addresses as input for your start- and end-point as well as an address to be on the way. This information is send to the API of openrouteservice.org and the response is converted into a polyline. Additionally I am adding the points to the map as a point shapefile. At the moment it only analysis the whole route and does not separate each segment of the route.

Interface of OSM route… simple as fcuk

For longer routes it took some time to convert the graph. Here are some measures for different routes (travel mode car, distance type: fastest):
  • 0.224828s Distance: 1045.4578 km from Berlin to Paris
  • 0.148206s Distance: 573.9421 km from Berlin to Warschau
  • 0.21995s Distance: 491.5858 km from Berlin to Würzburg
  • 0.868936s Distance: 4494.0776 km from Berlin to Kairo
  • 1.128764s Distance: 7880.0434 km from Berlin to UlaanBaatar

routes between major cities in QGIS

Reachability/Service Times

But that’s not all! Additionally to routing you can use the plugin to calculate the reachability of places in a given time period. So called isochrones. Actually the plugin does not create lines but polygons. You can choose a time (maximum 30min as stated by the API provider) and a time for given intervals. So choosing 10 as time and 2 as intervals will provide 5 polygons for each min-interval (2min, 4min, 6min, …). This will give you the great opportunities to compare different locations in their “centrality” or to choose your next flat wisely if you have the latest point shapefile of club-locations or book stores in your desired area. It works on the input address only!

Berlin with its accessibility by car in 30min divided in 5min intervals

You can participate with any errors/issues via Github. It is available using the official plugin page of QGIS and via the “Manage and Install Plugins” dialog in QGIS. The plugin is marked as experimental ATM.

ATTENTION: The OpenRouteService.org API does allow routing only on the continents Europe, Asia, Africa and Australia. We are in contact regarding this issue.

  • Florent

    Nice post! But i cant find where to download this Plugin ?

    • Enrico

      1.The download link is here: https://plugins.qgis.org/plugins/OSMroute/
      2. Otherwise within QGIS plugin manager enable “experimental plugins” under settings and then search for “OSM route” and install as usually

      • Florent

        Thanks a lot

  • awesome.. keep it going! I think it will help me a lot.. by the way, do you know any plugin to search features values within the “view”.. I have made one for Arcview 3.x that suited my needs, but I can´t get it around with pyqgis (can´t find a GOOD basic programming tutorial). I use the MMQGIG plugin, which is similar to the one I made, but is still missing some things..

    • You are searching for OSM data in the view or for every layer features in your current view?

      • every layer features.. I work in urban forestry and
        have made a system in arcview 3.x to manage the 16K features (trees). It is used by people with few computer skills, so I had to make it easy-to-use.. now I am trying to migrate to QGIS but I couldn’t find time to learn python to custom my system..

        blow the dust of arcview 3.x and have a look at my ext.. https://www.dropbox.com/s/92zk25dds9o6ghg/busqueda1-3.avx?dl=0

        I hope some day somebody make a good videotutorial to learn pyQgis from the beginning.. well, thanks to answer Riccardo, I love your work, keep it going!

  • F. Shah

    Good work.. Last year there was a very good blog post by Paul Blazer’s on “ÖPNV Fahrzeit Heatmap”, you might want to have a look on it.

    http://mechlab-engineering.de/2014/05/tutorial-entstehung-der-oepnv-fahrzeit-heatmaps-2-0-mit-postgresapp-tilemill-und-qgis/

    • saw it. I hope openrouteservice.org will integrate this sort of routing in the near future!

  • Jani Kinnunen

    Nice! Any idea which plugin to use in QGIS to to find shortest and fastest route with multiple stops between start and end point?

    • We can enhance the plugin for you 😉 it’s all about the money… and please make up an issue on github. maybe we will include the functionality for free…

    • I join the request. I will visit github.. could I fork it?

  • Hey Riccardo, the button that says “map it” is for selecting the start address directly from the map? isn’t available.I couldn’t make the plugin work for 2 cities in argentina, a python error comes out..

    • south america is not supported as well, sorry! the openrouteservice.org page will support it in a near future. the map it button is a future development to slect the address interactively on the map

    • Argentina is not supported from openrouteservice.org. the map it buttons will be enabled in a fture release of the plugin.

      • Thanks. I’ll keep using GeoSearch plugin for now.. Isn’t bad anyway.

  • Mohammed

    Hi Riccardo, I always have a python error even when I enter same parameters in the example provided above?

  • MikLet

    Hi Riccardo,
    I would like to know the parameters designed in pedestrian and car ? i.e. how much km/h miles/h ?

    I’m trying to go further usefullness of your plug-in. I just want to understand, and try to understand strenght and weakness of such usefull plug-in.

    I identify one issue about OSM quality. but others ?

    Topography is taken into account ?

  • Many05

    Great work and very usefull
    ! Is it possible to add multiple addresses from a file?

    Or for now we can not go a single address by hand ?
    Thank you for your response

    • You can always make up an issue or the whish of enhancement on the github page of the plugin. Yet we have to face it: multiple addresses willcreate multiple calls/requests and the API only accepts 1000 per hour which makes 31 addresses.
      Maybe there will be someone who will work on this issue in his/her sparetime. If you need it asap, just send us a line to info@geolicious.de and we will write an offer for your requirements.
      The second: what do you mean by “Or for now we can not go a single address by hand ?”

      • Many05

        Thanks for your return.
        I have my answer.

        By “Or for now we can not go a single address by hand ?”, I mean informing an address manually, but it’s ok for me.

        Thanks again,

  • Niel de Jong

    Hi Riccardo,

    Great plugin tnx! I’m computing a set of isochrones at the moment, 5 minutes max with intervals of one minute by foot. Sometimes it leaves out the first minute or couple of minutes, any idea why it would do that?

    • Hi Niel. I think this is a problem of the travelling distance: I mean sometime you cant walk to the next OSM node in 1 min… Can you test it on openroutservice.org whether this works there?

  • Stefan Frerichs

    Hi Riccardo,
    Thanks for the plugin. Unfortunately, it produces in QGis 2.16 nonsensical coordinates when I try to calculate the isochrones with respect to a motorway (30 min, 2 minute interval). As a starting point I use a coordinate from the website (51. 882 8. 4311), which I copy to the plugin. The website produced the desired result, unfortunately I can not use it. What could be wrong?

  • Shen Ken Ping

    Hi Riccardo,
    First I want to thanks for this useful plugin.
    Just have a problem is I do remember that I could put coordinate in “Start address”, but now it’s always shows me that :
    Python error: An error has occurred while executing Python code: See message log (Python Error) for more details.
    I do know that I can put address but for a big park or construction I need a precise point, is that possible to put coordinate?

    • it worked in my case just lng lat. no separator, decimals separated by poin.
      can you give me the coordinates?

      • Shen Ken Ping

        yes of course
        it’s 49.42704,7.75837,should be in Pfaffenbergstraße 97, 67663 Kaiserslautern

        • The plugin works with a geocoding of the input field. I could enhance the functionality to use a checkbox so user can also input coordinates directly instead of an address.

  • Philipp

    Hi Riccardo,
    This plugin is really great but I have a small issue.
    If I open an saved map with a isochrone the layer isn’t visible. Do I have to save the isochrome layer individual?

  • Simon

    Hei Ricardo,
    thanks for you great work!
    I want to run your plugin with pyQGIS, however I do not understand how to add my custom start/endpoint. I struggle to modify OSMrouteDialog(). Is there a way to a) access it with pyQGIS or b) use your python code and feed with my data?
    thanks for your help

    • Hi Simon,

      the plugin itself is written in pyQGIS. You can use parts of the source code or embed your own lines in the downloaded version. you can acces the plugin in your qgis2 directory und python/plugins.
      What struggle do you see with your “custom start/endpoint”? Maybe ask this on gis.stackexchange as there are for more experts available compared to this thread here.
      You can also ping me directly: riccardo.klinger@geolicious.de
      I hope this helps.

      Best regards,

      Riccardo

      • Simon

        Hi Ricardo,
        thanks for your quick answer.
        I am new to pyQGIS and python and I wanted to run your plugin using the python console in QGIS. However I failed to do so. I was trying to run OSMroute.py and then customize it to bypass the dialog and add a startpoint manually.
        For instance I get:
        ImportError: No module named resources_rc
        But I did stored this file right in the same folder as OSMroute or is it a special/global package I need to install?
        Thanks for your help, I couldnt find “my case” in gis.stackexchange, but i seems very helpful.
        best,
        Simon

        • Hi SImon,
          the core of it are the lines 316-391 of OSMroute.py. Replace all the variables with desired values and you should be able to work with that in the commandline. But be aware of the needed modules:

          from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication

          from PyQt4.QtGui import QAction, QIcon, QColor

          from PyQt4 import QtCore, QtGui
          from qgis.core import * #to get access to qgis

          #we will need these for getting the ors response and to parse it:

          from xml.etree import ElementTree

          import urllib2, os, qgis.utils, os.path, resources_rc, time

          #we need qvariant to build the shapefile

          from PyQt4.QtCore import QVariant

          import processing
          import time

          • Simon

            Hi Riccardo,
            it seems that I got the problem solved about the imports. However everthing else seems to be much more complicated than I anticipated 🙂
            Maybe I need to look more into python itself.
            Even if I customize the input in the metioned lines, I am not able to run the “run” function in OSMroute.py as it is called within “__init__” which is called in OSMroute_dialog.py which I obviously dont need/use as I want to skip the gui and add the input in the python console. Or should I run OSMroute_dialog.py as a main methode?
            Sorry to bother you and thanks for your help
            Simon

          • Hi Simon,

            don’t use the files at all! I suggested just to use the code lines. but for this: here is the minimal example:
            from xml.etree import ElementTree
            import urllib2, os, qgis.utils, os.path, resources_rc, time
            #we need qvariant to build the shapefile
            from PyQt4.QtCore import QVariant
            import processing
            from qgis.core import * #to get access to qgis
            from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
            from PyQt4.QtGui import QAction, QIcon, QColor
            from PyQt4 import QtCore, QtGui
            import time
            url=”http://openls.geog.uni-heidelberg.de/routing?api_key=e2017639f5e987e6dc1f5f69a66d049c”
            text = ”’

            Car

            Fastest

            12.2429261 51.8311104

            13.4385964 52.5198535

            ”’
            req = urllib2.Request(url=url, data=text, headers={‘Content-Type’: ‘application/xml’})
            response_route=urllib2.urlopen(req).read()
            newstr = response_route.replace(“n”, “”)
            response_route = newstr.replace(” “, “”)

            if response_route != “”:
            xml_route = ElementTree.fromstring(response_route)
            layer = QgsVectorLayer(‘LineString?crs=EPSG:4326’, ‘route_OSM’, “memory”)
            pr = layer.dataProvider()
            pr.addAttributes([QgsField(“attribution”, QVariant.String)])
            pr.addAttributes([QgsField(“distance”, QVariant.Double)])
            pr.addAttributes([QgsField(“time”, QVariant.String)])
            layer.updateFields()
            fet = QgsFeature()
            seg=[]
            for i in range(0,len(xml_route[1][0][1][0])):
            seg.append(QgsPoint(float(str.split(xml_route[1][0][1][0][i].text)[0]),float(str.split(xml_route[1][0][1][0][i].text)[1])))
            fet.setGeometry(QgsGeometry.fromPolyline(seg))
            fet.setAttributes([“route provided by openrouteservice.org”, float(xml_route[1][0][0][1].attrib[‘value’]), xml_route[1][0][0][0].text])
            pr.addFeatures([fet])
            layer.updateExtents() #update it
            QgsMapLayerRegistry.instance().addMapLayer(layer)

            use these lines in a new script in the python console. use coordinates as you like

          • Simon

            Hei Riccardo,
            It is working!! Thank you so much for your time, patience and code 😉
            cheers