1

I have the following, which retrieves the lat/long when given a postcode. How can I do the reverse (give lat/long, retrieve postcode) in SPARQL?

PREFIX pc: <http://data.ordnancesurvey.co.uk/ontology/postcode/>
PREFIX pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>

SELECT ?lat ?long
WHERE {
  <http://data.ordnancesurvey.co.uk/id/postcodeunit/S24SU> pos:lat ?lat .
  <http://data.ordnancesurvey.co.uk/id/postcodeunit/S24SU> pos:long ?long .
}
enter code here

http://data.ordnancesurvey.co.uk/datasets/os-linked-data

  • 1
    This isn't a duplicate, but there's [Querying Open Data Communities Data with SPARQL](http://stackoverflow.com/q/16608265/1281433) is related, sort of. – Joshua Taylor Sep 16 '14 at 20:07

1 Answers1

2

SPARQL is all about graph pattern matching. In your existing query, ?lat and ?long are variables, and you're looking for values for them. If you have a latitude and longitude already, you can can just put them into your query and replace the place with a variable. E.g.:

PREFIX pc: <http://data.ordnancesurvey.co.uk/ontology/postcode/>
PREFIX pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>

SELECT ?place
WHERE {
  ?place pos:lat <your-lat-here> .
  ?place pos:long <your-long-here> .
}

You just need to replace &langle;your-lat-here&rangle; and &langle;your-long-here&rangle; with the kind of values that you get as results to your original query.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Perfect! Thanks. You don't want to know how long I struggled over the syntax :-) –  Sep 16 '14 at 21:03
  • 1
    @TonyBlundell The [SPARQL 1.1 recommendation](http://www.w3.org/TR/sparql11-query/) is online, and it's got lots of examples. – Joshua Taylor Sep 16 '14 at 21:36