SPARQL vs Overpass QL examples

This page compares basic querying tasks between 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.

SPARQLOverpass QLOverpass turbo query wizard
#defaultView:Map
SELECT * WHERE {
  ?osmId osmm:type "n" ;
         osmt:office "education" ;
         osmm:loc ?loc .
}
Run it (edit query)
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=*.

SPARQLOverpass QLOverpass turbo query wizard
#defaultView:Map
SELECT * WHERE {
  VALUES ?types { "n" "w" }
  ?osmId osmm:type ?types;
         osmt:crane:type ?key;
         osmm:loc ?loc.
}
Run it (edit query)
(
  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.

SPARQLOverpass QLOverpass turbo query wizard
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:office "education" ;
         osmm:loc ?loc .
}
Run it (edit query)
[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 .

SPARQLOverpass QLOverpass turbo query wizard
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:highway "primary" ;
         osmt:junction "roundabout" ;
         osmt:lanes "3" ;
         osmm:loc ?loc .
}
Run it (edit query)
[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=*.

SPARQLOverpass QLOverpass turbo query wizard
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:office "education" ;
         osmm:loc ?loc .
  
  FILTER NOT EXISTS {
    ?osmId osmt:building [].
  }
}
Run it (edit query)
[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.

SPARQLOverpass QLOverpass turbo query wizard
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:office "education" ;
         osmm:loc ?loc .
  
  FILTER NOT EXISTS {
    ?osmId osmt:building "yes".
  }
}
Run it (edit query)
[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.

SPARQLOverpass QLOverpass turbo query wizard
#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)
[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.

SPARQLOverpass QLOverpass turbo query wizard
#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)
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.

SPARQLOverpass QLOverpass turbo query wizard
#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)
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.

SPARQLOverpass QLOverpass turbo query wizard
#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)
[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.

SPARQLOverpass QLOverpass turbo query wizard
#defaultView:Map
SELECT * WHERE {
  {
    ?osm osmt:office "education" ; 
  } UNION { 
    ?osm osmt:sport "pilates" . 
  }
  ?osm osmm:loc ?loc .
}
Run it (edit query)
(
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).

SPARQLOverpass QLOverpass turbo query wizard
#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)
(
  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

SPARQLOverpass QLOverpass turbo query wizard
#defaultView:Map
SELECT * WHERE {
  ?relation osmt:type "connectivity";
            osmm:type "r";
            osmm:has ?member;
            ?member "through".
}
Run it (edit query)
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.

SPARQLOverpass QLOverpass turbo query wizard
#defaultView:Map
SELECT * WHERE {
  ?osmId osmt:office "education" ;
         osmm:user "user_name";
         osmm:loc ?loc .
}
Run it (edit query)
[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.

SPARQLOverpass QLOverpass turbo query wizard
#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)
[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

This article is issued from Openstreetmap. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.