Skip to main content

Python API for making OSM queries via Overpass

Project description

Chauffeur

Chauffeur was designed to provide an intiutive and easy to get started with building and making Overpass queries in Python. It is also built to empower users who are interested in using the OverpassQureryLanguage primitives to do things like data QA, data "gardening," and otherwise through the consruction of more complex and logical queries.

In building Chauffeur, attention was taken to creating interfaces and naming conventions that shed light on some of the less understood aspects of Overpass, as well as Open Street Map(OSM). The more we know about the kind of data out there on OSM, the better judements we can make when, eg, building QA metrics on results, as well as optimizing query requests, etc. Chauffeur is supposed to empower researchers to develop these kind of data intuitions and statistics by abstracting the Query Language itself, providing best practices as taken from Overpass, providing access to formatting and service params, and defining preconfigured objects for different use cases.

Getting Started

import chauffeur as cfr

The main entrypoint is the chauffeur.QueryBuilder object. It provides the interfaces for query construction, parameterization, and execution. Initialize a QueryBuilder to get started.

qb = cfr.QueryBuilder()

Request and Query Parameters

The settings interface let's a user define Overpass-specific request+response parameters. Examples include global bounding boxes, global date filters, server timeouts, and data limits, as well as response format. Chauffeur defaults to the Overpass default request params, which means a blank string. If the Overpass default params aren't optimal for your query, you can change them:

qb.settings.timeout = 3600 # Set the Overpass server timeout in seconds
qb.settings.maxsize = int(cfr.QuerySettings.MAXSIZE_LIMIT / 2) # Set the return payload to max 1GB
qb.settings.payload_format = 'json' # Return results in JSON format instead of default XML

These params can also be passed as keywords during QueryBuilder initialization.

The output_mode interface is meant to bridge the "print" concept in OverpassQL. The query output controls, or mode descriptors, are relevant to the way Overpass processes queries and returns information. The defaults are consistent with Overpass, but can be changed:

qb.output_mode.GEOMETRY = cfr.fmt.Geometry.CENTER_POINT
qb.output_mode.VERBOSITY = cfe.fmt.Verbosity.VERBOSE
print(qb.output_mode)

Defining Query Statements with Filters

Chaufeur provides various query filters accessible via the main chauffeur module. These are: chauffeur.UserFilter, chauffeur.IdFilter, chauffeur.BboxFilter, and chauffeur.TagFilter. The following demonstrates some of the ways the TagFilter can be used to search for OSM tags, which are defined as key-value pairs.

The following defines a filter for schools - namely, a tag with key "amenity" and value "school":

schools = cfr.TagFilter("amenity", "school")

This would filter elements that have an "amenity" key that includes the tag value "school". To extend the search to include other elements, you can include them as a list of values:

places_of_study = cfr.TagFilter("amenity", ["school", "university", "college"])

Instead of creating three different filters on the same tag key, chauffeur concatenates the values into a regex string, which is supported by Overpass and is considered a best practice.

We can match a tag key with any value whatsoever by excluding input vals:

any_amenity = cfr.TagFilter("amenity")

Filtering by non-existence is supported as well:

no_amenities = cfr.TagFilter("amenity", exists=False) # Disallow any object with "amenity" key and any values
amenities_no_learning = cfr.TagFilter("amenity", ["school", "university", "college"], exists=False) # Find amenities without educational tags

Features of interest are often characterized by multiple combinations of tag filter values. We can create compound tag filters through union:

railstations = cfr.TagFilter("railway", "station") + cfr.TagFilter("public_transport", "station")

Building and Setting Query Statements

The OSM type paradigm is used to classify the "geometry" of an element, so to speak, which can be one of Node, Way, or Relation. A query statement consist of a stated set of elements to search for, and one of more filters. Chauffeur provides a query element interface for defining query statements.

The following asks for school tags classified as node objects in OSM:

schools_as_node_types = cfr.NodeQuery(filters=schools)

But not all schools are nodes; we might like to get ways and relations as well:

schools_as_all_types = cfr.NodeWayRelationQuery()
schools_as_all_types.add_filter(schools)

We can add more filters to the school query, or can combine the output of this query with something else we might be interested in, again using the plus sign for unioning:

beerstores = cfr.TagFilter("shop", "alcohol")
beerstores_as_all_types = cfr.NodeWayRelationQuery(filters=beerstores)
schools_and_beerstores_query = schools_as_all_types + beerstores_as_all_types

Finally, we set the constructed query statement to the QueryBuilder object.

qb.qsx.append(schools_and_beerstores_query)

Setting AOI's and Getting Output

We can set settings, output formats, etc as we see fit. But because we have not included any cfr.BoundingBox filters, we are searching the whole world. Setting the QueryBuilder.GLobalBoundingBox attribute defines a bounding box filter for each statement in the whole query, and is part of the settings component; it is considered good practice to specify AOIs in this way:

qb.GlobalBoundingBox = [50.6,7.0,50.8,7.3] # A bbox framing the German city of Bonn

The underlying OverpassQL string can be accessed through a property:

qb.raw_query_string

Finally, we can perform a query of all Node, Way, and Relation elements that identify both schools and beer stores in Bonn, Germany, by submitting our request to the Overpass servers, and get our results back as JSON:

results = qb.request()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chauffeur-pass-0.0.1.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

chauffeur_pass-0.0.1-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file chauffeur-pass-0.0.1.tar.gz.

File metadata

  • Download URL: chauffeur-pass-0.0.1.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.4.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for chauffeur-pass-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0dfa7163a9d0d550fa832a1f23707e84a47310aa67748d203b84c2c34367ac38
MD5 cc95df1241d037150377e5ef7d6c58b5
BLAKE2b-256 78ba43a050b8ee974deafd0b83bf678bca1c318e85d0711e1945c76ed45d9345

See more details on using hashes here.

File details

Details for the file chauffeur_pass-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: chauffeur_pass-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.4.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for chauffeur_pass-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0ec23237018149257ec66792bc84868a014270c9d277f5f56f92ddc5feb1cc9d
MD5 a3f1502f720a4dd96892cd8128579c4b
BLAKE2b-256 aeb35b6d79cf07fd9c67d7cb9a388fcc5ffd335b9016e390373bf914eeeb0e28

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page