Friday, April 15, 2016

Visualizing claims from BIG cycling using python and folium

BIG cycling is a list of the 1000 cycling summits that matter in Europe. Over the past few years I rode a fair amount of them, mostly in Switzerland. There are a total of 75 climbs in region 7 (Switzerland and Liechtenstein), of which I already climbed 34, and therefore there are 41 left. I had the idea of riding the remaining ones this year.

To aid planning, I wanted to have a map with all the summits colored by claim. The site features this, though I do not like that it collapses different cols under one symbol. Furthermore, I also wanted to plot the routes in which I made the claim as-well as the routes I planned to claim them. To create this map, I first needed three data sets:
  1. My planned rides
  2. The rides in which I made a claim
  3. The latitude/longitude of the summits
I plan my rides using either strava or the Quaeldich Tourenplaner. Both have a gpx export which I use for the planned rides. The rides in which I made the claims are mostly stored in strava and therefore I made a full export of them in gpx format. To obtain the latitude/longitude of all the summits we use a small scraper in python leveraging beautifulsoup4 and store them in a text file (to avoid scraping the site over and over again, the list does not change frequently and I need to scrape almost 1000 pages).

To visualize the locations and the rides I started of using pygmaps, than switched to the pygmaps-extended fork because that could also give the locations a color (green for claimed ones, red for unclaimed ones). The maps worked wonderfully, but only on a specific computer and a specific browser, so I decided to try folium. This worked wonderfully and also provided a fantastic visualization mode, as we will see later.

Creating the visualization was then really not all that difficult anymore.

  • We first read the list we created using the scraper and scrape my own claims (this is just a single page so no real need to store this). 
  • We then loop over all files in the sub directory 'planned' to read the gpx files for all planned rides. We use xml.etree that comes with python to extract the latitude/longitude data from these files.
  • Then we loop over all files in the sub directory 'rides'. Levering the power of numpy we determine the distance of all the locations in the track to all the summits. This is a fair amount of calculation (1000 summits times roughly 3000 track points per ride times 500 rides leads to roughly 1.5 billion distance calculations, but this still takes no more than a few minutes). During this loop we keep track of the rides containing summits and the summits that we passed.
  • The only thing we then have to do is make a map object using folium, add the summits (red for unclaimed, green for claimed, blue for forgot to claim) and the rides (using a polyline, red for planned, green for ridden) and write result as html file to disk.
We use a black and white scheme called Stamen Toner, which makes for a really clear but also artistic map. You can find the results (18MB) here.


No comments:

Post a Comment