SPARQL Geolocation Query Examples

  • A table of countries, capitals and lat/longs out of dbpedia:
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
    PREFIX dbo: <http://dbpedia.org/ontology/>

    SELECT distinct ?country ?capital ?caplat ?caplong
    WHERE {
      ?country rdf:type dbo:Country .
      ?country  dbo:capital ?capital .
      OPTIONAL {
        ?capital geo:lat ?caplat ;
          geo:long ?caplong .
      }
    }
    ORDER BY ?country
  • Querying dbpedia for the capital, latitude and longitude of a specific country:
    PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX dbp: <http://dbpedia.org/property/>
    PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
    PREFIX foaf: <http://xmlns.com/foaf/0.1/>
    SELECT ?country ?capital ?lat ?long WHERE {
        ?country a dbo:Country ; foaf:name "Germany"@en; dbo:capital [ geo:lat ?lat ; geo:long ?long ; dbp:name ?capital ]
    }
  • Dbpedia holds the geographical coordinates of resources that can be geographically positioned. The coordinates can be retrieved with the query :
    PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
    PREFIX dbo: <http://dbpedia.org/ontology/>
    SELECT * WHERE {
    ?s a dbo:Place .
    ?s geo:lat ?lat .
    ?s geo:long ?long .
    }
    LIMIT 100
  • Points around a given co-ordinate can be retrieved with the following query :
    PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
    PREFIX dbo: <http://dbpedia.org/ontology/>
    SELECT * WHERE {
    ?s a dbo:Place .
    ?s geo:lat ?lat .
    ?s geo:long ?long .
    } FILTER ( ?long > YOUR_LONG - radius && ?long < YOUR_LONG + radius &&
    lat > YOUR_LAT - radius && ?lat < YOUR_LAT + radius)
    LIMIT 100
  • GoodRelations and Geo / Location Data describes three patterns for describing GeoLocation data and provides the following query which lists the first 50 store locations and their longitude and latitude from a given graph or endpoint using the three patterns :
    PREFIX geo:   <http://www.w3.org/2003/01/geo/wgs84_pos#>
    PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
    PREFIX vc:    <http://www.w3.org/2001/vcard-rdf/3.0#>
    SELECT ?store, ?long, ?lat WHERE
    {?store a gr:LocationOfSalesOrServiceProvisioning.
      {
        {
          ?store vcard:geo ?y.
          ?y vcard:latitude ?lat.
          ?y vcard:longitude ?long.
        }
    UNION
        {
          ?store geo:location ?y.
          ?y geo:lat ?lat.
          ?y geo:long ?long.
        }
    UNION
        {
          ?store vc:GEO ?y.
          ?y vc:latitude ?lat.
          ?y vc:longitude ?long.
        }
    UNION
    # The following is a wrong usage of the geo vocabulary, but may be a common pattern on the Web
        {
          ?store geo:lat ?lat.
          ?store geo:long ?long.
        }
      }
    }