Async client for the Overpass API
Project description
A client for the Overpass API, a read-only API that serves up custom selected parts of OpenStreetMap data.
The Overpass API is optimized for data consumers that need a few elements within a glimpse or up to roughly 10 million elements in some minutes, both selected by search criteria like location, type of objects, tag properties, proximity, or combinations of them. To make use of it, you should familiarize yourself with Overpass QL, the query language used to select the elements that you want.
Contents
See also
- An overview of modules, classes and functions can be found in the API reference
- There are some notebooks to check out in examples/
- The version history is available in CHANGELOG.md
- Developers can find some instructions in CONTRIBUTING.md
- The Overpass API repository, its blog, its user's manual and its release notes
- Overpass Turbo to prototype your queries in your browser
Features
- Asynchronous requests using aiohttp
- Parallel queries within rate limits
- Fault tolerance through a (customizable) retry strategy
- Extensions
Design Goals
- A small and stable set of core functionality.
- Good defaults for queries and retrying.
- Room for extensions that simplify querying and/or processing of spatial data in specific problem domains.
- Sensible and spec-compliant GeoJSON exports for all objects that represent spatial features.
- Detailed documentation that supplements learning about OSM and the Overpass API.
Getting Started
pip install aio-overpass
pip install aio-overpass[shapely, networkx, joblib]
poetry add aio-overpass
poetry add aio-overpass[shapely, networkx, joblib]
Choosing Extras
This library can be installed with a number of optional extras.
-
Install no extras, if you're fine with
dict
result sets. -
Install the
shapely
extra, if you would like the convenience of typed OSM elements. It is also useful if you are interested in elements' geometries, and either already use Shapely, or want a simple way to export GeoJSON.- This includes the
pt
module to make it easier to interact with public transportation routes. Something seemingly trivial like listing the stops of a route can have unexpected pitfalls, since stops can have multiple route members, and may have a range of different tags and roles. This submodule will clean up the relation data for you.
- This includes the
-
Install the
networkx
extra to enable thept_ordered
module, if you want a route's path as a simple line from A to B. It is hard to do this consistently, mainly because ways are not always ordered, and stop positions might be missing. You can benefit from this submodule if you wish to- render a route's path between any two stops
- measure the route's travelled distance between any two stops
- validate the order of ways in the relation
- check if the route relation has gaps
-
Install the
joblib
extra to speed uppt_ordered.collect_ordered_routes()
, which can benefit greatly from parallelization.
Basic Usage
There are three basic steps to fetch the spatial data you need:
-
Formulate a query
- Either write your own custom query, f.e.
Query("node(5369192667); out;")
, - or use one of the
Query
subclasses, f.e.SingleRouteQuery(relation_id=1643324)
.
- Either write your own custom query, f.e.
-
Call the Overpass API
- Prepare your client with
client = Client(user_agent=...)
. - Use
await client.run_query(query)
to fetch the result set.
- Prepare your client with
-
Collect results
- Either access the raw result dictionaries with
query.result_set
, - or use a collector, f.e.
collect_elements(query)
to get a list of typedElements
. - Collectors are often specific to queries -
collect_routes
requires aRouteQuery
, for instance.
- Either access the raw result dictionaries with
Example
Results as Dictionaries
from aio_overpass import Client, Query
query = Query("way(24981342); out geom;")
client = Client()
await client.run_query(query)
query.result_set
[
{
"type": "way",
"id": 24981342,
# ...
"tags": {
"addr:city": "Hamburg",
"addr:country": "DE",
"addr:housename": "Elbphilharmonie",
# ...
},
}
]
Results as Objects
from aio_overpass.element import collect_elements
elems = collect_elements(query)
elems[0].tags
{
"addr:city": "Hamburg",
"addr:country": "DE",
"addr:housename": "Elbphilharmonie",
# ...
}
Results as GeoJSON
import json
json.dumps(elems[0].geojson, indent=4)
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
9.9832434,
53.5415472
],
...
]
]
},
"properties": {
"id": 24981342,
"type": "way",
"tags": {
"addr:city": "Hamburg",
"addr:country": "DE",
"addr:housename": "Elbphilharmonie",
...
},
...
},
"bbox": [
9.9832434,
53.540877,
9.9849674
53.5416212,
]
}
Coordinates
- Geographic point locations are expressed by latitude (
lat
) and longitude (lon
) coordinates.- Latitude is given as an angle that ranges from –90° at the south pole to 90° at the north pole, with 0° at the Equator.
- Longitude is given as an angle ranging from 0° at the Prime Meridian (the line that divides the globe into Eastern and Western hemispheres), to +180° eastward and −180° westward.
lat/lon
values arefloats
that are exactly those degrees, just without the ° sign.
- This might help you remember which coordinate is which:
- If you think of a world map, usually it’s a rectangle.
- The long side (the largest side) is the longitude.
- Longitude is the x-axis, and latitude is the y-axis.
- Be wary of coordinate order:
- OpenStreetMap uses the WGS84 spatial reference system used by the Global Positioning System (GPS).
- OpenStreetMap node coordinates have seven decimal places, which gives them centimetric precision. However, the position accuracy of GPS data is only about 10m. A reasonable display accuracy could be five places, which is precise to 1.1 metres at the equator.
- Spatial features that cross the 180th meridian are
problematic,
since you go from longitude
180.0
to-180.0
. Such features usually have their geometries split up, like the area of Russia.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for aio_overpass-0.12.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cba8c640c72f122b23d00ff6458a9f67139cbe58200a9e27a795e97c263cb29f |
|
MD5 | ff6951487df1b7b8dd1594756786e6d8 |
|
BLAKE2b-256 | 2a099d33e37be9516a00f8b3540d9edc61fc4e29bc074cb3ec7384c8a71d1402 |