Visualize (your) train trips
I like to travel by train. It's ecological, relaxing, and you can discover beautiful and unreachable places. I book all my tickets on Trainline Europe (because the product is incredibly well designed — I also worked 💻 here for a year), and if you ask its client support, you can get a copy of your personal data as JSON.
My idea was to create a map of all my trips. But if you just draw lines between departure and arrival: it's not representative.
Hopefully, OSRM can be used to find the shortest route for a train between two points on earth, thanks to a special profile. You can experience it in live thanks to Raildar.
I tried to install OSRM on my server to be able to compute those routes, but it seems that 16GB of memory isn't enough to launch the server 😔. So I ended up using Raildar endpoint to compute train routes. If there is a solution to install OSRM with all Europe data on a Debian server with 16GB of RAM only, I am listening to you!
The API
Because Raildar endpoint isn't very handy to use (it returns a special encoded polygon), I created a wrapper around it: a simple API you can call to get an array of coordinates corresponding to the shortest train route between two points: https://trainmap.ntag.fr/api/route?dep=48,2&arr=52,13&simplify=1
You can pass coordinates (latitude,longitude
) or Trainline stations IDs.
And it's open source.
Then it was very simple to write a script to call this API for each leg of my Trainline data, and to create a GeoJSON file. Finally, I could import this file into Kepler.gl (I love this tool) to vizualise the trips. That's it! Bonus, you can easily add a date filter.
Planning your next train trip
On top of this API, I created an interface allowing you to get a GeoJSON combining multiple train routes. It's available at https://trainmap.ntag.fr.
You can use it (+ kepler.gl) to create beautiful maps of your next trip if you move by 🚅.
Limits and what's next
The API only returns one route: the main/shortest one. But it may not be the right one. For example if you ask for Paris ❯ Toulouse, you will get a route going through Bordeaux, which is valid if you travel by TGV, but is incorrect if you have a night train.
I would also prefer to have my own instance of OSRM instead of consuming Raildar API, which may not have been designed for that.
Finally, it's really fun to play with Trainline data: here I just show one example of data exploration, but you can also compute all sorts of stats (the total distance you traveled, the total duration you spent in a train, the total amount of money you gave to Trainline, the number of trips, the longest one, the shortest one, your busiest month…). Ask for your data and play with it 🎉.
Trainmap tool: https://trainmap.ntag.fr
The source: https://github.com/NTag/trainmap