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.


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

select distinct ?type
  ?a rdf:type ?type .
limit 100

This tells me what the data store contains - such as

I snoop...

Useful query number 2

PREFIX rdf: <>

select *
  ?civilparish a <>.
limit 10

One of the returned rows is 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: and you'll reach the ward:

(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: <>
PREFIX myextent: <>
PREFIX myparish: <>
PREFIX myrdf: <>
PREFIX mygml: <>

select ?mycivilparishlabel ?mypolygon
  <> mypc:district ?mydistrict. 
  ?mydistrict myparish: ?acivilparish.
  ?acivilparish myrdf: ?mycivilparishlabel.
  ?acivilparish myextent: ?acivilparishextent.
  ?acivilparishextent mygml: ?mypolygon.
orderby ?mycivilparishlabel
limit 100


Next step

Get my head around...

PREFIX spatialrelations: <>


  • 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?)


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

Page last updated 24th August 2011
[email protected] 

Valid CSS!   Valid XHTML 1.0 Strict