SPARQL vs Overpass QL examples

From OpenStreetMap Wiki
Jump to navigation Jump to search

This page compares basic querying tasks between QLever and Sophox's SPARQL language and the Overpass API's Overpass QL.

At a high level, Sophox is best suited for querying globally, while the Overpass API is best suited for querying locally or regionally. It is very easy to run into a timeout or out-of-memory error when performing a regional query using Sophox or performing a global query using the Overpass API. The Overpass QL examples below all specify a specific bounding box or the currently viewed bounding box; it is possible but less common to omit the ({{bbox}}) filter to perform a global query. Likewise, you can use the wikibase:box service to limit a SPARQL query to a bounding box.

Nodes with a given tag

Displays an interactive map of nodes tagged office=education.

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osm: <https://www.openstreetmap.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT * WHERE {
  ?osmId rdf:type osm:node;
         osmkey:office "education" ;
         geo:hasGeometry/geo:asWKT ?loc .
}
Run it (edit query)
#defaultView:Map
SELECT * WHERE {
  ?osmId osmm:type "n" ;
         osmt:office "education" ;
         osmm:loc ?loc .
}
Run it (edit query)
try it yourself in overpass-turbo
node["office"="education"]({{bbox}});

out body;
>;
out skel qt;
office=education and type:node

Nodes and ways with a given key

Displays an interactive map of nodes and ways tagged with key crane:type=*.

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX osm: <https://www.openstreetmap.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT * WHERE {
  VALUES ?types { osm:node osm:way }
  ?osmId rdf:type ?types;
         osmkey:crane:type ?key;
         geo:hasGeometry/geo:asWKT ?loc.
}
Run it (edit query)
#defaultView:Map
SELECT * WHERE {
  VALUES ?types { "n" "w" }
  ?osmId osmm:type ?types;
         osmt:crane:type ?key;
         osmm:loc ?loc.
}
Run it (edit query)
try it yourself in overpass-turbo
(
  nw ["crane:type"]({{bbox}});
);

out body;
>;
out skel qt;
crane:type=* and type:node or crane:type=* and type:way

Features with a given tag

Displays an interactive map of nodes, ways, and relations that are tagged office=education.

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT * WHERE {
  ?osmId osmkey:office "education" ;
         geo:hasGeometry/geo:asWKT ?loc .
}
Run it (edit query)
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:office "education" ;
         osmm:loc ?loc .
}
Run it (edit query)
try it yourself in overpass-turbo
[out:json][timeout:25];
// gather results
(
  nwr["office"="education"]({{bbox}});
);
// print results
out body;
>;
out skel qt;
office=education

Features with a couple of given tags

Displays an interactive map of nodes, ways, and relations that are tagged highway=primary + junction=roundabout + lanes=3 .

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT * WHERE {
  ?osmId osmkey:highway "primary" ;
         osmkey:junction "roundabout" ;
         osmkey:lanes "3" ;
         geo:hasGeometry/geo:asWKT ?loc .
}
Run it (edit query)
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:highway "primary" ;
         osmt:junction "roundabout" ;
         osmt:lanes "3" ;
         osmm:loc ?loc .
}
Run it (edit query)
try it yourself in overpass-turbo
[out:json][timeout:25];
// gather results
(
  nwr["highway"="primary"]["junction"="roundabout"]["lanes"="3"]({{bbox}});
);
// print results
out body;
>;
out skel qt;
highway=primary and junction=roundabout and lanes=3

Features with one tag but not another key

Displays an interactive map of nodes, ways, and relations that are tagged office=education but not building=*.

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT * WHERE {
  ?osmId osmkey:office "education" ;
         geo:hasGeometry/geo:asWKT ?loc .
  MINUS { ?osmId osmkey:building [] . }
}
Run it (edit query)
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:office "education" ;
         osmm:loc ?loc .
  
  FILTER NOT EXISTS {
    ?osmId osmt:building [].
  }
}
Run it (edit query)
try it yourself in overpass-turbo
[out:json][timeout:25];
// gather results
(
  nwr["office"="education"][!"building"]({{bbox}});
);
// print results
out body;
>;
out skel qt;
office=education and building is null
or

office=education and building!=*

Features with one tag but not another tag

