SparQL

I've been playing with SparQL. The acronym stands for SPARQL Protocol and RDF Query Language. If you appreciate the wacky humour of  the self referential acronym - then I think you will love SparQL.

I don't. Anything to do with with databases tends to leave me cold - but SparQL does have some very interesting concepts.

Background

A background story will explain my motivation for my investigation.

Picture a fetid bordello in Milton Keynes. The elders of the MKBAG enclave are in session. We've raided an ISP and have a list of postcodes that will be served with BT's Fibre To The Premises trial.

There's a problem - people living at these postcodes don't know about the trial.

We decide to approach the civil parish councilors - they can make trial announcement in their propaganda leaflets.

The enclave's wise leader leans over to me, covering me with thick blue brown cigar smoke: "I want you to select all postcodes within MK that are on the FTTP trial, and then group them by parish."

"I want the list by tomorrow!" he demanded as he pressed a grubby USB memory stick in my hand. "That's got an Excel spreadsheet with all the parish details."

The Approach

I was not going to use dubious data. Let's go for the golden source. In the UK that's the Ordinance Survey. They will list postcodes by parish.

I quickly discovered their SparQL query test page. (Copy the Sparql text below in to their testpage.)

Useful query number 1

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

select distinct ?type
where
{
  ?a rdf:type ?type .
}
limit 100

This tells me what the data store contains - such as http://data.ordnancesurvey.co.uk/ontology/admingeo/CivilParish

I snoop...

Useful query number 2

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

select *
where
{
  ?civilparish a <http://data.ordnancesurvey.co.uk/ontology/admingeo/CivilParish>.
}
limit 10

One of the returned rows is http://data.ordnancesurvey.co.uk/id/7000000000000611 the parish of "Wick and Abson."

This record give me an "extent" which is a series of boundary points in OSGB coordinates.

A problem!

I discover that wards can be easily found if you have a postcode:

Follow this link for my post code: http://data.ordnancesurvey.co.uk/id/postcodeunit/MK109HG and you'll reach the ward: http://data.ordnancesurvey.co.uk/doc/7000000000041910.

(I'm sure there's another dataset that will yield the ward councilors' names.)

But the problem is civil parishes are not part of the postcode result.

An interim solution

Given the urgency I have, I'll construct a query that gets all the parish boundaries in Milton Keynes. I master some C# code that will "IsPointInPolygon".

I'm sure there is a way to do this using SparQL - probably by hosting my own web server with  an IsPointInPolygon implementation. (I put this down to the OS's penchant for OSGB co-ordinates, not lat longs.)

Useful query number 3

 I request all the parish names and parish boundaries for the district (Milton Keynes) where I live (MK10 9HG).

PREFIX mypc: <http://data.ordnancesurvey.co.uk/ontology/postcode/>
PREFIX myextent: <http://data.ordnancesurvey.co.uk/ontology/geometry/extent>
PREFIX myparish: <http://data.ordnancesurvey.co.uk/ontology/admingeo/parish>
PREFIX myrdf: <http://www.w3.org/2000/01/rdf-schema#label>
PREFIX mygml: <http://data.ordnancesurvey.co.uk/ontology/geometry/asGML>

select ?mycivilparishlabel ?mypolygon
where
{
  <http://data.ordnancesurvey.co.uk/id/postcodeunit/MK109HG> mypc:district ?mydistrict. 
  ?mydistrict myparish: ?acivilparish.
  ?acivilparish myrdf: ?mycivilparishlabel.
  ?acivilparish myextent: ?acivilparishextent.
  ?acivilparishextent mygml: ?mypolygon.
}
orderby ?mycivilparishlabel
limit 100

Links

http://www.pezholio.co.uk/2010/07/further-adventures-in-sparql/

http://blog.whatclinic.com/tag/ontologies

http://answers.semanticweb.com/questions/2042/how-to-represent-ordnance-survey-national-grid-reference-system-locations-in-rdf-and-convert-between-wgs84-and-osgb36

http://beta.kasabi.com/dataset/ordnance-survey-linked-data/apis/2rb

http://www.w3.org/2011/02/GeoSPARQL.pdf

http://os.services.tso.co.uk/geo/api

Next step

Get my head around...

PREFIX spatialrelations: <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/>

Likes

  • I can play with live data. That's data that is current. If the data is updated - I've got the latest! (That may seem obvious, but where I've worked with quarterly updates from a mapping supplier - this is pretty BIG!)
  • Queries can be run on "the cloud" or some data centre - I don't care - they are QUICK. The Milton Keynes query above returns 473kb within 400ms. (It's the browser that slows the rendering; oh and the FTTP connection to my house helps.)
  • Queries are SQL like. (Is that a good thing?)

Dislikes

  • What an arcane syntax!
  • Need more help and examples!

Page last updated 24th August 2011
nick@anotherurl.com 

Valid CSS!   Valid XHTML 1.0 Strict