Tutorial X:
Covers SPARQL Extensions for
Geo Spatials & Geography

OpenLink Software

Live Instances

All the queries in this tutorial can be verified against the following live instances:

For each of these instances you have /sparql (basic SPARQL Endpoint) and /isparql (SPARQL Query By Example UI and Endpoint).

Basic Geo Lookups: Distance Variant I

## "Find things within 20km of New York City":

SELECT DISTINCT ?resource ?label ?location
WHERE
  {
    <http://dbpedia.org/resource/New_York_City> geo:geometry ?sourcegeo .
    ?resource geo:geometry ?location ;
                                 rdfs:label ?label .
    FILTER( bif:st_intersects( ?location, ?sourcegeo, 20 ) ) .
    FILTER( lang( ?label ) = "en" )
  }

## See dbpedia live results.

Basic Geo Lookups: Distance Variant II

## "Find Distance between New York City
## and London, England":

SELECT ( bif:st_distance( ?nyl,?ll ) )
      AS ?distanceBetweenNewYorkCityAndLondon
WHERE
  {
    <http://dbpedia.org/resource/New_York_City>
        geo:geometry ?nyl .
    <http://dbpedia.org/resource/London>
        geo:geometry ?ll  .
  }

## See dbpedia live results.

Basic Geo Lookups: Distance Variant III

## "Find Distance between New York City
## and London, England -- Extended Variant":

SELECT ?nylLabel
               ?nyl
               ?lnLabel
               ?ln ( bif:round ( bif:st_distance ( ?nyl, ?ln ) ) )
                    AS ?distanceBetweenNewYorkCityAndLondon
WHERE
  {
    <http://dbpedia.org/resource/New_York_City> geo:geometry ?nyl .
    <http://dbpedia.org/resource/London>             geo:geometry ?ln .
    <http://dbpedia.org/resource/New_York_City> rdfs:label        ?nylLabel .
    <http://dbpedia.org/resource/London>             rdfs:label        ?lnLabel
  }

## See dbpedia live results.

Basic Geo Lookups: Distance Variant IV

## "Find Objects around London with Populated Places in close proximity 10 km":

SELECT DISTINCT ?m ( bif:round ( bif:st_distance ( ?geo, ?gm ) ) )
WHERE
  {
    <http://dbpedia.org/resource/London> geo:geometry ?gm .
    ?m geo:geometry ?geo .
    ?m a <http://dbpedia.org/ontology/PopulatedPlace> .
    FILTER ( bif:st_intersects ( ?geo, ?gm,10 ) )
  }
ORDER BY DESC 2
LIMIT 50

## See dbpedia live results.

Basic Geo Lookups: Distance Variant V

## "Get Universities Around Paris":

SELECT ?m (bif:st_distance (?geo, bif:st_point (2.3498,48.853)))
WHERE
  {
    ?m geo:geometry ?geo .
    ?m a <http://umbel.org/umbel/rc/University> .
    FILTER (bif:st_intersects (?geo, bif:st_point (2.3498,48.853), 5))
    }
ORDER BY desc 2
LIMIT 20


## See dbpedia live results.

Basic Geo Lookups: Distance Variant VI

## "Stuff around Notre Dame de Paris":

SELECT *
WHERE
  {
    ?s geo:geometry ?geo .
    OPTIONAL { ?s foaf:name ?cn } .
    OPTIONAL { ?s geo:lat ?lat } .
    OPTIONAL { ?s geo:long ?long }.
    FILTER (bif:st_intersects (?geo, bif:st_point (2.3498, 48.853), 5)) .
  }
LIMIT 20

## See dbpedia live results.

Basic Geo Lookups: Distance Variant VII

## # Find Types of Things around Notre Dam de Paris -- Geo Variant":

SELECT ?c COUNT (*)
WHERE
  {
    ?s geo:geometry ?geo .
    FILTER (bif:st_intersects (?geo, bif:st_point (2.3498, 48.853), 5)) .
    ?s a ?c
  }
GROUP BY ?c
ORDER BY desc 2
LIMIT 50

## See dbpedia live results.

Geo Spatial: ROUND

## "Villages within 30 km proximity of London":

SELECT ?m (bif:round(bif:st_distance (?geo, ?gm)))
WHERE
  {
    <http://dbpedia.org/resource/London> geo:geometry ?gm .
    ?m geo:geometry ?geo .
    ?m a <http://umbel.org/umbel/rc/Village> .
    FILTER (bif:st_intersects (?geo, ?gm, 30))
  }
ORDER BY DESC 2
LIMIT 20

## See dbpedia live results.

Geo Spatial: DESCRIBE Variant I

## "Geo Spatial "DESCRIBE usage:
## Describe places within 5 km of Paris,
## that have railway stations opened in 1903
## in close proximity(0.2 km)":

DESCRIBE ?place ?station
WHERE
  {
    ?place a <http://schema.org/Place> .
    ?place foaf:name ?placename .
    ?place geo:geometry ?placegeo .
    ?station a <http://dbpedia.org/class/yago/RailwayStationsOpenedIn1903> .
    ?station geo:geometry ?stationgeo .
    ?station foaf:name ?stationname .
    ?station geo:lat ?lat.
    ?station geo:long ?long.
    FILTER ( bif:st_intersects ( ?placegeo, bif:st_point ( 2.3498, 48.853 ), 5 )
                      && bif:st_intersects ( ?stationgeo, ?placegeo, 0.2 ) )
  }
LIMIT 100

Geo Spatial: DESCRIBE Variant II

## "Geo Spatial "DESCRIBE usage:
## Stuff around Notre Dame de Paris":

DESCRIBE ?s
WHERE
  {
    ?s geo:geometry ?geo .
    ?s geo:lat ?lat.
    ?s geo:long ?long.
    FILTER (bif:st_intersects (?geo, bif:st_point (2.3498, 48.853), 0.3))
  }
LIMIT 20

Geo Spatial: DISTINCT

## "Geo Spatial "DISTINCT usage:
## Describe places within 5 km of Paris,
## that have railway stations opened in 1903
## in close proximity(10 km)":

SELECT DISTINCT ?place ?lat ?long ?stationname ?placename
(bif:round(bif:st_distance (?placegeo, ?stationgeo)))
WHERE
  {
    ?place a <http://schema.org/Place> .
    ?place foaf:name ?placename .
    ?place geo:geometry ?placegeo .
    ?station a <http://dbpedia.org/class/yago/RailwayStationsOpenedIn1903> .
    ?station geo:geometry ?stationgeo .
    ?station foaf:name ?stationname .
    ?station geo:lat ?lat.
    ?station geo:long ?long.
    FILTER ( bif:st_intersects ( ?placegeo, bif:st_point ( 2.3498, 48.853 ), 5 )
                      && bif:st_intersects ( ?stationgeo, ?placegeo, 10 ) )
  }
LIMIT 100

## See dbpedia live results.

Geo Spatial: CONSTRUCT

## "Geo Spatial "CONSTRUCT a custom
## Linked Data Mesh (graph) about: Places with
## Railway Stations opened in 1903,
## near Paris in close proximity(0.2 km)":

CONSTRUCT
  {
    ?station geo:geometry ?stationgeo  ;
                foaf:name ?stationname .
    ?place geo:geometry ?placegeo      ;
           foaf:name ?placename        .
  }
WHERE
  {
    ?place a <http://schema.org/Place> .
    ?place geo:geometry ?placegeo .
    ?place foaf:name ?placename .
    ?station a <http://dbpedia.org/class/yago/RailwayStationsOpenedIn1903> .
    ?station geo:geometry ?stationgeo .
    ?station foaf:name ?stationname .
    FILTER ( bif:st_intersects (?placegeo, bif:st_point (2.3498, 48.853), 5)
                      && bif:st_intersects (?stationgeo, ?placegeo, 0.2) )

Geo Spatial: ASK

## "Asks for Places with Railway Stations opened
## in 1903 near Paris in close proximity(0.2 km)":

ASK
WHERE
  {
    ?place a <http://schema.org/Place> .
    ?place geo:geometry ?placegeo .
    ?place rdfs:label ?placename .
    ?station a <http://dbpedia.org/class/yago/RailwayStationsOpenedIn1903> .
    ?station rdfs:label ?stationname .
    ?station geo:geometry ?stationgeo .
    ?station geo:lat ?lat .
    ?station geo:long ?long .
    FILTER ( bif:st_intersects ( ?placegeo, bif:st_point ( 2.3498, 48.853 ), 5 )
                      && bif:st_intersects ( ?stationgeo, ?placegeo, 0.2 ) )
  }

## See dbpedia live results.

Geo Spatial: UNION

## Motorways across England & Scotland from DBpedia:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpprop: <http://dbpedia.org/property/>
PREFIX yago: <http://dbpedia.org/class/yago/>

SELECT ?road ?services ?lat ?long
WHERE
  {
    {
      ?services dbpprop:road ?road .
      ?road a yago:MotorwaysInEngland .
      ?services dbpprop:lat ?lat .
      ?services dbpprop:long ?long .
    }
    UNION
    {
      ?services dbpprop:road ?road .
      ?road a yago:MotorwaysInScotland .
      ?services dbpprop:lat ?lat .
      ?services dbpprop:long ?long .
    }
  }
LIMIT 20

## See dbpedia live results.

Geo Spatial: COUNT Variant I

## "COUNT: Find big cities
## associated with a lot of GeoSpatial items":

SELECT ?s ?p COUNT (*)
WHERE
  {
    ?s <http://dbpedia.org/ontology/populationTotal> ?p .
    FILTER ( ?p > 6000000 ).
    ?s geo:geometry ?geo .
    FILTER ( bif:st_intersects (?pt, ?geo,2) ) .
    ?xx geo:geometry ?pt
  }
GROUP BY ?s ?p
ORDER BY DESC 3
LIMIT 20

## See dbpedia live results.

Geo Spatial: COUNT Variant II

## "COUNT: Get Stuff Around English Football Clubs":

SELECT ?m COUNT (*)
WHERE
  {
    ?m a <http://dbpedia.org/class/yago/EnglishFootballCluBs> .
    ?m geo:geometry ?geo .
    FILTER (bif:st_intersects (?geo, bif:st_point (0, 52), 100))
  }

## See dbpedia live results.

Geo Spatial: COUNT Variant III

## "COUNT: Get All Stuff For Given Coordinates":

SELECT ?c COUNT (*)
WHERE
  {
    ?m geo:geometry ?geo . ?m a ?c .
    FILTER (bif:st_intersects (?geo, bif:st_point (0, 52), 100))
  }
GROUP BY ?c
ORDER BY DESC 2


## See dbpedia live results.

Geo Spatial: COUNT Variant IV

## "COUNT: Things within 10 km proximity of Museums In Paris":

SELECT ?c COUNT (*)
WHERE
  {
    ?s a ?c .
    ?s a <http://dbpedia.org/class/yago/MuseumsInParis> .
    ?s geo:geometry ?geo .
    FILTER (bif:st_intersects (?geo, bif:st_point (2.3498, 48.853), 10))
  }
GROUP BY ?c
ORDER BY DESC 2
LIMIT 200

## See dbpedia live results.

Geo Spatial: FILTER

## "FILTER: Find places within 5 km of Paris,
## that have railway stations opened in 1903
## in close proximity(10 km)":

SELECT ?placename ?stationname (bif:st_distance (?placegeo, ?stationgeo))
WHERE
  {
    ?place a <http://schema.org/Place> .
    ?place foaf:name ?placename .
    ?place geo:geometry ?placegeo .
    ?station a <http://dbpedia.org/class/yago/RailwayStationsOpenedIn1903> .
    ?station geo:geometry ?stationgeo .
    ?station foaf:name ?stationname .
    FILTER ( bif:st_intersects ( ?placegeo, bif:st_point ( 2.3498, 48.853 ), 30 )
                       && bif:st_intersects ( ?stationgeo, ?placegeo, 10) )
    }
LIMIT 100

## See dbpedia live results.

Geo Spatial: Regular Expression Variant I

## "Geo Spatial "Regular Expression usage:
## Describe cities around London":

SELECT DISTINCT ?m (bif:st_distance (?geo, bif:st_point (0, 52)))
WHERE
  {
    ?m ?p ?c .
    ?m geo:geometry ?geo .
    ?m a <http://dbpedia.org/ontology/City> .
    FILTER ( bif:st_intersects ( ?geo, bif:st_point (0, 52), 100 )
                      && REGEX (str (?c), "London" ) )
  }
ORDER BY 2
LIMIT 20

## See dbpedia live results.

Geo Spatial: Regular Expression Variant II

## "Geo Spatial "Regular Expression usage:
## Describe places around London":

SELECT DISTINCT ?m (bif:st_distance (?geo, bif:st_point (0, 52)))
WHERE
  {
    ?m ?p ?c .
    ?m geo:geometry ?geo .
    ?m a <http://dbpedia.org/ontology/PopulatedPlace> .
    FILTER ( bif:st_intersects ( ?geo, bif:st_point (0, 52), 10)
                      && REGEX (str (?c), "London" ) ) .
  }
ORDER BY 2
LIMIT 20

## See dbpedia live results.

Geo Spatial: GROUP BY

## "Geo Spatial "GROUP BY usage:
## Get Stuff Around Notre Dame De Paris":

SELECT ?c COUNT (*)
WHERE
  {
    ?s a ?c .
    ?s geo:geometry ?geo .
    FILTER (bif:st_intersects (?geo, bif:st_point (2.3498, 48.853), 0.3))
  }
GROUP BY ?c
ORDER BY DESC 2
LIMIT 20

## See dbpedia live results.

Geo Spatial: ORDER BY

## "Geo Spatial "ORDER BY usage:
## Get Stuff Around Notre Dame De Paris
## with Populated Places":

SELECT ?c COUNT (*)
WHERE
  {
    ?s a ?c .
    ?s a <http://dbpedia.org/ontology/PopulatedPlace> .
    ?s geo:geometry ?geo .
    FILTER (bif:st_intersects (?geo, bif:st_point (2.3498, 48.853), 6))
   }
GROUP BY ?c
ORDER BY DESC 2
LIMIT 200

## See dbpedia live results.

Geo Spatial: LIMIT

## "Geo Spatial "LIMIT usage:
## Get Stuff Around Notre Dame De Paris with Names":

SELECT ?c COUNT (*)
WHERE
  {
    ?s a ?c .
    ?s a <http://dbpedia.org/ontology/PopulatedPlace> .
    ?s geo:geometry ?geo .
    FILTER (bif:st_intersects (?geo, bif:st_point (2.3498, 48.853), 6))
  }
GROUP BY ?c
ORDER BY DESC 2
LIMIT 200

## See dbpedia live results.

Querying Time and Space: Variant I

## "Find "All Educational Institutions
## within 10km of Oxford, UK; ordered by
## date of establishment":

SELECT DISTINCT ?thing AS ?uri
      ?thingLabel AS ?name
      ?date AS ?established
      ?matchgeo AS ?location
WHERE
  {
    <http://dbpedia.org/resource/Oxford> geo:geometry ?sourcegeo .
    ?resource geo:geometry ?matchgeo .
    FILTER( bif:st_intersects( ?matchgeo, ?sourcegeo, 5 ) ) .
    ?thing ?somelink ?resource .
    ?thing <http://dbpedia.org/ontology/established> ?date .
    ?thing rdfs:label ?thingLabel .
    FILTER( lang( ?thingLabel ) = "en" )
  }
ORDER BY ASC( ?date )

## See dbpedia live results.

Querying Time and Space: Variant II

## "Find "All Educational Institutions
## within 10km of Oxford, UK; ordered by
## date of establishment -- Extended Variant":

SELECT DISTINCT ?thing AS ?uri
            ?thingLabel AS ?name
            ?date AS ?established
            ?lat
            ?long
WHERE
  {
    <http://dbpedia.org/resource/Oxford> geo:geometry ?sourcegeo .
    ?resource geo:geometry ?matchgeo .
    ?resource geo:lat ?lat .
    ?resource geo:long ?long .
    FILTER ( bif:st_intersects ( ?matchgeo, ?sourcegeo, 5 ) ) .
    ?thing ?somelink ?resource .
    ?thing <http://dbpedia.org/property/established> ?date .
    ?thing rdfs:label ?thingLabel .
    FILTER ( lang ( ?thingLabel ) = "en" )
    }
ORDER BY asc( ?date )

## See dbpedia live results.

Querying Time and Space: Variant III

## "Find Historical cross section of events related
## to Edinburgh and the surrounding area (within 30km)
## during the 19th century":

SELECT DISTINCT ?thing ?thingLabel ?dateMeaningLabel ?date ?matchGEO
WHERE
  {
    {
      SELECT DISTINCT ?thing ?matchGEO
      WHERE
        {
          <http://dbpedia.org/resource/Edinburgh> geo:geometry ?sourceGEO .
          ?resource geo:geometry ?matchGEO .
          FILTER( bif:st_intersects( ?matchGEO, ?sourceGEO, 30 ) ) .
          ?thing ?somelink ?resource
        }
    }
    {
      ?property rdf:type owl:DatatypeProperty ;
                rdfs:range xsd:date
    } .
    ?thing ?dateMeaning ?date .
    FILTER( ?dateMeaning in ( ?property ) ) .
    FILTER( ?date >= xsd:gYear("1800") && ?date <= xsd:gYear("1900") )
    ?dateMeaning rdfs:label ?dateMeaningLabel .
    FILTER( lang(?dateMeaningLabel) = "en" ) .
    ?thing rdfs:label ?thingLabel .
    FILTER( lang(?thingLabel) = "en" )
  }
ORDER BY asc( ?date )

## See dbpedia live results.

Geo SPARQL Geometry: Box

## Counts the number of items of each type
## whose coordinates fall within a bounded
## Geometry box shape: BOX

SELECT ?f as ?facet COUNT(?s) as ?cnt
FROM <http://linkedgeodata.org>
WHERE
  {
    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?f .
    ?s <http://www.w3.org/2003/01/geo/wgs84_pos#geometry> ?p .
    FILTER
     (
      bif:st_intersects
       (
        bif:st_geomfromtext
         (
          "BOX(0.3412 43.5141, 9.3412 48.0141)"
         ),
         ?p
       )
     )
  }
GROUP BY ?f
ORDER BY DESC(?cnt)
LIMIT 10

## See LOD Cloud live results.

Geo SPARQL Geometry: POLYGON

## Counts the number of items of each type
## whose coordinates fall within a bounded
## Geometry box shape: POLYGON

SELECT ?f as ?facet COUNT(?s) as ?cnt
FROM <http://linkedgeodata.org>
WHERE
  {
    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?f .
    ?s <http://www.w3.org/2003/01/geo/wgs84_pos#geometry> ?p .
    FILTER
     (
      bif:st_intersects
       (
        bif:st_geomfromtext
         (
          "POLYGON((1 2, 6 1, 9 3, 8 5, 3 6, 1 2))"
         ),
         ?p
       )
     )
  }
GROUP BY ?f
ORDER BY DESC(?cnt)
LIMIT 10

## See LOD Cloud live results.

Geo SPARQL Geometry: POLYGON WITH HOLE

## Counts the number of items of each type
## whose coordinates fall within a bounded
## Geometry box shape: POLYGON WITH HOLE

SELECT ?f as ?facet COUNT(?s) as ?cnt
FROM <http://linkedgeodata.org>
WHERE
  {
    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?f .
    ?s <http://www.w3.org/2003/01/geo/wgs84_pos#geometry> ?p .
    FILTER
     (
      bif:st_intersects
       (
        bif:st_geomfromtext
         (
          "POLYGON((1 2, 6 1, 9 3, 8 5, 3 6, 1 2), (3 3, 5 5, 6 2, 3 3))"
         ),
         ?p
       )
     )
  }
GROUP BY ?f
ORDER BY DESC(?cnt)
LIMIT 10

## See LOD Cloud live results.

Geo SPARQL Geometry: MULTIPOLYGON

## Counts the number of items of each type
## whose coordinates fall within a bounded
## Geometry box shape: MULTIPOLYGON

SELECT ?f as ?facet COUNT(?s) as ?cnt
FROM <http://linkedgeodata.org>
WHERE
  {
    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?f .
    ?s <http://www.w3.org/2003/01/geo/wgs84_pos#geometry> ?p .
    FILTER
     (
      bif:st_intersects
       (
        bif:st_geomfromtext
         (
          "MULTIPOLYGON(((1 2, 6 1, 9 3, 3 6, 1 2)), ((4 9, 7 6, 9 8, 4 9)))"
         ),
         ?p
       )
     )
  }
GROUP BY ?f
ORDER BY DESC(?cnt)
LIMIT 10

## See LOD Cloud live results.

Geo SPARQL Geometry: GEOMETRY COLLECTION

## Counts the number of items of each type
## whose coordinates fall within a bounded
## Geometry box shape: GEOMETRY COLLECTION

SELECT ?f as ?facet COUNT(?s) as ?cnt
FROM <http://linkedgeodata.org>
WHERE
  {
    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?f .
    ?s <http://www.w3.org/2003/01/geo/wgs84_pos#geometry> ?p .
    FILTER
     (
      bif:st_intersects
       (
        bif:st_geomfromtext
         (
          "GEOMETRYCOLLECTION( POINT(4 5), POINT(7 4), POINT(6 2), LINESTRING(4 5, 6 7, 7 4, 6 2), POLYGON((1 2, 6 1, 9 3, 8 5, 3 6, 1 2)) )"
         ),
         ?p
       )
     )
  }
GROUP BY ?f
ORDER BY DESC(?cnt)
LIMIT 10

## See LOD Cloud live results.

Geo SPARQL Geometry: MULTIPOINT

## Counts the number of items of each type
## whose coordinates fall within a bounded
## Geometry box shape: MULTIPOINT

SELECT ?f as ?facet COUNT(?s) as ?cnt
FROM <http://linkedgeodata.org>
WHERE
  {
    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?f .
    ?s <http://www.w3.org/2003/01/geo/wgs84_pos#geometry> ?p .
    FILTER
     (
      bif:st_intersects
       (
        bif:st_geomfromtext
         (
          "MULTIPOINT(3 7, 4 2, 8 6)"
         ),
         ?p
       )
     )
  }
GROUP BY ?f
ORDER BY DESC(?cnt)
LIMIT 10

## See LOD Cloud live results.

Geo SPARQL Geometry: LINESTRING

## Counts the number of items of each type
## whose coordinates fall within a bounded
## Geometry box shape: LINESTRING

SELECT ?f as ?facet COUNT(?s) as ?cnt
FROM <http://linkedgeodata.org>
WHERE
  {
    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?f .
    ?s <http://www.w3.org/2003/01/geo/wgs84_pos#geometry> ?p .
    FILTER
     (
      bif:st_intersects
       (
        bif:st_geomfromtext
         (
          "LINESTRING(1 2, 3 6, 9 4)"
         ),
         ?p
       )
     )
  }
GROUP BY ?f
ORDER BY DESC(?cnt)
LIMIT 10

## See LOD Cloud live results.

Geo SPARQL Geometry: MULTILINESTRING

## Counts the number of items of each type
## whose coordinates fall within a bounded
## Geometry box shape: MULTILINESTRING

SELECT ?f as ?facet COUNT(?s) as ?cnt
FROM <http://linkedgeodata.org>
WHERE
  {
    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?f .
    ?s <http://www.w3.org/2003/01/geo/wgs84_pos#geometry> ?p .
    FILTER
     (
      bif:st_intersects
       (
        bif:st_geomfromtext
         (
          "MULTILINESTRING((1 8, 4 4), (4 9, 8 5, 6 2, 1 4))"
         ),
         ?p
       )
     )
  }
GROUP BY ?f
ORDER BY DESC(?cnt)
LIMIT 10

## See LOD Cloud live results.

RDF
SPARQL
SQL
OpenLink
SIOC
WG
SPARUL
ARQ
XQuery
XPath
URI
Vocabulary
IFP
RIF
RDFS
OWL
RDBMS
ODBC
JDBC
GET
POST
QL