Displays an interactive map of nodes, ways, and relations that are tagged office=education but not building=yes.

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT * WHERE {
  ?osmId osmkey:office "education" ;
         geo:hasGeometry/geo:asWKT ?loc .
  MINUS { ?osmId osmkey:building "yes" . }
}
Run it (edit query)
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:office "education" ;
         osmm:loc ?loc .
  
  FILTER NOT EXISTS {
    ?osmId osmt:building "yes".
  }
}
Run it (edit query)
try it yourself in overpass-turbo
[out:json][timeout:25];
// gather results
(
  nwr["office"="education"]["building"!="yes"]({{bbox}});
);
// print results
out body;
>;
out skel qt;
office=education and building!=yes

Features with some tags but not another tags

Displays an interactive map of nodes, ways, and relations that are tagged office=education but not building=yes.

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT * WHERE {
  ?osmId osmkey:lanes "1" ;
         osmkey:highway "primary" ;
         geo:hasGeometry/geo:asWKT ?loc .
  MINUS { ?osmId osmkey:oneway "yes" . }
  MINUS { ?osmId osmkey:junction "roundabout" . }
}
Run it (edit query)
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:lanes "1" ;
         osmt:highway "primary" ;
         osmm:loc ?loc .
  
  FILTER NOT EXISTS {
    ?osmId osmt:oneway "yes".
  }
  FILTER NOT EXISTS {
    ?osmId osmt:junction "roundabout".
  }
}
Run it (edit query)
try it yourself in overpass-turbo
[out:json][timeout:25];
// gather results
(
  nwr["lanes"="1"]["highway"="primary"]["oneway"!="yes"]["junction"!="roundabout"]({{bbox}});
);
// print results
out body;
>;
out skel qt;
lanes=1 and highway=primary and oneway!=yes and junction!=roundabout

Features within a specific bounding box

Displays an interactive map of nodes, ways, and relations that are tagged leisure=pitch but not sport=* and that are located within a specific bounding box encompassing the Dallas area. You can use a tool like bboxfinder or geojson.io to calculate a bounding box.

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
SELECT * WHERE {
  ?osmId osmkey:leisure "pitch" ;
         geo:hasGeometry/geo:asWKT ?loc.
  MINUS { ?osmId osmkey:sport [] . }
  BIND(geof:latitude(?loc) AS ?lat)
  BIND(geof:longitude(?loc) AS ?lon)
  FILTER (?lat > -97.00 && ?lat < -96.60 && ?lon > 32.50 && ?lon < 33.00)
}
Run it (edit query)
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:leisure "pitch".
  
  # Filter bbox
  SERVICE wikibase:box {
    ?osmId osmm:loc ?coordinates .
    bd:serviceParam wikibase:cornerSouthWest 'Point(-97.00 32.50)'^^geo:wktLiteral.
    bd:serviceParam wikibase:cornerNorthEast 'Point(-96.60 33.00)'^^geo:wktLiteral.
  }
  
  FILTER NOT EXISTS {
    ?osmId osmt:sport [].
  }
}
Run it (edit query)
try it yourself in overpass-turbo
nwr["leisure"="pitch"][!"sport"](32.5,-97.0,33.0,-96.6); 

out body; 
>; 
out skel qt;
leisure=pitch and sport is null (then click the picture frame icon to choose the bounding box)

Features within a radius of a specific coordinate

Displays an interactive map of nodes, ways, and relations tagged leisure=pitch but not sport=* that fall within 300 kilometres (190 mi) of a specific coordinate, approximating Suriname.

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
SELECT * WHERE {
  ?osmId osmkey:leisure "pitch" ;
         geo:hasGeometry/geo:asWKT ?loc.
  MINUS { ?osmId osmkey:sport [] . }
  BIND ("Point(-56.00 4.00)"^^geo:wktLiteral AS ?center)
  FILTER (geof:distance(?loc, ?center) <= 300)
}
Run it (edit query)
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:leisure "pitch" .
  
  SERVICE wikibase:around { 
    ?osmId osmm:loc ?coordinates .
    bd:serviceParam wikibase:center "Point(-56.00 4.00)"^^geo:wktLiteral .
    bd:serviceParam wikibase:radius "300" .
    bd:serviceParam wikibase:distance ?distance .
  }
  
  FILTER NOT EXISTS { 
    ?osmId osmt:sport [] .
  }
}
Run it (edit query)
try it yourself in overpass-turbo
nwr(around:300000,4.00,-56.00)["leisure"="pitch"][!"sport"];
 
out body;
>;
out skel qt;
leisure=pitch and sport is null around "4.00,-56.00"

