Skip to content

Overpass Guide

maning edited this page Aug 8, 2016 · 4 revisions

Quick guide to extracting data from OSM using Overpass Turbo

Overpass Turbo provides an interactive environment for live querying the OpenStreetMap database and visualize the results on a map. Results can be exported as a GeoJSON or CSV for further analysis and visualizations for mapping projects.

Overpass is great for

  • Querying features by OSM tags
  • Looking for features that was edited within a time range.
  • Find features added by a particular user or group of users.
  • Create quick map visualizations of results using MapCSS

Overpass is not great for

  • Complex queries fetching a large volume of data (>20MB) usually fails.
  • Queries that span a really large area of the map.

For bulk data downloads, use extracts from OSM Planet, Geofabrik or Metro.

Overpass Query Syntax

Overpass API performs read-only queries to the OSM database, using the Overpass Query Language(QL).

screenshot 2016-06-20 18 15 15

  • The syntax is a sequence of statements, each statements ending in a semicolon ;
  • The statements define what attributes, metadata or filters to use.
  • Each statement can define what OSM objects to query for by using keywords node, way or relation.

Examples

screenshot 2016-06-20 18 35 01

In the above example, we are looking for all nodes, ways and relations edited by user:ramyaragupathy within the current map view. By default, Overpass Turbo uses the map view to define the bbox of the query.

Here are some common uses:

One common point in all the use cases above is that OSM features are specified in square brackets, e.g [highway], [name], [amenity=cafe] and the metadata is specified within round brackets, e.g (user:OSMusername),(changed:"2016-02-24T07:00:00Z","2016-03-01T07:01:00Z"), (boundarybox), (objectid)

A few queries related to mapping projects

Specify a custom bounding box

One of the most common search criteria is to fetch data within a bounding box, as in the following example:

[out:json][timeout:25];

(
  // query for data in a custom bounding box
  node(11.5,79.6,11.8,79.8);
  way(11.5,79.6,11.8,79.8);
  rel(11.5,79.6,11.8,79.8);
 
);

// print results
out body;
out meta;
>;
out skel qt;

The coordinates of the bounding box are in the sequence (South latitude, West longitude, North latitude, East longitude).

Specify an object id

With the numerical id of the desired node, way or relation, the details can be requested with a query:

[out:json][timeout:25];
// gather results
(
  relation(1685018); //fetch Lausanne city boundary
);
// print results
out body;
>;
out skel qt;

Specify particular tags

Ex: Search for all highways


[out:json][timeout:25];
// gather results
(
  way[highway](11.5,79.6,11.8,79.8); //fetches all highways within a given bounding box
);
// print results
out body;
>;
out skel qt;
Search for all highways without name tag

[out:json][timeout:25];
// gather results
(
  way[highway][name!~"."](11.5,79.6,11.8,79.8); //fetches all highways without a name tag
);
// print results
out body;
>;
out skel qt;

Search for a tag within a particular area

[out:json][timeout:240];
// fetch area “California” to search in
{{geocodeArea:California}}->.searchArea;
// gather results
(
  // query part for: “place=city” in California
  node["place"="city"](area.searchArea);
  way["place"="city"](area.searchArea);
  relation["place"="city"](area.searchArea);
);
// print results
out body;
>;
out skel qt;

Changes to a feature in a given time period

[out:xml][timeout:600];
// search for all highways edited between Jun-01 to Jun-30 in the current map view
(
  // time specified in UTC format
  way["highway"](changed:"2016-06-01T00:00:00Z","2016-06-30T11:59:59Z")({{bbox}});

);
out body;
>;
out skel qt;

Turn Lanes

Since we have custom polygons drawn for the cities in our list, our queries should also be run against the same boundary of work. All the below queries can be customized for use along with a rectangular bounding box or for current map view.

In case of polygons, overpass query should have the boundaries specified as (poly:lat1 long1 lat2 long2............latN longN lat1 long1). Coordinates (lat, long) at the beginning and at the end of the poly are the same signifying that it is a closed polygon. If there is a mismatch here, query fails with an error message.

In case of turn lanes, there are three different tags taken into account for stats purpose. They are,

  • turn:lanes
  • turn:lanes:forward
  • turn:lanes:backward

Instead of constructing a query for each tag, we are using regular expression to query for all tags that begins with "turn:lanes".

To construct a regular expression for a key-value pair, start with a tilde () for the key. Then specify the regular expression for the key. Here it is "^turn:lanes.*$". This is then followed by another tilde () character and a regular expression for the value.

