Python and R for machine-learning, optimisations & visualisations

15 mins Public Transport of World Cities

Recently, I enjoyed playing around with mapnificent to see their definition of areas one can reach with public transport in a given time. However, I was wanted to (1) expand this to include more cities and (2) create an illustration where one can compare the differences easily.

Public Transport / Drive (15 minutes) of World Cities

I will briefly go through the method used.

Step 1 (Generating the Data):

I used the code I described in an earlier post to generate a 15-minute drive-time and public-transport isochrone using 2PM (local-time) for around 30 cities (the centre point was chosen automatically the Google API as I simply passed the city name to the geocoder).

I thought they looked pretty similar – bear in mind that they use a different data-source and public transport varies by time of day / day of the week, etc.

And Barcelona:

Ooops! One of the nice things about the Geocoding/Distance API is that if a point is not available it selects the closest available point. This means that isochrones are smoother and look less jerky (in general), however this doesn’t quite work for the ocean! I had two options:

A – Geocode each co-ordinate and then replace the initial co-ordinate with the geocoded one (this would move all the sea points to land), however this would also eliminate the useful ‘smoothing’ feature

B – Find some water shape-files and drop the points not in land using a point-in-polygon test

Step 2 (Cleaning the Data):

I found some water here and here (actually lakes). I then wrapped shapely’s “shape.contains(point)” function and used that to kill the points in the water. I also took advantage of the re-run (on the .csv of points output in the previous step) to tweak the map how I wanted (constant zoom to allow comparison, I wanted one version in black to emphasise the shape and another in colour, some useful statistics, etc).

Ideally one would use a spatial-index (like “r-tree”) for the point in polygon test but I was in a slight rush and it didn’t take that long anyway (when I get some more time I will revisit this).

Step 3 (Creating the Final Output:

Obviously when you have around 25 HTML files you don’t want to manually screen-shot and photo-shop each one so I put together this script, which would automatically open each HTML, take a screenshot, then piece the screenshots together.

Finally, I wanted to emphasize the shape and have the viewer focus on the isochrones and hence the simple style. If you prefer having everything included check out the below (I think my favourite version would involve adding a white city boundary to each one of the black maps)