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 data.southampton.ac.uk — 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.

#!/usr/bin/php
<?php

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

$sparqlh = sparql_connect( "http://data-dev.ecs.soton.ac.uk:8002/sparql/" );
$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 <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
 ?thing <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?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"]);
 $ll2w->WGS84ToOSGB36();
 $os2w = $ll2w->toOSRef();

 print "<".$row["thing"]."> <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/easting> "";
 print round($os2w->easting).""^^<http://www.w3.org/2001/XMLSchema#integer> .\n";
 print "<".$row["thing"]."> <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/northing> "";
 print round($os2w->northing).""^^<http://www.w3.org/2001/XMLSchema#integer> .\n";
}


**UPDATE**

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;

SELECT DISTINCT * WHERE { 
 ?dataset <http://rdfs.org/ns/void#dataDump> ?graph . 
 ?dataset <http://purl.org/dc/terms/publisher> <http://id.southampton.ac.uk/> .
 ?dataset a <http://purl.org/openorg/AuthoritativeDataset> .
 GRAPH ?graph {
   ?thing <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
   ?thing <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?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.