API Development Transportation & Logistics

Public transport APIs: Singapore’s smart city example

Public transport APIs

Did you know that the International Space Station (ISS) has an API? You can try it in the browser to get the current location or to find out when exactly it’s flying over Singapore next time. For all the non-geeks in the audience: congratulations, if you clicked on these links you can now say you’ve triggered a call for public transport APIs, and you might even be able to make sense of the data snippets you see in the browser. The ISS API is just one of many examples, an increasing number of companies and institutions are making some of their data available for everyone to consume and build mashups.

User case of public transport APIs

Let’s pick a user case that’s a little more down to earth: I live in Singapore and usually take the bus to the office. There are no free seats, sometimes not even a chance to get on the bus at all because it’s packed with people standing all the way to the front. I recently discovered public transport APIs from Singapore’s Land Transport Authority (LTA) that provides access to data via its DataMall offering, with just a little coding we can visualize busy buses on Google Maps and analyze the peaks of a given day. We can transform something that looks just like the data snippets from the ISS example above into something like this:

0.jpg
API mapping

We now have an interactive Google Map with all bus stops (within a five km radius from Raffles Place) that shows buses with standing-only capacity or the ones that are completely full. You can navigate through individual snapshots in five-minute intervals with data from Friday, January 5, 2018 here. And we can compute a graph that illustrates how there are more busy buses during peak hours—probably not a surprise:

graph

graphHow did we get there? Let’s take a step back and look at the bigger picture for just another moment. Within a very short period of time, I was able to get access to public transport API data, understand the data format and build a prototype for a program that might be able to improve service availability and customer experience for an increasing number of people who use buses and trains daily!

Allowing third-party developers to innovate and create apps is one of the motivations for countries like Singapore for making data like this available via Open Data APIs as part of “Smart City” initiatives. Public Transport APIs is a great example and often a starting point for cities that are experimenting with Open Data. In today’s transport systems, real-time data such as current locations are already available, with a modern API Management solution it doesn’t take a lot of effort to make it easily consumable for developers inside and outside the organization and build apps just like I did.

In the example I showed above, you’ll find all code in my Github repository, but let’s go through the steps here. After signing up at LTA’s DataMall you’ll receive an API Key which allows you to make API requests such as the following, where we’re looking to get a list of all bus stops:

graphs2.jpg
datamall transport

When going through the specs for the bus system I was excited to find out that besides bus stops, services, arrival times, etc. there was a data field called “Load” that tells you the current capacity of a bus. All we have to do is find a way to query the system frequently for all bus services to get the insight we’re after. My choice of programming language for working with data is Python, so I’ve used the GeoPy library for identifying all bus stops that are within a five km radius of Raffles Place and PyCurl for making HTTP requests to query the Public transport APIs. The scripts produce data files with bus stop codes, locations and finally the current loads of the buses. I’m using Shell-Script wrappers to manipulate files and automate the process.

To create interactive Google Maps, we need JavaScript for adding the custom markers with icons and text and let’s say we’re not interested in the green buses. To automate this, I built this Python program that generates the JavaSript code for concatenation via Shell-Scripts:
The classic PyLab was an easy choice for plotting graphs, I’ve yet to figure out how Seaborn works, the examples look stunning!

Needless to say, with the available data you can now do all sorts of analysis, compare weekdays to weekends, and even trending and predictive analytics.

What’s next? Getting a taxi when it’s raining is a challenge—the number of available taxis is yet another public transport API, all you’d have to do is put this in context with the current rainfall readings to prove this point. Why don’t you give it a try?

Discover AMPLIFY API Management solution by Axway here.