3. June 2015pgrouting enabled, or you had some network and used this via the roadgraph plugin. I would like to show you, how to do routing and path finding via googlemaps and import the path into QGIS. Big advantage: You don’t even have to think about a network…
Current SolutionsMy current knowledge of routing possibilities in QGIS is quite limited. I only know these three:
- roadgraph plugin
- points to route
pgroutingpgrouting is not a routing method. Instead it is an extension for postgresql like postgis. It requires you not only to have access to a postgresql database which you can administrate but also to have a transportation layer. Once you have it imported into the database you can use pgrouting algorithms on this network. Anita Graser covered the whole process in a blog post 2 years ago. pgrouting is probably the most versatile approach regarding routing in QGIS.
roadgraph pluginThe roadgraph plugin makes it a bit easier to work with points and routes. All you need is a local shapefile which serves as a network. Once you have this, the plugin is able to snap o the network, calculate travel times (if you have the data), lengths and the shortest path from one way to another. Keep in mind that we don’t talk about real-life routing as this plugin doesn’t know any traffic lights ;-).
points to routepoints to route is quite comparable to the roadgraph plugin but offers less more possibilities or outputs. It simply uses a polyline layer as a network and creates routes for a set of input points along the network.
Routing using GoogleMaps APIThe primary problems on the above mentioned solutions are:
- You need a periodic update of your network layer
- acquire a network layer
- prob. install a postgres DB
- log in at the Google dev page.
- Create a new project.
- Choose “Enable an API”.
- Browse for the API and activate the following APIs: Directions API
- Click on Credentials.
- Click Create new Key for a new public key.
- Select “Server Key”.
- Click Create.
sudo pip install googlemapsOn Windows I was using the OSGeo4W Shell and:
pip install googlemapsOnce you got this, the finding of a direction is quite easy from the python coding in the qgis python console:
import googlemaps from datetime import datetime now = datetime.now() gmaps = googlemaps.Client(key="put YOUR key here!") directions_result = gmaps.directions("Dessau, Germany", "Berlin, Germany",mode="driving",departure_time=now) point_str = str(directions_result["overview_polyline"]["points"])The result is an ugly list of dictionaries. Especially the rout is not given as a pair of nice X-Y coordinates. Instead it is a encoded polyline as Google names it. The good thing: we can handle it!
Importing the route into QGISFirst we need to decode the polyline string into X and Y coordinate pairs as this string representing our polyline is damn ugly:
coord_chunks = [] for char in point_str: value = ord(char) - 63 split_after = not (value & 0x20) value &= 0x1F coord_chunks[-1].append(value) if split_after: coord_chunks.append() del coord_chunks[-1] coords =  for coord_chunk in coord_chunks: coord = 0 for i, chunk in enumerate(coord_chunk): coord |= chunk << (i * 5) if coord & 0x1: coord = ~coord #invert coord >>= 1 coord /= 100000.0 coords.append(coord) points =  prev_x = 0 prev_y = 0 for i in xrange(0, len(coords) - 1, 2): if coords[i] == 0 and coords[i + 1] == 0: continue prev_x += coords[i + 1] prev_y += coords[i] points.append((round(prev_x, 6), round(prev_y, 6))) print pointsNow we have a smooth list of coordinates. We will convert this into vertices of a new polyline shapefile:
from PyQt4.QtCore import QVariant layer = QgsVectorLayer('LineString', 'route', "memory") pr = layer.dataProvider() pr.addAttributes([QgsField("attribution", QVariant.String)]) layer.updateFields() fet = QgsFeature() seg= for i in range(0,len(points)): seg.append(QgsPoint(points[i],points[i])) fet.setGeometry(QgsGeometry.fromPolyline(seg)) fet.setAttributes(["route provided by google maps api"]) pr.addFeatures([fet]) layer.updateExtents() #update it QgsMapLayerRegistry.instance().addMapLayer(layer)