Search for all turn lanes in a given polygon
[out:json][timeout:250];

(
  way[~"^turn:lanes.*$"~"."](poly:"37.56743975318767 -122.39250183105467 37.61967039695652 -122.34786987304686 37.640334898059486 -122.38426208496094 37.659362907485374 -122.37396240234375 37.68708070686609 -122.38426208496094 37.70772645289051 -122.39250183105467 37.70881291183666 -122.37258911132812 37.72836644908416 -122.354736328125 37.74791482485267 -122.37327575683594 37.76745803822967 -122.38700866699219 37.7897092979573 -122.38426208496094 37.811411388625636 -122.40623474121092 37.805986463750315 -122.46528625488281 37.811953859192705 -122.47901916503906 37.791879793952084 -122.48863220214842 37.78699608830537 -122.5147247314453 37.72293542866175 -122.51129150390625 37.67240786750202 -122.49755859375 37.60824807622547 -122.50030517578124 37.56743975318767 -122.39250183105467");

 
);

out body;
>;
out skel qt;

Search for all turn lanes edited by a mapper in a given polygon
[out:json][timeout:250];

(
  way[~"^turn:lanes.*$"~"."](user:"Jothirnadh")(poly:"37.56743975318767 -122.39250183105467 37.61967039695652 -122.34786987304686 37.640334898059486 -122.38426208496094 37.659362907485374 -122.37396240234375 37.68708070686609 -122.38426208496094 37.70772645289051 -122.39250183105467 37.70881291183666 -122.37258911132812 37.72836644908416 -122.354736328125 37.74791482485267 -122.37327575683594 37.76745803822967 -122.38700866699219 37.7897092979573 -122.38426208496094 37.811411388625636 -122.40623474121092 37.805986463750315 -122.46528625488281 37.811953859192705 -122.47901916503906 37.791879793952084 -122.48863220214842 37.78699608830537 -122.5147247314453 37.72293542866175 -122.51129150390625 37.67240786750202 -122.49755859375 37.60824807622547 -122.50030517578124 37.56743975318767 -122.39250183105467");

 
);

out body;
>;
out skel qt;
Search for all turn lanes edited by a mapper within a given time period in a given polygon
[out:json][timeout:250];

(
  way[~"^turn:lanes.*$"~"."](user:"Jothirnadh")(changed:"2016-05-01T00:00:00Z","2016-06-30T11:59:59Z")(poly:"37.56743975318767 -122.39250183105467 37.61967039695652 -122.34786987304686 37.640334898059486 -122.38426208496094 37.659362907485374 -122.37396240234375 37.68708070686609 -122.38426208496094 37.70772645289051 -122.39250183105467 37.70881291183666 -122.37258911132812 37.72836644908416 -122.354736328125 37.74791482485267 -122.37327575683594 37.76745803822967 -122.38700866699219 37.7897092979573 -122.38426208496094 37.811411388625636 -122.40623474121092 37.805986463750315 -122.46528625488281 37.811953859192705 -122.47901916503906 37.791879793952084 -122.48863220214842 37.78699608830537 -122.5147247314453 37.72293542866175 -122.51129150390625 37.67240786750202 -122.49755859375 37.60824807622547 -122.50030517578124 37.56743975318767 -122.39250183105467");

 
);

out body;
>;
out skel qt;

Turn restrictions

Number of turn restrictions in the current map view
[out:json][timeout:600];
// gather results
(
    relation[~"^restriction.*$"~"."]({{bbox}});
  
 
);
// print results
out body;
>;
out skel qt;
Number of conditional turn restrictions in the current map view

[out:json][timeout:25];
// gather results
(
  
  relation["restriction:conditional"]({{bbox}});
);
// print results
out body;
>;
out skel qt;

Search for turn restrictions edited by a mapper within a given time period in the current map view
[out:json][timeout:600];
// gather results
(
    relation[~"^restriction.*$"~"."](user:"oini")(changed:"2016-05-20T01:00:00Z","2016-06-10T11:00:00Z")({{bbox}});
);
// print results
out body;
>;
out skel qt;
Search for conditional turn restrictions edited by a mapper within a given time period in the current map view
[out:json][timeout:600];
// gather results
(
    relation["restriction:conditional"](user:"oini")(changed:"2016-05-20T01:00:00Z","2016-06-10T11:00:00Z")({{bbox}});
);
// print results
out body;
>;
out skel qt;
Clone this wiki locally