Main Content

Ordnance Survey Grid Reference to Latitude / Longitude

Archive - Originally posted on "The Horse's Mouth" - 2007-10-14 07:39:28 - Graham Ellis

I've already written about conversion of postal addresses to latitude and longitude using Google's interface - but what is I want to convert the Ordinance Survey's grid references into latitude and longitude? Google's interface doesn't support the OS units - and in any case it's "just" a a calculation. I have put quotes around the word "just" because it's actually a complex calculation that I had no desire to re-code from learned papers.

Of course ... it's been done already. Perl's Geography::NationalGrid module which you can download from the CPAN. Now I wanted to use that in a PHP page to demonstrate to you, so I've written a quick Perl command line program that my PHP calls up when it needs to and .... bob's your uncle.

Try it out here (examples ST906637 is in Melksham ... SJ347904 is in Liverpool) :
Enter a grid reference:


The source code is also available for you:
The Perl command line program
The PHP which calls up that Perl

External links -
Descritpion of coordinate system and conversion algorithms (.pdf)
Perl Cpan module

Update = August 2008 I've also added code for the OPPOSITE mapping - Latitude and Longitude to OS Grid reference.

• Demonstration - here
• Source code of PHP page - here
• Source code of Perl program - here