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
?a <> ?name .
?a <> ?x .
?a <> ?y .
?a a <> .

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:

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);


    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.