Pezholio

Further Adventures in SPARQL

| Comments

It’s been a while since my last blog post, and after a request from Twitter, I thought it might be time to dust off Wordpress and do a quick blog post. Since we last spoke, UK Postcodes (the postcodes webservice I blogged about a few blog posts back) has been plodding along nicely and, as a natural tinkerer, I’ve been tinkering ever since.

I’ve sped up request times considerably, and wanted to add the ability to get parliamentary constituencies from a Postcode - originally, I was using the TheyWorkForYou API, which, while useful, didn’t return a National Statistics or Ordnance Survey URI / ID, so I got to thinking about how I could SPARQL this using the National Statistics Endpoint, after some exploration, I noticed that district wards were always inside one parliamentary consituency, so thanks to some linked data knowledge (helpfully pumped into my brain at a two-day training course at Talis a few weeks back), I got to writing a SPARQL query, which you can see below (using the SNAC ID for Coleshill South Ward, where I live):

PREFIX rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
PREFIX administrative-geography: http://statistics.data.gov.uk/def/administrative-geography/
PREFIX rdfs: http://www.w3.org/2000/01/rdf-schema#
PREFIX skos: http://www.w3.org/2004/02/skos/core#
PREFIX electoral-geography: http://statistics.data.gov.uk/def/electoral-geography/

SELECT ?ward ?label ?constituency ?constituencyname
WHERE {
?ward
a http://data.ordnancesurvey.co.uk/ontology/admingeo/Ward .
?ward skos:notation "44UBGC"^^administrative-geography:StandardCode .
?ward rdfs:label ?label .
?ward electoral-geography:parliamentaryConstituency ?constituency .
?constituency skos:prefLabel ?constituencyname ;
}
LIMIT 1

This query basically asks for the URI and name of a ward with a specific SNAC ID, as well as the constiuency’s URI and name. I’ve now added this to UK Postcodes.

But I haven’t stopped there, oh no! The next step is to get parishes. Again, ward and parish boundaries don’t overlap, so I’ve used another (slightly more complex) SPARQL query to find out what parish a ward is inside, this time using the Ordnance Survey Endpoint:

PREFIX rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
PREFIX foaf: http://xmlns.com/foaf/0.1/
PREFIX spatialrelations: http://data.ordnancesurvey.co.uk/ontology/spatialrelations/
PREFIX admingeo: http://data.ordnancesurvey.co.uk/ontology/admingeo/

SELECT ?parish ?parishcode ?ward ?label
WHERE {
?ward a http://data.ordnancesurvey.co.uk/ontology/admingeo/DistrictWard .
?ward admingeo:hasCensusCode "44UBGB" .
?parish a http://data.ordnancesurvey.co.uk/ontology/admingeo/CivilParish .
?parish spatialrelations:contains ?ward .
?parish admingeo:hasCensusCode ?parishcode .
?parish foaf:name ?label ;
}
LIMIT 1

This fella here is asking for a parish which contains a given ward - at the moment I’m using the OS URI, but I’m hoping to figure out a way of using SNAC IDs before I put it onto the live UK Postcodes system.

Hope this gives people a bit of an insight, and if anyone who is cleverer than me wants to point out any problems, or make any suggestions, feel free! :)

Comments