Home > linked data, Semantic Web > Ordnance Survey Linked Data – A Simple Postcode Query

Ordnance Survey Linked Data – A Simple Postcode Query


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.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: