· software-development

London Bus Stops API: Mapping northing/easting values to lat/long

I started playing around with the TFL Bus stop location and routes API and one of the annoying things about the data is that it uses easting/northing values to describe the location of bus stops rather than lat/longs.

The first few lines of the CSV file look like this:

1000,91532,490000266G,WESTMINSTER STN <> / PARLIAMENT SQUARE,530171,179738,177,0K08,0
10001,72689,490013793E,TREVOR CLOSE,515781,174783,78,NB16,0
10002,48461,490000108F,HIGHBURY CORNER,531614,184603,5,C902,0

For each of the stops I wanted to convert from the easting/northing value to the equivalent lat/long value but I couldn’t find a simple way of doing it in code although I did come across an API that would do it for me.

I wrote the following script to save a new CSV file with all the London bus stops and their lat/long location:

require 'rubygems'
require 'csv'
require 'open-uri'
require 'json'

data_dir = File.expand_path('data') + '/'
file_name = data_dir + "stops.csv"

stops = CSV.read(file_name).drop(1)

out = CSV.open(data_dir + "stops_with_lat_longs.csv","w")

stops.each do |stop|
  code = stop[1]
  easting = stop[4]
  northing = stop[5]
  url  = "http://www.uk-postcodes.com/eastingnorthing.php?easting=#{easting}&northing=#{northing}"
  location = JSON.parse(open(url).read)

  puts "Processing #{stop[3]}: #{location['lat']}, #{location['lng']}"

  out << [code, location['lat'],location['lng']]
end

out.close

I’ve uploaded the file with mapping from bus stop code to lat/long to github as well.

Peter Hicks has a blog post showing another way of doing this using just Ruby code but I couldn’t get the 'proj4' gem to install and I didn’t fancy shaving that yak when I had another solution which worked.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket