Search code examples
sparqldbpedia

Use SPARQL and Dbpedia to query for cities within a given radius based on long and lat


I've been trying to use DBpedia and SPARQL to get cities based on a geo location.

E.g., http://dbpedia.org/page/Berlin has the properties

geo:lat: 52.516666 (xsd:float)
geo:long: 13.383333 (xsd:float)

and I want to query cities based on location and within a given radius. E.g., I want to find cities within 10km of lat: 51.5033640 and long: -0.1276250

I know this gives me cities --

SELECT ?city WHERE {      ?city rdf:type dbo:City    } 

-- but I don't how to use filter in order to filter out based on radius. Does anyone know that?


Solution

  • Use Virtuoso GeoSpatial functions bif:st_intersects and bif:st_point:

    Either with the geometry object (no cities for your London coordinates, change to dbo:Place to see that there other objects around London):

    SELECT  * { 
     ?city a dbo:City ;
           geo:geometry ?geo
     FILTER ( bif:st_intersects( ?geo, bif:st_point (-0.1276250, 51.5033640), 10))
    } 
    

    Or with lat/long:

    SELECT * { 
     ?city a dbo:City ;
           geo:lat ?lat ;
           geo:long ?long .
     FILTER ( bif:st_intersects( bif:st_point (?long, ?lat), bif:st_point (-0.1276250, 51.5033640), 10))
    }