If you have arrived here in search of an online grid reference to latitude/longitude
converter, you are probably out of luck. While you will find here a command line utility
for Unix-like systems, there is nothing here for the average user - no matter - go back to your
favourite search engine and you will find plenty.
If however you are a web developer seeking ways to use Ordnance Survey (Great Britain) grid
references in your sites, you are in the right place. You will find:
- Links to a perl library and command line utility to output latitude/longitude from a 6 figure grid reference.
- PHP functions to call the above perl script
- ModX snippets to display Google Maps (which require latitude and longitude as inputs).
No doubt users of other CMS's will be able to adapt these as required.
I will be assuming you have Perl and PHP both installed on your machine. These pages are intended for web
developers, and hence I am assuming you are using an up to date standards compliant browser...
The heart of the system I use for using OS grid references is from the
CPAN
archive of Perl modules.
At http://search.cpan.org/~pkent/Geography-NationalGrid-1.6/ you
will find the relevant modules. If you are unfamiliar with Perl you will need to know that Perl modules are arranged in directory
structures that mirror their names. Geography::NationalGrid therefore refers to a file called NationalGrid.pm (note the .pm extension) in
the directory Geography. Within Geography you also need a NationalGrid directory containing GB.pm and IE.pm.
To use the modules, use the file you can download here (save it as osgb.pl)
as a command line utility, and ensure that the Geography directory is in the same folder. The script accepts
six-figure grid references, with a preceeding letter pair to denote the relevant 100km grid square:
>perl osgb.pl nc123456
58.3589211877188 -5.20870388762637
>perl osgb.pl se123456 nc678567
53.9062116134846 -1.81275809170673
58.478799669197 -4.26736536540815
If like me you work in PHP,
then you will need a caller function to run the perl script. Download this and save it as caller.php.
A brief look at the code will see that whitespace is removed wherever it occours, and the grid reference
is trimmed to 6-figures. Note that 6, 8 or 10 figures are accepted as input, but the results will only be as
precise as a 6-figure grid reference. A regular expression checks that we then have two letters followed by six figures.
Note that the letters are not compulsory, and in this incarnation of the script ‘SE’ are
the default, for no other reason than they cover most of West Yorkshire, where I live at the time of writing.
The script then calls the perl using shell_exec.
Check that OSGBPATH will be set to the location of the perl script and if required edit the relevant line
near the start of caller.php
Two functions are provided to output Javascript to create Google Map objects - the first just creates a
location object, while the
second creates a marker
on an already existing map. This map is referenced using the Javascript variable map which must be already created using
new Gmap2 and centred using the
setCenter method. Note that two variables are set; marker and markerlocation. markerlocation is a Google Map
location object,
and can be used (for example) to pan the map to this location.
See the Google Map API for more info on what you
can do with Google Maps.
Errors result in null or empty strings being returned.
Download the following files:
-
ModX snippet to implement a variation of the aforementioned caller.php PHP script. Name it OSGB_SE.
It doesn't contain quite the same functions as caller.php, and the main function, OSGBgridref(), returns array(0,0) on failure. Whilst not a good
solution, particularly if you expect to occasionally receive incorrect grid references to process, it means that the javascript provided below
will not throw an error if the grid reference is invalid. In many cases this will not be a reasonable solution.
-
Javascript to create a map centred at the
location stored in template variable [*OSGridRef*]. Position the script in the <head>.
This code expects to find an element on the page with id="gmap". I suggest using
CSS
to give this element a fixed size.
It also includes a ditto call to create markers on this map according to the value of the same template variable in
all child documents. If you don't require such markers, just delete the call.
- Ditto template for the above ditto call.
-
This includes a commented modx document id, making debugging easier.
-
The bindInfoWindow call results in a window or ‘bubble’ being
associated with each marker;
these appear when the user hovers over the relevant marker on the map.
In this version the window
contains the [+pagetitle+] which is a link to the relevant page/document.
Don't forget to replace YOURKEYHERE in the Javascript with your own (freely obtainable)
Google maps key.
IE7 users - right click on these links and 'Save Target As...' - otherwise IE may try and parse
some of these files now. Open with Wordpad or any editor that can cope with Unix line breaks to view the source.