Python bindings for the OpenStreetMap Overpass API
Python bindings for the OpenStreetMap Overpass API.
pip install overpass
First, create an API object.
import overpass api = overpass.API()
The API constructor takes several parameters, all optional:
The default endpoint is
https://overpass-api.de/api/interpreterbut you can pass in another instance:
api = overpass.API(endpoint="https://overpass.myserver/interpreter")
The default timeout is 25 seconds, but you can set it to whatever you want.
api = overpass.API(timeout=600)
Setting this to
Truewill get you debug output.
Most users will only ever need to use the
get()method. There are some convenience query methods for common queries as well, see below.
response = api.get('node["name"="Salt Lake City"]')
responsewill be a dictionary representing the JSON output you would get from the Overpass API directly.
Note that the Overpass query passed to
get()should not contain any
outor other meta statements. See
verbositybelow for how to control the output.
>>> print [( ... feature['properties']['name'], ... feature['id']) for feature in response["features"]] [(u'Salt Lake City', 150935219), (u'Salt Lake City', 585370637)]
You can find more examples in the
examples/directory of this repository.
get()method takes a few parameters, all optional having sensible defaults.
You can set the verbosity of the Overpass querydirective using the same keywords Overpass does. In order of increased verbosity:
meta. As is the case with the Overpass API itself,
bodyis the default.
>>> import overpass >>> api = overpass.API() >>> data = api.get('way(42.819,-73.881,42.820,-73.880);(._;>;)', verbosity='geom') >>> [f for f in data.features if f.geometry['type'] == "LineString"]
You can set the response type of your query using
responseformatparameter to GeoJSON (
geojson, the default), plain JSON (
json), CSV (
csv), and OSM XML (
response = api.get('node["name"="Salt Lake City"]', responseformat="xml")
We will construct a valid Overpass QL query from the parameters you set by default. This means you don't have to include 'meta' statements like
[out body], etcetera. You just supply the meat of the query, the part that actually tells Overpass what to query for. If for whatever reason you want to override this and supply a full, valid Overpass QL query, you can set
Falseto make the API not do any pre-processing.
You can query the data as it was on a given date. You can give either a standard ISO date alone (YYYY-MM-DD) or a full overpass date and time (YYYY-MM-DDTHH:MM:SSZ, i.e. 2020-04-28T00:00:00Z). You can also directly pass a
datetimeobject from the
In addition to just sending your query and parse the result,
overpassprovides shortcuts for often used map queries. To use them, just pass them like to normal query to the API.
This is a shorthand for a complete ways and relations query in a bounding box (the 'map call'). You just pass the bounding box to the constructor:
MapQuery = overpass.MapQuery(50.746,7.154,50.748,7.157) response = api.get(MapQuery)
This is shorthand for getting a set of ways and their child nodes that satisfy certain criteria. Pass the criteria as a Overpass QL stub to the constructor:
WayQuery = overpass.WayQuery('[name="Highway 51"]') response = api.get(WayQuery)
Create a new issue.
The command line tool was deprecated in version 0.4.0.
There are other python modules that do similar things.