Matching part of a tag value

Displays an interactive map of features tagged place=village whose name=* contains the substring or regular expression "View". SPARQL has functions for matching substrings or regular expressions, while OverpassQL only has a filter for regular expressions.

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT * WHERE {
  ?osmId osmkey:place "village" ;
         osmkey:name ?name ;
         geo:hasGeometry/geo:asWKT ?loc .
  FILTER CONTAINS(?name, "View")
}
Run it (edit query)
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:place "village" ;
         osmt:name ?name ;
         osmm:loc ?loc .
  FILTER CONTAINS(?name, "View")
  # Alternatively, match a regular expression
  # for consistency with the OverpassQL query
#  FILTER REGEX(?name, "View")
}
Run it (edit query)
try it yourself in overpass-turbo
[out:json][timeout:250];
(
  nwr["place"="village"]["name"~"View"];
);

out body;
>;
out skel qt;
place=village and name~View

Features with either one tag or another tag

Displays an interactive map of nodes, ways, and relations tagged office=education or sport=pilates or both.

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX osmkey: <https://www.openstreetmap.org/wiki/Key:>
SELECT * WHERE {
  {
    ?osm osmkey:office "education" .
  } UNION { 
    ?osm osmkey:sport "pilates" . 
  }
  ?osm geo:hasGeometry/geo:asWKT ?loc.
}
Run it (edit query)
#defaultView:Map
SELECT * WHERE {
  {
    ?osm osmt:office "education" ; 
  } UNION { 
    ?osm osmt:sport "pilates" . 
  }
  ?osm osmm:loc ?loc .
}
Run it (edit query)
try it yourself in overpass-turbo
(
nwr["office"="education"]({{bbox}}); 
nwr["sport"="pilates"]({{bbox}});
);
out body; 
>; 
out skel qt;
office=education or sport=pilates

Filter features by last-edited timestamp

Displays an interactive map of nodes, ways, and relations tagged leisure=* that were last edited on 10 August 2020 (UTC).

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
N/A
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:leisure ?leisure ;
         osmm:loc ?loc ;
         osmm:timestamp ?ts .

  # Filter by timestamp
  FILTER ("2020-08-10T00:00:00Z"^^xsd:dateTime < ?ts &&
          ?ts < "2020-08-11T00:00:00Z"^^xsd:dateTime)
}
Run it (edit query)
try it yourself in overpass-turbo
(
  nwr["leisure"](newer:"2020-08-10T00:00:00Z")({{bbox}});
  - nwr["leisure"](newer:"2020-08-11T00:00:00Z")({{bbox}});
);
out body; 
>; 
out skel qt;
N/A

Find relations which have a member with stated role name

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
N/A
#defaultView:Map
SELECT * WHERE {
  ?relation osmt:type "connectivity";
            osmm:type "r";
            osmm:has ?member;
            ?member "through".
}
Run it (edit query)
try it yourself in overpass-turbo
rel[connectivity](if:count_by_role("through") > 0);
(._;>;);
out ;
N/A

Features with a given tag touched from a specific user

Displays an interactive map of nodes, ways, and relations that are tagged with office=education by a specific user.

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
N/A
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:office "education" ;
         osmm:user "user_name";
         osmm:loc ?loc .
}
Run it (edit query)
try it yourself in overpass-turbo
[out:json][timeout:25];
// gather results
(
  nwr["office"="education"](user:"user_name")({{bbox}});
);
// print results
out meta;
>;
out meta qt;
office=education and user:user_name

Features with a given tag from a specific time period

Displays an interactive map of nodes, ways, and relations that are tagged within a specific period of time and with the tag office=education.

QLever SPARQL Sophox SPARQL Overpass QL Overpass turbo wizard
N/A
#defaultView:Map
SELECT * WHERE {
  ?leisure osmt:office "education" ;
           osmm:timestamp ?timestamp ;
           osmm:loc ?loc .
  
  # Filter by timestamp
  FILTER("2012-01-01"^^xsd:dateTime < ?timestamp && 
          ?timestamp < "2013-01-01"^^xsd:dateTime)
}
Run it (edit query)
try it yourself in overpass-turbo
[diff:"2012-02-01T00:00:00Z","2013-02-11T00:00:00Z"];

(
  nwr["office"="education"]({{bbox}});
);
// print results
out meta;
>;
out meta qt;
N/A

See also