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) :
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
Some other articles
Q110 - Programming Algorithms Some gems from an introduction to Python Identifying the first and last records in a sequence Testing new algorithms in PHP A good example of recursion - a real use in Python Finding sum, minimum, maximum and average in Python (and Ruby) Selecting RECENT and POPULAR news and trends for your web site users Learning to program - what are algorithms and design patterns? Finding all the unique lines in a file, using Python or Perl Finding the total, average, minimum and maximum in a program Why would you want to use a Perl hash? AND and OR operators - what is the difference between logical and bitwise varieties? How many toilet rolls - hotel inventory and useage Finding elements common to many lists / arrays Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl Arrays v Lists - what is the difference, why use one or the other Lots of way of converting 3 letter month abbreviations to numbers Sorting people by their names Comparing floating point numbers - a word of caution and a solution And and Or illustrated by locks A life lesson from the accuracy of numbers in Excel and Lua Grouping rows for a summary report - MySQL and PHP Matching disparate referencing systems (MediaWiki, PHP, also Tcl) Nuclear Physics comes to our web site Validating Credit Card Numbers This article Updating a page strictly every minute (PHP, Perl) Speed Networking - a great evening and how we arranged it How similar are two words Bellringing and Programming and Objects and Perl Searching for numbers P219 - Libraries and Resources What do I mean when I add things in Perl? The week before Christmas Expect in Perl - a short explanation and a practical example Syncronise - software, trains, and buses. Please! Operator overloading - redefining addition and other Perl tricks Loading external code into Perl from a nonstandard directory Do not re-invent the wheel - use a Perl module Debugging and Data::Dumper in Perl About dieing and exiting in Perl Using English can slow you right down! This article Outputting numbers as words - MySQL with Perl or PHP Judging the quality of contributed Perl code Self help in Perl Coloured text in a terminal from Perl Why reinvent the wheel Use standard Perl modules Where do Perl modules load from Avoid the wheel being re-invented by using Perl modules Talk review - Idiomatic Perl, David Cross H999 - Additional PHP Material Image indexer / thumbnail display scripts in PHP Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON Arrays of arrays - or 2D arrays. How to program tables. Exception handling in PHP If nothing, make it nothing. Extra PHP Examples PHP Techniques - a workshop Flipping images on your web page Script to present commonly used images - PHP Copyright and theft of images, bandwidth and members. More PHP sample and demonstration programs This article Converting from postal address to latitude / longitude Controlling and labelling Google maps via PHP PHP Standalone - keyboard to screen Drawing hands on a clock face - PHP Drawing dynamic graphs in PHP Sorting people by name in PHP Parallel processing in PHP Dates, times, clickable diarys in PHP Display an image from a MySQL database in a web page via PHP Syntax checking in PHP Reporting on the 10 largest files or 10 top scores PHP - a team member leaves Check your user is human. Have him retype a word in a graphic Hot answers in PHP Presentation, Business and Persistence layers in Perl and PHP PHP Image viewing application PHP - setting sort order with an associative array Merging pictures using PHP and GD Running a Perl script within a PHP page Double Dollars in PHP Stand alone PHP programs Time calculation in PHP the array returned by preg_match_all More maps Ordnance Survey - using a 'Get a map' What and why for the epoch PHP and natural sorting H309 - Maps, Graphics and Geographics Image indexer / thumbnail display scripts in PHP Adding a PHP build option, rotating an image based on camera data, and a new look at thumbnails in PHP The changing face of Christmas Where are you? How to write a geosensitive application Fpdf - generating .pdf documents easily from your PHP program QR codes with marketing logos embedded QR codes - graphics images that provide quick phone links UK Mapping Data - and more to come - under government Open Data measures Needle in a haystack - finding the web server overload Computer Graphics in PHP - World (incoming data) to Pixel (screen) conversion Finding and diverting image requests from rogue domains An image from a website that occasionally comes out as hyroglyphics Server logs - drawing a graph of gathered data Protecting your images from use out of context Uploading a document or image to its own URL via a browser Redirecting to your main domain for correct security keys Reducing image size on digital photos - PHP Dynamic / changing images on your web page Geocoding - converting address to latitude / Longitude with PHP via Google World Flags in your PHP pages Images for Christmas Making it all worthwhile Ever had One of THOSE mornings? Dynamic maps / geographics in PHP All around the world? addslashes v mysql_real_escape_string in PHP Gant charts - drawing them with a PHP script This article Converting from postal address to latitude / longitude Controlling and labelling Google maps via PHP Drawing hands on a clock face - PHP Drawing dynamic graphs in PHP Display an image from a MySQL database in a web page via PHP PHP Image viewing application Merging pictures using PHP and GD Ordnance Survey - using a 'Get a map' H115 - Designing PHP-Based Solutions: Best Practice Real life PHP application using our course training MVC example Using an MVC structure - even without a formal framework Learning to program - comments, documentation and test code We not only teach PHP and Python - we teach good PHP and Python Practice! Even early on, separate out your program from your HTML! Filtering PHP form inputs - three ways, but which should you use? PHP sessions - a best practice teaching example Injection Attacks - PHP, SQL, HTML, Javascript - and how to neutralise them Separating program and artwork in PHP - easier maintainance, and better for the user How to build a test harness into your PHP Not just a PHP program - a good web application Adding a newsfeed for your users to a multipage PHP application Improving the structure of your early PHP programs Refactoring - a PHP demo becomes a production page Defensive coding techniques in PHP? PHP Techniques - a workshop Short and sweet and sticky - PHP form input Software to record day to day events and keep an action list Efficient PHP applications - framework and example A story about benchmarking PHP This article Converting from postal address to latitude / longitude Controlling and labelling Google maps via PHP Using a MySQL database to control mod_rewrite via PHP Easy handling of errors in PHP Resetting session based tests in PHP Drawing hands on a clock face - PHP Painting a masterpiece in PHP Good Programming practise - where to initialise variables Back button - ensuring order are not submitted twice (PHP) Learning to write secure, maintainable PHP Maintainable code - some positive advice Code quality counts Global, Superglobal, Session variables - scope and persistance in PHP PHP - good coding practise and sticky radio buttons Reporting on the 10 largest files or 10 top scores Giving the researcher power over database analysis Merging pictures using PHP and GD Robust checking of data entered by users A year on - should we offer certified PHP courses Code and code maintainance efficiency Putting a form online Crossfertilisation, PHP to Python Short underground journeys and a PHP book