Converting OSGB36 (Eastings/Northings) to WGS84 (Longitude/Latitude) in Ruby
The excellent people at the Greater London Assembly have released a list of bus stops and bus routes in London. The coordinates of each bus stop are in eastings and northings, and I wanted to convert these to longitude and latitude for my Ruby on Rails application.
Using the proj4rb gem and some projection definitions from spatialreference.org – with some help from Harry Wood’s blog, I came up with the following code:
#!/usr/bin/ruby
require 'rubygems'
gem 'proj4rb'
require 'proj4'
easting = 529978
northing = 186491
srcPoint = Proj4::Point.new(easting, northing)
srcProj = Proj4::Projection.new('+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs')
dstProj = Proj4::Projection.new('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
dstPoint = srcProj.transform(dstProj, srcPoint)
print "lat=#{dstPoint.lat * (180 / Math::PI)}\n"
print "lon=#{dstPoint.lon * (180 / Math::PI)}\n"
To convert WGS84 to OSGB36:
#!/usr/bin/ruby
require 'rubygems'
gem 'proj4rb'
require 'proj4'
latitude = 51.5623279577278
longitude = -0.126277004538848
srcPoint = Proj4::Point.new(longitude * (Math::PI / 180), latitude * (Math::PI / 180))
srcProj = Proj4::Projection.new('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
dstProj = Proj4::Projection.new('+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs')
dstPoint = srcProj.transform(dstProj, srcPoint)
print "easting=#{dstPoint.x}\n"
print "northing=#{dstPoint.y}\n"
[...] Converting OSGB36 (Eastings/Northings) to WGS84 (Longitude/Latitude) in Ruby « Peter Hicks… That's a lot more succinct than my version. (tags: geo ruby wgs84 osgb36 conversion ) [...]
[...] Converting OSGB36 (Eastings/Northings) to WGS84 (Longitude/Latitude) in Ruby « Peter Hicks' Bl… "The excellent people at the Greater London Assembly have released a list of bus stops and bus routes in London. The coordinates of each bus stop are in eastings and northings, and I wanted to convert these to longitude and latitude for my Ruby on Rails application. [...]
Thanks for this Peter, I think this may be exactly what I am looking for. I have a series of grid references with letters, of the form:
SS9146
Will your code convert this to WGS84?
Mine won’t, but I believe it’s trivial to convert SS9146 to eastings and northings, which the code here will then handle.
FieldenMaps.info can do the conversion, and it looks like there’s a direct mapping from the square (SS) to an eastings/northings offset. I think the Ordnance Survey website may be a good source of information.
Don’t suppose you’re making an api for this? I can’t install proj4 on my dreamhost server (requires proj-devel which I don’t think I can install) and am trying desperately to translate the os bus locations into lat lon.
What I’d do in that case is translate them offline on a server where you can run proj4, then upload the data there. It’s less optimal, but at least you won’t be tied in to sending API calls over the Internet to another host all the time.