Archive

Posts Tagged ‘OS OpenSpace’

Putting SPARQL on the Map with Ordnance Survey Linked Data & OS OpenSpace

August 20, 2013 Leave a comment

A colleague was asking me if I knew how to plot SPARQL query results from the Ordnance Survey linked data onto an OS OpenSpace map. Although I’d done it a few times before, it was never something I’d blogged. So here goes…

This is a lot easier than you might imagine. The first thing you want to do is perform your SPARQL query and get back the results as a csv file. I blogged about this a while back, but here is a quick recap. Let us suppose I want to plot a centroid for all the districts in England, and have their name appear in the pop up text. It is easy to perform a query to get back the easting, northing and name for all the districts. First go to the Boundary-Line(TM) SPARQL endpoint and enter the following query:

select ?x ?y ?name
where
{
?a <http://www.w3.org/2000/01/rdf-schema#label> ?name .
?a <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/easting> ?x .
?a <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/northing> ?y .
?a a <http://data.ordnancesurvey.co.uk/ontology/admingeo/District> .
}

Make sure the response format is set to CSV. Now click the query button. The “Reponse” box will have your query results, and the “Request box” should have a long complicated looking URL:

http://data.ordnancesurvey.co.uk/datasets/boundary-line/apis/sparql?query=select+%3Fx+%3Fy+%3Fname%0D%0Awhere%0D%0A%7B%0D%0A%3Fa+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23label%3E+%3Fname+.%0D%0A%3Fa+%3Chttp%3A%2F%2Fdata.ordnancesurvey.co.uk%2Fontology%2Fspatialrelations%2Feasting%3E+%3Fx+.%0D%0A%3Fa+%3Chttp%3A%2F%2Fdata.ordnancesurvey.co.uk%2Fontology%2Fspatialrelations%2Fnorthing%3E+%3Fy+.%0D%0A%3Fa+a+%3Chttp%3A%2F%2Fdata.ordnancesurvey.co.uk%2Fontology%2Fadmingeo%2FDistrict%3E+.%0D%0A%7D&output=csv

So far so easy.

Now we come to the OS OpenSpace part. It is easy to plot a text file in OS OpenSpace. To find out how go to the OS OpenSpace Code Playground and select the link “Add markers and text from a file“. You should see an example mashup showing some points plotted. To see what is going on click on “Edit in Code Playground” and you should see the javascript & HTML that produces the map. In the Javascript window you can edit the code and preview the changes. For this example the first simple thing you need to do is adjust the zoom level. To do this change:

    osMap.setCenter(new OpenSpace.MapPoint(400000, 400000), 7);

to

    osMap.setCenter(new OpenSpace.MapPoint(400000, 400000), 1);

so we are zoomed all the way out.

We now need to change the input text file. To do this change the following line in the sample code:

    var markersFile = “/res/mymarkers.txt”;

In this line replace /res/mymarkers.txt with the URL you got from the SPARQL endpoint in the Request box. Once you have done that click the ‘render’ button and you should now see your results plotted on an OS OpenSpace map. Click on a map pointer to display the name of the district. Easy as that.

As an exercise to the reader…consult my last few blog posts and display markers for postcodes in a district of your choosing.

Follow

Get every new post delivered to your Inbox.

Join 2,189 other followers