Archive

Posts Tagged ‘linked data’

On Beyond OWL: challenges for ontologies on the Web by James Hendler

October 11, 2015 Leave a comment

Quick Play with Cayley Graph DB and Ordnance Survey Linked Data

June 29, 2014 2 comments

Earlier this month Google announced the release of the open source graph database/triplestore Cayley. This weekend I thought I would have a quick look at it, and try some simple queries using the Ordnance Survey Linked Data.

Cayley is written in Go, so first I had to download and install that. I then downloaded Cayley from here. As an initial experiment I decided to use the Boundary Line Linked Data, and you can grabbed the data as n-triples here. I only wanted a subset of this data – I didn’t need all of the triplestores storing the complex boundary geometries for my initial test so I discarded the files of the form *-geom.nt and the files of the form county.nt, dbu.nt etc. (these are the ones with the boundaries in). Finally I put the remainder of the data into one file so it was ready to load into Cayley.

It is very easy to load data into Cayley – see the getting started section part on the Cayley pages here. I decided I wanted to try the web interface so loading the data (in a file called all.nt) was a simple case of typing:

./cayley http –dbpath=./boundaryline/all.nt

Once you’ve done this point your web browser to http://localhost:64210/ and you should see something like:

Screen Shot 2014-06-29 at 10.43.35

 

One of the things that will first strike people used to using RDF/triplestores is that Cayley does not have a SPARQL interface, and instead uses a query language based on Gremlin. I am new to Gremlin, but seems it has already been used to explore linked data – see blog from Dan Brickley from a few years ago.

The main purpose of this blog post is to give a few simple examples of queries you can perform on the Ordnance Survey data in Cayley. If you have Cayley running then you can find the query language documented here.

At the simplest level the query language seems to be an easy way to traverse the graph by starting at a node/vertex and following incoming or outgoing links. So to find All the regions that touch Southampton it is a simple case of starting at the Southampton node, following a touches outbound link and returning the results:

g.V(“http://data.ordnancesurvey.co.uk/id/7000000000037256“).Out(“http://data.ordnancesurvey.co.uk/ontology/spatialrelations/touches“).All()

Giving:

Screen Shot 2014-06-29 at 10.56.15

If you want to return the names and not the IDs:

g.V(“http://data.ordnancesurvey.co.uk/id/7000000000037256“).Out(“http://data.ordnancesurvey.co.uk/ontology/spatialrelations/touches“).Out(“http://www.w3.org/2000/01/rdf-schema#label“).All()

Screen Shot 2014-06-29 at 10.58.30

You can used also filter – so to just see the counties bordering Southampton:

g.V(“http://data.ordnancesurvey.co.uk/id/7000000000037256“).Out(“http://data.ordnancesurvey.co.uk/ontology/spatialrelations/touches“).Has(“http://www.w3.org/1999/02/22-rdf-syntax-ns#type“,”http://data.ordnancesurvey.co.uk/ontology/admingeo/County“).Out(“http://www.w3.org/2000/01/rdf-schema#label“).All()

Screen Shot 2014-06-29 at 11.01.17

 

The Ordnance Survey linked data also has spatial predicates ‘contains’, ‘within’ as well as ‘touches’. Analogous queries can be done with those. E.g. find me everything Southampton contains:

g.V(“http://data.ordnancesurvey.co.uk/id/7000000000037256“).Out(“http://data.ordnancesurvey.co.uk/ontology/spatialrelations/contains“).Out(“http://www.w3.org/2000/01/rdf-schema#label“).All()

So after this very quick initial experiment it seems that Cayley is very good at providing an easy way of doing very quick/simple queries. One query I wanted to do was find everything in, say, Hampshire – the full transitive closure. This is very easy to do in SPARQL, but in Cayley (at first glance) you’d have to write some extra code (not exactly rocket science, but a bit of a faff compared to SPARQL). I rarely touch Javascript these days so for me personally this will never replace a triplestore with a SPARQL endpoint, but for JS developers this tool will be a great way to get started with and explore linked data/RDF. I might well brush up on my Javascript and provide more complicated examples in a later blog post…

 

 

 

Advertisement

Visualising the Location Graph – example with Gephi and Ordnance Survey linked data

March 28, 2014 2 comments

This is arguably a simpler follow up to my previous blog post, and here I want to look at visualising Ordnance Survey linked data in Gephi. Now Gephi isn’t really a GIS, but it can be used to visualise the adjacency graph where regions are represented as nodes in a graph, and links represent adjacency relationships.

The approach here will be very similar to the approach in my previous blog. The main difference is that you will need to use the Ordnance Survey SPARQL endpoint and not the DBpedia one. So this time in the Gephi semantic web importer enter the following endpoint URL:

http://data.ordnancesurvey.co.uk/datasets/os-linked-data/apis/sparql

The Ordnance Survey endpoint returns turtle by default, and Gephi does not seem to like this. I wanted to force the output as XML. I figured this could be done in the using a ‘REST parameter name’ (output) with value equal to xml. This did not seem to work, so instead I had to do a bit of a hack. In the ‘query tag…’ box you will need to change the value from ‘query’ to ‘output=xml&query’. You should see something like this in the Semantic Web Importer now:

Screen Shot 2014-03-28 at 11.28.28

Now click on the query tab. If we want to, for example, view the adjacent graph for consistuencies we can enter the following query:

prefix gephi:<http://gephi.org/>
construct {
?s gephi:label ?label .
?s gephi:lat ?lat .
?s gephi:long ?long .
?s <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/touches> ?o .}
where
{
?s a <http://data.ordnancesurvey.co.uk/ontology/admingeo/WestminsterConstituency> .
?o a <http://data.ordnancesurvey.co.uk/ontology/admingeo/WestminsterConstituency> .
?s <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/touches> ?o .
?s <http://www.w3.org/2000/01/rdf-schema#label> ?label .
?s <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
?s <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .
}

and click ‘run’. To visualise the output you will need to follow the exact same steps mentioned here (remember to recast the lat and long variables to decimal).

If we want to view adjacency of London Boroughs then we can do this with a similar query:

prefix gephi:<http://gephi.org/>
construct {
?s gephi:label ?label .
?s gephi:lat ?lat .
?s gephi:long ?long .
?s <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/touches> ?o .}
where
{
?s a <http://data.ordnancesurvey.co.uk/ontology/admingeo/LondonBorough> .
?o a <http://data.ordnancesurvey.co.uk/ontology/admingeo/LondonBorough> .
?s <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/touches> ?o .
?s <http://www.w3.org/2000/01/rdf-schema#label> ?label .
?s <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
?s <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .
}

When visualising you might want to change the scale parameter to 10000.0. You should see something like this:

Screen Shot 2014-03-28 at 11.40.18

So far so good. Now imagine we want to bring in some other data – recall my previous blog post here. We can use SPARQL federation to bring in data from other endpoints. Suppose we would like to make the size of the node represent the ‘IMD rank‘ of each London Borough…we can do with by bringing in data from the Open Data Communities site:

prefix gephi:<http://gephi.org/>
construct {
?s gephi:label ?label .
?s gephi:lat ?lat .
?s gephi:long ?long .
?s gephi:imd-rank ?imdrank .
?s <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/touches> ?o .}
where
{
?s a <http://data.ordnancesurvey.co.uk/ontology/admingeo/LondonBorough> .
?o a <http://data.ordnancesurvey.co.uk/ontology/admingeo/LondonBorough> .
?s <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/touches> ?o .
?s <http://www.w3.org/2000/01/rdf-schema#label> ?label .
?s <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
?s <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .
SERVICE <http://opendatacommunities.org/sparql> {
?x <http://purl.org/linked-data/sdmx/2009/dimension#refArea> ?s .
?x <http://opendatacommunities.org/def/IMD#IMD-score> ?imdrank . }
}

You will need to recast the imdrank as an integer for what follows (do this using the same approach used to recast the lat/long variables). You can now use Gephi to resize the nodes according to IMD rank. We do this using the ranking tab:

Screen Shot 2014-03-28 at 11.50.43

You should now see you London Boroughs re-sized according to their IMD rank:

Screen Shot 2014-03-28 at 11.51.51

turning the lights off and adding some labels we get:

Screen Shot 2014-03-28 at 12.04.27

All roads lead to? Experiments with Gephi, Linked Data and Wikipedia

March 26, 2014 3 comments

Gephi is “an interactive visualization and exploration platform for all kinds of networks and complex systems, dynamic and hierarchical graphs”. Tony Hirst did a great blog post a while back showing how you could use Gephi together with DBpedia (a linked data version of Wikipedia) to map an influence network in the world of philosophy. Gephi offers a semantic web plugin which allows you to work with the web of linked data. I recommend you read Tony’s blog to get started with using that plugin with Gephi. I was interested to experiment with this plugin, and to look at what sort of geospatial visualisations could be possible.

If you want to follow all the steps in this post you will need to:

Initially I was interested to see if there were any interesting networks we might visualise between places. In order to see how Wikipedia relates one place to another was a simple case of going to the DBpedia SPARQL endpoint and trying the following query:

select distinct ?p
where
{
?s a <http://schema.org/Place> .
?o a <http://schema.org/Place> .
?s ?p ?o .
}

– where s and o are places, find me what ‘p’ relates them. I noticed two properties ‘http://dbpedia.org/ontology/routeStart‘ and ‘http://dbpedia.org/ontology/routeEnd‘ so I thought I would try to visualise how places round the world were linked by transport connections.  To find places connected by a transport link you want to find pairs ‘start’ and ‘end’ that are the route start and route end, respectively, of some transport link. You can do this with the following query:

select ?start ?end
where
{
?start a <http://schema.org/Place> .
?end a <http://schema.org/Place> .
?link <http://dbpedia.org/ontology/routeStart> ?start .
?link <http://dbpedia.org/ontology/routeEnd> ?end .
}

This gives a lot of data so I thought I would restrict the links to be only road links:

select ?start ?end
where
{?start a <http://schema.org/Place> .
?end a <http://schema.org/Place> .
?link <http://dbpedia.org/ontology/routeStart> ?start .
?link <http://dbpedia.org/ontology/routeEnd> ?end .
?link a <http://dbpedia.org/ontology/Road> . }

We are now ready to visualise this transport network in Gephi. Follow the steps in Tony’s blog to bring up the Semantic Web Importer. In the ‘driver’ tab make sure ‘Remote – SOAP endpoint’ is selected, and the EndPoint URL is http://dbpedia.org/sparql. In an analogous way to Tony’s blog we need to construct our graph so we can visualise it. To simply view the connections between places it would be enough to just add this query to the ‘Query’ tab:

construct {?start <http://foo.com/connectedTo> ?end}
where
{
?start a <http://schema.org/Place> .
?end a <http://schema.org/Place> .
?link <http://dbpedia.org/ontology/routeStart> ?start .
?link <http://dbpedia.org/ontology/routeEnd> ?end .
?link a <http://dbpedia.org/ontology/Road> .
}

However, as we want to visualise this in a geospatial context we need the lat and long of the start and end points so our construct query becomes a bit more complicated:

prefix gephi:<http://gephi.org/>
construct {
?start gephi:label ?labelstart .
?end gephi:label ?labelend .
?start gephi:lat ?minlat .
?start gephi:long ?minlong .
?end gephi:lat ?minlat2 .
?end gephi:long ?minlong2 .
?start <http://foo.com/connectedTo> ?end}
where
{
?start a <http://schema.org/Place> .
?end a <http://schema.org/Place> .
?link <http://dbpedia.org/ontology/routeStart> ?start .
?link <http://dbpedia.org/ontology/routeEnd> ?end .
?link a <http://dbpedia.org/ontology/Road> .
{select ?start (MIN(?lat) AS ?minlat) (MIN(?long) AS ?minlong) where {?start <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat . ?start <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .} }
{select ?end (MIN(?lat2) AS ?minlat2) (MIN(?long2) AS ?minlong2) where {?end <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat2 . ?end <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long2 .} }
?start <http://www.w3.org/2000/01/rdf-schema#label> ?labelstart .
?end <http://www.w3.org/2000/01/rdf-schema#label> ?labelend .
FILTER (lang(?labelstart) = ‘en’)
FILTER (lang(?labelend) = ‘en’)
}

Note that query for the lat and long is a bit more complicated that it might be. This is because DBpedia data is quite messy, and many entities will have more than one lat/long pair. I used a subquery in SPARQL to pull out the minimum lat/long for all the pairs retrieved. Additionally I also retrieved the English labels for each of the start/end points.

Now copy/paste this construct query into the ‘Query’ tab on the Semantic Web Importer:

Screen Shot 2014-03-26 at 15.54.34

Now hit the run button and watch the data load.

To visual the data we need to do a bit more work. In Gephi click on the ‘Data Laboratory’ and you should now see your data table. Unfortunately all of the lats and longs have been imported as strings and we need to recast them as decimals. To do this click on the ‘More actions’ pull down menu and look for ‘Recast column’ and click it. In the ‘Recast manipulator’ window go to ‘column’ and select ‘lat(Node Table)’ from the pull down menu. Under ‘Convert to’ select ‘Double’ and click recast. Do the same for ‘long’.

Screen Shot 2014-03-26 at 16.01.19

when you are done click ‘ok’ and return to the ‘overview’ tab in Gephi. To see this data geospatially go to the layout panel and select ‘Geo Layout’. Change the latitude and longitude to your new recast variable names, and unclick ‘center’ (my graph kept vanishing with it selected). Experiment with the scale value:

Screen Shot 2014-03-26 at 16.09.49

You should now see something like this:

Screen Shot 2014-03-26 at 16.11.13

in your display panel (click image to view in higher resolution).

Given that this is supposed to be a road network you will find some oddities. This it seems to down to ‘European routes’ like European route E15 that link from Scotland down to Spain.

Tell Me About Hampshire – Linking Government Data using SPARQL federation 2

March 23, 2014 3 comments

Yesterday I blogged about how to do some SPARQL federated queries across various government websites, and this blog is a continuation of this with a different example. In this blog I give an example query which basically say ‘tell me stuff about Hampshire‘. I do this by linking up data from Ordnance Survey, the Office of National Statistics, the Department of Communities and Local Government and Hampshire County Council. This query is really just for illustrative purposes, but I want to ask ‘for all districts in Hampshire find me the index of multiple deprivation rank, the change order and operative date for that district, the website for the local authority of that district along with the addresses of parcels of land where it is planned to build new dwellings. To achieve this I need to take data from several sources and use SPARQL federation. Here is the query that answers my question. First I query Ordnance Survey linked data to find districts in Hampshire, and I then pass these districts to three other linked data services to retrieve the relevant information. To try this example head over to the Ordnance Survey SPARQL endpoint and copy/paste the following:

select ?districtname ?imdrank ?changeorder ?opdate ?councilwebsite ?siteaddress
where
{?district <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/within>
   <http://data.ordnancesurvey.co.uk/id/7000000000017765> .
  ?district a <http://data.ordnancesurvey.co.uk/ontology/admingeo/District> .
  ?district <http://www.w3.org/2000/01/rdf-schema#label> ?districtname .
 SERVICE <http://opendatacommunities.org/sparql> {
 ?s <http://purl.org/linked-data/sdmx/2009/dimension#refArea> ?district .
?s <http://opendatacommunities.org/def/IMD#IMD-rank> ?imdrank .
 ?authority <http://opendatacommunities.org/def/local-government/governs> ?district .
 ?authority <http://xmlns.com/foaf/0.1/page> ?councilwebsite .
 }
 ?district <http://www.w3.org/2002/07/owl#sameAs> ?onsdist .
 SERVICE <http://statistics.data.gov.uk/sparql> {
 ?onsdist <http://statistics.data.gov.uk/def/boundary-change/originatingChangeOrder>
          ?changeorder .
 ?onsdist <http://statistics.data.gov.uk/def/boundary-change/operativedate>
          ?opdate .
 }
 SERVICE <http://linkeddata.hants.gov.uk/sparql> {
   ?landsupsite <http://data.ordnancesurvey.co.uk/ontology/admingeo/district> ?district .
   ?landsupsite a <http://linkeddata.hants.gov.uk/def/land-supply/LandSupplySite> .
   ?landsupsite
<http://www.ordnancesurvey.co.uk/ontology/BuildingsAndPlaces/v1.1/BuildingsAndPlaces.owl#hasAddress>
   ?siteaddress .
   }
}

Happy SPARQLing…

Ordnance Survey Linked Data: The Search API

September 24, 2013 Leave a comment

Please note in some of the examples below I have been having trouble with wordpress ‘correcting’ quote marks in my text. If you find the queries don’t work you may need to manually replace the copied quote marks from below with new ones via your keyboard. Hope that makes sense.

One of the biggest improvements to the new Ordnance Survey Linked Data site is the much improved search functionality. You can either search over a specific dataset (e.g. the Code-Point(R) Open linked data) or over all the combined datasets. I will first give some examples of using the Boundary-Line(TM) search API.

The Boundary-Line search API explorer can be found here. The simplest use of this search API is to enter some text for the name of an administrative area or the GSS code (the ONS identifier for a statistical region) into the search box. To get started enter Southampton into the query box. You will see that the search results are returned in JSON (RSS and Atom are additional options). Results contain the URI of the entities that match your queries along with a number of useful attributes.

Note that the Request box shows the actual GET request that is being done, and you can use this GET request in your applications. Now try searching for a GSS code, enter E06000045 into the query box. You should see results for the City of Southampton returned. So far so straight forward. The search function also allows for wildcards in search, for example in the Query box type:

label:Southa*

It is also possible to narrow search results by type. Recall that the search for Southampton returned both Westminster constituencies and a unitary authority with Southampton in their name. To just find the Westminster constituencies search for the following:

label:Southampton AND type:”http://data.ordnancesurvey.co.uk/ontology/admingeo/WestminsterConstituency

The search API also allows you to perform a number of simple spatial queries. The first of these are bounding box queries. For the Boundary-Line data you can specify a bounding box, and find all the administrative regions whose centroids lie within that bounding box. The bounding box can be expressed in eastings and northings. For example try the following:

easting:[371000 TO 374000] AND northing:[161000 TO 164500]

in the query box.

The answers can be narrowed down further by specifying the type of object that should be returned. For example to just get the civil parishes in this bounding box try the following:

easting:[371000 TO 374000] AND northing:[161000 TO 164500] AND type:”http://data.ordnancesurvey.co.uk/ontology/admingeo/CivilParish

Another type of simple spatial query we can do in the search API is ‘find me all feature of a kind type within a certain radius of a given point’. Here the point can be specified in either lat/long or easting/northing. To find all of the civil parishes in a 50 km radius of the point with easting 442339 and northing 112882 put:

type:”http://data.ordnancesurvey.co.uk/ontology/admingeo/CivilParish

into the query box and put the appropriate values in the easting and northing boxes, followed by a 50 in the radio search box. If, for example, you want to perform this query again but find civil parishes and districts enter the following into the query box:

type:”http://data.ordnancesurvey.co.uk/ontology/admingeo/CivilParish OR type:”http://data.ordnancesurvey.co.uk/ontology/admingeo/District

and try the query again.

These are just some simple examples of the search API. The full documentation is here.

Putting SPARQL on the Map with Ordnance Survey Linked Data & OS OpenSpace

August 20, 2013 Leave a comment

A colleague was asking me if I knew how to plot SPARQL query results from the Ordnance Survey linked data onto an OS OpenSpace map. Although I’d done it a few times before, it was never something I’d blogged. So here goes…

This is a lot easier than you might imagine. The first thing you want to do is perform your SPARQL query and get back the results as a csv file. I blogged about this a while back, but here is a quick recap. Let us suppose I want to plot a centroid for all the districts in England, and have their name appear in the pop up text. It is easy to perform a query to get back the easting, northing and name for all the districts. First go to the Boundary-Line(TM) SPARQL endpoint and enter the following query:

select ?x ?y ?name
where
{
?a <http://www.w3.org/2000/01/rdf-schema#label> ?name .
?a <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/easting> ?x .
?a <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/northing> ?y .
?a a <http://data.ordnancesurvey.co.uk/ontology/admingeo/District> .
}

Make sure the response format is set to CSV. Now click the query button. The “Reponse” box will have your query results, and the “Request box” should have a long complicated looking URL:

http://data.ordnancesurvey.co.uk/datasets/boundary-line/apis/sparql?query=select+%3Fx+%3Fy+%3Fname%0D%0Awhere%0D%0A%7B%0D%0A%3Fa+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23label%3E+%3Fname+.%0D%0A%3Fa+%3Chttp%3A%2F%2Fdata.ordnancesurvey.co.uk%2Fontology%2Fspatialrelations%2Feasting%3E+%3Fx+.%0D%0A%3Fa+%3Chttp%3A%2F%2Fdata.ordnancesurvey.co.uk%2Fontology%2Fspatialrelations%2Fnorthing%3E+%3Fy+.%0D%0A%3Fa+a+%3Chttp%3A%2F%2Fdata.ordnancesurvey.co.uk%2Fontology%2Fadmingeo%2FDistrict%3E+.%0D%0A%7D&output=csv

So far so easy.

Now we come to the OS OpenSpace part. It is easy to plot a text file in OS OpenSpace. To find out how go to the OS OpenSpace Code Playground and select the link “Add markers and text from a file“. You should see an example mashup showing some points plotted. To see what is going on click on “Edit in Code Playground” and you should see the javascript & HTML that produces the map. In the Javascript window you can edit the code and preview the changes. For this example the first simple thing you need to do is adjust the zoom level. To do this change:

    osMap.setCenter(new OpenSpace.MapPoint(400000, 400000), 7);

to

    osMap.setCenter(new OpenSpace.MapPoint(400000, 400000), 1);

so we are zoomed all the way out.

We now need to change the input text file. To do this change the following line in the sample code:

    var markersFile = “/res/mymarkers.txt”;

In this line replace /res/mymarkers.txt with the URL you got from the SPARQL endpoint in the Request box. Once you have done that click the ‘render’ button and you should now see your results plotted on an OS OpenSpace map. Click on a map pointer to display the name of the district. Easy as that.

As an exercise to the reader…consult my last few blog posts and display markers for postcodes in a district of your choosing.

Ordnance Survey SPARQL Endpoint

August 7, 2013 Leave a comment

I just wanted to quick mention one feature of the Ordnance Survey linked data SPARQL endpoints that I think it pretty neat.  Go to the SPARQL endpoint and try one of the queries from my last four blogs posts. In this post I’ll got with the following simple query (recall this query gets the name, lat, long, gss code and unit_id for all districts in Great Britain):

select ?name ?lat ?long ?gss ?unit_id

where

{

?x <http://www.w3.org/2000/01/rdf-schema#label> ?name .

?x <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .

?x <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .

?x <http://data.ordnancesurvey.co.uk/ontology/admingeo/gssCode> ?gss .

?x <http://data.ordnancesurvey.co.uk/ontology/admingeo/hasUnitID> ?unit_id .

?x a <http://data.ordnancesurvey.co.uk/ontology/admingeo/District> .

}

You will notice that on hitting the query button that a box will appear that says “Request” and a rather long URL will appear:

SPARQL

You can now use this URL to issue a GET request in PHP, Javascript etc. and use these output within a web application just as you would with any API call. To see this working in a simple way copy the long URL you get from your SPARQL query and at the command line (if running something UNIXy) type:

curl LONG_URL

where LONG_URL is your long URL. You should now see the JSON response from that GET request.

Happy SPARQLing…

Ordnance Survey Linked Data – Combining postcode and spatial queries

August 5, 2013 Leave a comment

In my previous blog posts (here, here and here) I concentrated on a few simple SPARQL queries. However, these SPARQL queries were only performed on one dataset at a time. With linked data things get more interesting when you combine datasets (even if those datasets are both from the same publisher). The original opendata for Code-Point(R) Open was published as a CSV file, and Boundary-Line(TM) was published as a shape file. Both formats are useful, but combining the two datasets can be tricky if you are not sure how to use shape files. The two datasets are, however, implicitly linked via a key. In the linked data versions we have converted both datasets into a common data language (RDF) and made those implicit links explicit (see https://johngoodwin225.wordpress.com/2013/08/05/ordnance-survey-linked-data-a-simple-postcode-query/ ). We can now easily query both datasets together.

To query both datasets go to the SPARQL endpoint for the combined data:

http://data.ordnancesurvey.co.uk/datasets/os-linked-data/explorer/sparql

Recall that the query:

select ?postcode ?lat ?long
where
{
  ?x <http://www.w3.org/2000/01/rdf-schema#label> ?postcode .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .
  ?x <http://data.ordnancesurvey.co.uk/ontology/postcode/ward>
     <http://data.ordnancesurvey.co.uk/id/7000000000017707> .
}

finds all of the postcodes (and their lat/long) in a ward called Bevois. Using the combined datasets we can run queries like: find me all postcodes in the ward Bevois and all the wards touching Bevois. Let’s break this down. To find all postcodes (and their lat/long) in regions that touch Bevois first find all the wards that touch Bevois, and then find all the postcodes within those wards. So the first line in the query below  “?y <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/touches&gt; <http://data.ordnancesurvey.co.uk/id/7000000000017707>&#8221; finds all wards touching Bevois, and the rest of the query matches postcodes in the wards “?y”:

select ?postcode ?lat ?long
where
{
  ?y <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/touches>
  <http://data.ordnancesurvey.co.uk/id/7000000000017707> .
  ?x <http://www.w3.org/2000/01/rdf-schema#label> ?postcode .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .
  ?x <http://data.ordnancesurvey.co.uk/ontology/postcode/ward> ?y .
}

To find all the postcodes in Bevois and the wards touching Bevois we simply union the two queries above together:

select ?postcode ?lat ?long
where
{{
  ?x <http://www.w3.org/2000/01/rdf-schema#label> ?postcode .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .
  ?x <http://data.ordnancesurvey.co.uk/ontology/postcode/ward>
     <http://data.ordnancesurvey.co.uk/id/7000000000017707> .
}

UNION

{
  ?y <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/touches>
  <http://data.ordnancesurvey.co.uk/id/7000000000017707> .
  ?x <http://www.w3.org/2000/01/rdf-schema#label> ?postcode .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .
  ?x <http://data.ordnancesurvey.co.uk/ontology/postcode/ward> ?y .
}}

As an exercise for the reader – find all postcodes in Southampton and districts touching Southampton. Happy SPARQLing…

Ordnance Survey Linked Data – A Simple Postcode Query

August 5, 2013 1 comment

In the previous two blog posts (here and here) I gave some simple examples of queries you could run on the Ordnance Survey Boundary-Line(TM) linked data. Here I want to give some simple examples of what you can do with the Code-Point(R) Open linked data. The Code-Point(R) Open linked data has a URI for every Postcode Unit, Postcode Sector, Postcode District and Postcode Area in England, Scotland and Wales.  Each Postcode Unit is nested within a Postcode Sector, each Postcode Sector is nested within Postcode District and each Postcode District is nested within a Postcode Area. The reciprocal contains relationships are also included.

A Postcode Unit URI takes the form:

http://data.ordnancesurvey.co.uk/id/postcodeunit/SO160AS

A Postcode SectorURI takes the form:

http://data.ordnancesurvey.co.uk/id/postcodesector/SO160

A Postcode District URI takes the form:

http://data.ordnancesurvey.co.uk/id/postcodedistrict/SO16

A Postcode Area URI takes the form:

http://data.ordnancesurvey.co.uk/id/postcodearea/SO

Let us now try some SPARQL. Go to the CodePoint-Open SPARQL endpoint, and for simplicity select the response format to CSV. Supposed we wanted to select all of the postcodes and their lat/long coordinate for postcodes within the SO postcode area. This can be done simply as follows:

select ?postcode ?lat ?long
where
{
  ?x <http://www.w3.org/2000/01/rdf-schema#label> ?postcode .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .
  ?x <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/within>
     <http://data.ordnancesurvey.co.uk/id/postcodearea/SO> .
}

Notice that I do not need to specify that ‘x’ is a postcode unit in this case as only postcode units have a lat/long value.

Let us try a slightly more complicated query – say I wanted all the postcodes, and their lat/long, within the postcode sectors SO16 and SO17. I would do a query similar to the above, but with a UNION to collect together results for each postcode sector:

select ?postcode ?lat ?long
where
{
{
  ?x <http://www.w3.org/2000/01/rdf-schema#label> ?postcode .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .
  ?x <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/within>
     <http://data.ordnancesurvey.co.uk/id/postcodedistrict/SO16> .
}
UNION
{
  ?x <http://www.w3.org/2000/01/rdf-schema#label> ?postcode .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .
  ?x <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/within>
     <http://data.ordnancesurvey.co.uk/id/postcodedistrict/SO17> .
}
}

So far so easy…

The Code-Point Open linked data also contains links to the administrative areas that ‘contain’ the postcode. A word of caution is needed here. Postcodes do not respective administrative boundaries so containment in this case actually means the administrative region that the lat/long for that postcode lies within. There are three predicates for relating postcode units to administrative areas. These are:

  • ward – this relates postcode units to wards and unitary electoral divisions
  • district – this relates postcode units to districts, metropolitan districts, London boroughs and unitary authorities
  • county – this relates postcode units to counties (where applicable)

So say I want a list of all the postcode units (and their lat/longs) that lie within the ward of Bevois. This is a straightforward query. First you can find the URI for Bevois here. You’ll find the URI for Bevois is:

http://data.ordnancesurvey.co.uk/id/7000000000017707

Now enter the following SPARQL query to find all the postcodes in Bevois:

select ?postcode ?lat ?long
where
{
  ?x <http://www.w3.org/2000/01/rdf-schema#label> ?postcode .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
  ?x <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .
  ?x <http://data.ordnancesurvey.co.uk/ontology/postcode/ward>
     <http://data.ordnancesurvey.co.uk/id/7000000000017707> .
}

I’ll leave it as an exercise for the reader to find all of the postcode units (and their lat/long) in Southampton.

What if I want to find out the postcode districts that are covered by the ward Bevois? First I find all the postcode units in Bevois, and then I do a query to look up all the postcode districts those postcode units are within as follows:

select distinct ?postcodedistrict
where
{
  ?x <http://data.ordnancesurvey.co.uk/ontology/postcode/ward>
     <http://data.ordnancesurvey.co.uk/id/7000000000017707> .
  ?x <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/within> ?y .
  ?y a <http://data.ordnancesurvey.co.uk/ontology/postcode/PostcodeDistrict> .
  ?y <http://www.w3.org/2000/01/rdf-schema#label> ?postcodedistrict .
}

You will now see all the postcode districts the ward Bevois covers. As a final exercise to the reader perform the same query, but find postcode sectors that cover Southampton. Happy SPARQLing.