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.

{
    "status": "emitted",
    "booked_at": "2018-01-11T15:10:34+01:00",
    "emitted_at": "2018-01-12T19:08:49+01:00",
    "public_token": "…",
    "code": "…",
    "legs": [
      {
        "departure_date": "2018-01-26T19:07:00+01:00",
        "arrival_date": "2018-01-26T20:39:00+00:00",
        "departure": {
          "public_id": "4922",
          "latitude": 48.880185,
          "longitude": 2.355151,
          "time_zone": "Europe/Paris",
          "country": "FR",
          "slug": "paris-gare-du-nord",
          "name": "Paris Gare du Nord"
        },
        "arrival": {
          "public_id": "5892",
          "latitude": 51.531921,
          "longitude": -0.126361,
          "time_zone": "Europe/London",
          "country": "GB",
          "slug": "london-st-pancras",
          "name": "London St-Pancras"
        },
        "travel_class": "first",
        "carrier": "eurostar",
        "train": {
          "type": "e300",
          "number": "9055"
        },
A very small extract of my Trainline data export: the beginning of an old trip

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.

European map showing a line between London and Paris and the real train route.
London ❯ Paris: naive line vs. real train route

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.

Screenshot of raildar map, showing two train routes between Madrid and Moscow.
From Madrid to Moscow by train

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.

Animation of my trips on a European map changing over the years.
All my train trips for the previous seven years 😊

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.

Screenshot of trainmap.ntag.fr with four trips: Paris to Offenburg, Offenburg to Berlin, Berlin to Köln and Köln to Paris.
Add your trips and get a GeoJSON.

You can use it (+ kepler.gl) to create beautiful maps of your next trip if you move by 🚅.

Screenshot of Kepler.gl showing the GeoJSON created in the previous screenshot.
Don't forget to uncheck "Fill Color" on the left or you will get a very strange filled polygon.

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