{"id":869,"date":"2012-06-11T12:27:56","date_gmt":"2012-06-11T12:27:56","guid":{"rendered":"http:\/\/blog.soton.ac.uk\/webteam\/?p=869"},"modified":"2012-06-12T15:27:57","modified_gmt":"2012-06-12T15:27:57","slug":"mapping-latlong-in-our-sparql-endpoint-into-uk-ordnance-survey-easting-and-northing","status":"publish","type":"post","link":"https:\/\/blog.soton.ac.uk\/webteam\/2012\/06\/11\/mapping-latlong-in-our-sparql-endpoint-into-uk-ordnance-survey-easting-and-northing\/","title":{"rendered":"Mapping Lat\/Long in our SPARQL endpoint into UK Ordnance Survey Easting and Northing"},"content":{"rendered":"<p>\ufeffThis is a command-line php script which creates UK Easting &amp; Northing values from the lat long values. I plan to run it once a day on <a href=\"http:\/\/data.southampton.ac.uk\/\">data.southampton.ac.uk<\/a> &#8212; 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.<\/p>\n<p>The libraries it uses are my <a href=\"http:\/\/graphite.ecs.soton.ac.uk\/sparqllib\/\">sparqllib<\/a> (LGPL) and <a href=\"http:\/\/www.jstott.me.uk\/phpcoord\/\">phpcoord-2.3<\/a> (GPL)<\/p>\n<p>You&#8217;ll need the latest <a href=\"https:\/\/github.com\/cgutteridge\/PHP-SPARQL-Lib\">github version<\/a> of sparqllib, if you want to\u00a0 include the &#8220;cgiParams&#8221; line, which tells 4store to give all the results &#8212; we&#8217;ve got more than 2000 lat\/long pairs, and by default we&#8217;ve set 4store to set a limit of 2000 results on queries.<\/p>\n<p>As this script uses a GPL library (not LGPL), I guess that makes it GPL, although it&#8217;s just a slip of script, I&#8217;m sure other UK open data services might want to consider something similar.<\/p>\n<pre>\ufeff\ufeff#!\/usr\/bin\/php\r\n&lt;?php\r\n\r\nrequire_once( \"\/var\/wwwsites\/tools\/phpcoord\/phpcoord-2.3.php\" );\r\nrequire_once( \"\/var\/wwwsites\/tools\/PHP-SPARQL-Lib\/sparqllib.php\" );\r\n\r\n$sparqlh = sparql_connect( \"http:\/\/data-dev.ecs.soton.ac.uk:8002\/sparql\/\" );\r\n$sparqlh-&gt;cgiParams( \"soft-limit=-1\" );\r\n\r\n# Limit lat and long to the same graph to try to minimise the\r\n# number of 'multiplying out' we get with multiple lat\/long\r\n$result = $sparqlh-&gt;query( \"SELECT DISTINCT * WHERE {\r\n GRAPH ?g {\r\n ?thing &lt;http:\/\/www.w3.org\/2003\/01\/geo\/wgs84_pos#lat&gt; ?lat .\r\n ?thing &lt;http:\/\/www.w3.org\/2003\/01\/geo\/wgs84_pos#long&gt; ?long .\r\n }\r\n}\" );\r\nif( !$result ) { print $db-&gt;errno() . \": \" . $db-&gt;error(). \"\\n\"; exit; }\r\n\r\n$fields = $result-&gt;field_array( $result );\r\n\r\nwhile( $row = $result-&gt;fetch_array() )\r\n{\r\n $ll2w = new LatLng($row[\"lat\"],$row[\"long\"]);\r\n $ll2w-&gt;WGS84ToOSGB36();\r\n $os2w = $ll2w-&gt;toOSRef();\r\n\r\n print \"&lt;\".$row[\"thing\"].\"&gt; &lt;http:\/\/data.ordnancesurvey.co.uk\/ontology\/spatialrelations\/easting&gt; \"\";\r\n print round($os2w-&gt;easting).\"\"^^&lt;http:\/\/www.w3.org\/2001\/XMLSchema#integer&gt; .\\n\";\r\n print \"&lt;\".$row[\"thing\"].\"&gt; &lt;http:\/\/data.ordnancesurvey.co.uk\/ontology\/spatialrelations\/northing&gt; \"\";\r\n print round($os2w-&gt;northing).\"\"^^&lt;http:\/\/www.w3.org\/2001\/XMLSchema#integer&gt; .\\n\";\r\n}\r\n\r\n\r\n<\/pre>\n<h2>**UPDATE**<\/h2>\n<p>It&#8217;s been pointed out to me by Yang Yang (a PhD researcher in WAIS) that it&#8217;s a bit of an antipattern to publish data derived from other people&#8217;s datasets, and there&#8217;s no real value to me in providing Easting Northing data for bus-stops and a few places in wikipedia we import. I&#8217;ve made a rather elegant solution;<\/p>\n<pre>SELECT DISTINCT * WHERE { \r\n ?dataset &lt;http:\/\/rdfs.org\/ns\/void#dataDump&gt; ?graph . \r\n ?dataset &lt;http:\/\/purl.org\/dc\/terms\/publisher&gt; &lt;http:\/\/id.southampton.ac.uk\/&gt; .\r\n ?dataset a &lt;http:\/\/purl.org\/openorg\/AuthoritativeDataset&gt; .\r\n GRAPH ?graph {\r\n   ?thing &lt;http:\/\/www.w3.org\/2003\/01\/geo\/wgs84_pos#lat&gt; ?lat .\r\n   ?thing &lt;http:\/\/www.w3.org\/2003\/01\/geo\/wgs84_pos#long&gt; ?long .\r\n }\r\n}\r\n<\/pre>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ufeffThis is a command-line php script which creates UK Easting &amp; Northing values from the lat long values. I plan to run it once a day on data.southampton.ac.uk &#8212; 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 [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[4226,352,4224,138,411],"tags":[],"class_list":["post-869","post","type-post","status-publish","format-standard","hentry","category-4store","category-data","category-geo-2","category-php","category-sparql"],"_links":{"self":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts\/869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/comments?post=869"}],"version-history":[{"count":3,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts\/869\/revisions"}],"predecessor-version":[{"id":873,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts\/869\/revisions\/873"}],"wp:attachment":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/media?parent=869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/categories?post=869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/tags?post=869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}