Don’t hesitate to contact us:
Forum: discuss.graphhopper.com
Email: support@graphhopper.com
As our Directions API currently does not include traffic data we still show you that it is possible to integrate traffic data into GraphHopper if you have the data. A few days ago I’ve blogged about a simple way to feed GraphHopper with generic traffic data from elsewhere. Today we look into one specific example: Cologne.
This German city has a nice open data community and an open data portal. There I found the traffic data and it is noted that the update is done every 5-10 minutes so we have rough real time traffic information. For other open data or other cities have a look into codefor.de
The source repository for the necessary changes is also at github. The most important change was to visualize the traffic information directly in the browser, this helps a lot when debugging things – still lots of room for you to improve it. Using leaflet.canvas is a bit complex as we would need to separate the traffic information into the tiles structure. Instead I’m using the big canvas solution from CartoDB making everything really simple.
Three further simple steps are necessary when extending the old example:
Fetching the data and parsing it is a simple procedure done within DataUpdater.fetch. Also surrounding the data feeding method with a write-lock is easy:
public void feed(RoadData data) { writeLock.lock(); try { lockedFeed(data); } finally { writeLock.unlock(); } }
And finally we need to surround every route request with a read-lock:
GraphHopper tmp = new GraphHopper() { @Override public GHResponse route(GHRequest request) { lock.readLock().lock(); try { return super.route(request); } finally { lock.readLock().unlock(); } } }.forServer().init(args);
Now we just need to fetch the road network for this area and start the server:
And you’ll see regularly updates of the speed where routing should be influenced, at least the duration for the route:
Speed change at 50.94432295851602, 7.057916495227443. Old: 30.0, new:5.0 Speed change at 50.944496505815735, 7.057842025768907. Old: 60.0, new:45.0 Speed change at 50.94422920435813, 6.982818132995898. Old: 65.0, new:45.0 Speed change at 50.96702284992454, 7.03188984955171. Old: 45.0, new:20.0 Speed change at 50.90650702086146, 7.0605028341376235. Old: 70.0, new:45.0 Updated 86 street elements. Errors:0
To start from scratch you can remove the graph-cache folder and the old speed values will be used. Still this is only a rough prototype. For example:
This is only one city, but we collect more here! Now have fun to test and tweak the system!