Optimizing bicycling routes with OpenStreetMap and APIs
(There’s a video version of this article available here, where we also do a deep dive into the code behind it, remember to turn on subtitles):
I’ve started playing with data from OpenStreetMap (OSM). It started with me trying to fetch all the places where I could get free water when moving around Copenhagen, which turned out to be a daunting task, because OSM seems to have a lot of different ways to categorise available water. I’ve identified the following tags to look out for:
Contributing back to OpenStreetMap #
I soon became interested in contributing back, so I found the Android application called “StreetComplete” that makes it easy to contribute to OpenStreetMap. The app will give you different challenges or missions to complete that will provide more data for OpenStreetMap.
Some call this “data-exercising” where you go out and get exercise and gather data to OpenStreetMap.
I follow a person called Søren on Twitter, with the handle of @neogeografen, and he has been speaking about mapping the surfaces of bicycle paths in Denmark. He shared a map to look up missing surfaces nearby. This is how looks for me in Copenhagen:
Biking around and mapping different surfaces #
I wanted to map these as efficiently as possible. I have a bike so the idea was to create the most optimal path between the nodes that are missing data. Luckily it is easy to export the data from Overpass:
I found an Optimization API from OpenRouteService that can calculate all sorts of combinations. If you have lots of stops and different drivers with different start and end points it can find the most optimal routes; luckily I don’t need any of that. They also have very generous pricing (free).
Building the script #
So the idea was to build a script that:
- Takes in a GeoJSON file with all the nodes.
- Maybe filters the nodes based on a subset.
- Formats the nodes in a way that OpenRouteService understands.
- Sends the request off to OpenRouteService.
- Formats the result from OpenRouteService in an easy and digestible format, maybe with Google Maps links to routes so it’s easy to travel and find the spots.
The Optimization API has a limit of 50 stops, and Google Maps routing seems to have a limit of 10-15 stops (depending on different sources).
Here’s the HTML output my scripts generates:
And when I click the Google Maps route link this is what I see:
The code is available here: https://github.com/kaspergrubbe/openroute-routetest