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 administrative-geography:
PREFIX rdfs:
PREFIX skos:
PREFIX electoral-geography:

SELECT ?ward ?label ?constituency ?constituencyname
a .
?ward skos:notation "44UBGC"^^administrative-geography:StandardCode .
?ward rdfs:label ?label .
?ward electoral-geography:parliamentaryConstituency ?constituency .
?constituency skos:prefLabel ?constituencyname ;

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 foaf:
PREFIX spatialrelations:
PREFIX admingeo:

SELECT ?parish ?parishcode ?ward ?label
?ward a .
?ward admingeo:hasCensusCode "44UBGB" .
?parish a .
?parish spatialrelations:contains ?ward .
?parish admingeo:hasCensusCode ?parishcode .
?parish foaf:name ?label ;

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! :)