Skip to content

Mapping Lat/Long in our SPARQL endpoint into UK Ordnance Survey Easting and Northing

This is a command-line php script which creates UK Easting & Northing values from the lat long values. I plan to run it once a day on — I know that the big local taxi firm uses Easting and Northing in their database so it might be interesting to see if I can generate a report for them which benefits our members by giving the taxi services better data.

The libraries it uses are my sparqllib (LGPL) and phpcoord-2.3 (GPL)

You’ll need the latest github version of sparqllib, if you want to  include the “cgiParams” line, which tells 4store to give all the results — we’ve got more than 2000 lat/long pairs, and by default we’ve set 4store to set a limit of 2000 results on queries.

As this script uses a GPL library (not LGPL), I guess that makes it GPL, although it’s just a slip of script, I’m sure other UK open data services might want to consider something similar.


require_once( "/var/wwwsites/tools/phpcoord/phpcoord-2.3.php" );
require_once( "/var/wwwsites/tools/PHP-SPARQL-Lib/sparqllib.php" );

$sparqlh = sparql_connect( "" );
$sparqlh->cgiParams( "soft-limit=-1" );

# Limit lat and long to the same graph to try to minimise the
# number of 'multiplying out' we get with multiple lat/long
$result = $sparqlh->query( "SELECT DISTINCT * WHERE {
 GRAPH ?g {
 ?thing <> ?lat .
 ?thing <> ?long .
}" );
if( !$result ) { print $db->errno() . ": " . $db->error(). "\n"; exit; }

$fields = $result->field_array( $result );

while( $row = $result->fetch_array() )
 $ll2w = new LatLng($row["lat"],$row["long"]);
 $os2w = $ll2w->toOSRef();

 print "<".$row["thing"]."> <> "";
 print round($os2w->easting).""^^<> .\n";
 print "<".$row["thing"]."> <> "";
 print round($os2w->northing).""^^<> .\n";


It’s been pointed out to me by Yang Yang (a PhD researcher in WAIS) that it’s a bit of an antipattern to publish data derived from other people’s datasets, and there’s no real value to me in providing Easting Northing data for bus-stops and a few places in wikipedia we import. I’ve made a rather elegant solution;

 ?dataset <> ?graph . 
 ?dataset <> <> .
 ?dataset a <> .
 GRAPH ?graph {
   ?thing <> ?lat .
   ?thing <> ?long .

This now only finds lat/long pairs in graphs which are listed as published by the University of Southampton AND Authoritative. That works pretty well.

Posted in 4store, Geo, PHP, SPARQL.

Tagged with .

One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Continuing the Discussion

  1. Easting & Northing – Southampton Data Blog linked to this post on June 12, 2012

    […] the post on the Webteam Blog for the nitty gritty about how this […]

Some HTML is OK

or, reply to this post via trackback.