A gtfs server in 5 minutes

Want to be able to serve the schedules of any railway / bus company ?

Some providers already exist (starting by google of course) and allow you to do so… with a pricing table and a very low free tier quota.
There are two ways to get rid of this limitation : buy the paid plan (which often comes with pricing by use) or build your own service.

How to make a server from gtfs data ? quite straightforward : read the zip, parse the csv files and normalize it in javascript data (index it with relevant ids).
Then, store that data as a global var and serve it

For the curious folks, here lies the substance : https://gist.github.com/libetl/eedb72b27de3b888c001d0ab14eb3c66#file-readgtfsdumps-js
It also indexes every station by latitude longitude so that a mobile device can recover the name of the nearest station thanks to its gps coordinates.

After that : when a request comes from an express route, extract some data out of it and output it.
How to transform that data into schedules ? Here is one way to do that : https://gist.github.com/libetl/eedb72b27de3b888c001d0ab14eb3c66#file-timetable-js
No find, only one filter operation in a small dataset : you guessed it, the operation will be really fast.

I have tried myself for a gtfs object that contains 3 different gtfs dumps in France.
It never takes more that 40 ms to achieve.

You can see my working example on a free server at heroku : https://train-schedules-server.herokuapp.com

Also checkout the source git if you need to fork it : https://github.com/libetl/train-schedules-server
(the dumps list is in the sources.js file)

Leave a Reply

Your email address will not be published. Required fields are marked *