API Reference

Base classes

class overpassforge.base.Statement(label=None)

Represents a generic Overpass QL statement.

Parameters:

label (Optional[str]) – A label for this statement which may be used as variable name for the result of this statement at compilation.

class overpassforge.base.Set(filters=[], label=None)

Represents a set, i.e. a statement that always returns a set of elements.

Parameters:

label (Optional[str]) – A label for this statement which may be used as variable name for the result of this statement at compilation.

out(*options)

Indicate that this set must be outputed.

Parameters:

options (str | tuple[float, float, float, float]) – empty or any combination of “ids”, “skel”, “body”, “tags”, “meta”, “noids”, “geom”, “bb”, “center”, “asc”, “qt”, “count” or a bounding box (south,west,north,east).

Raises:

ValueError – Invalid output options.

filter(*filters)

Adds filters to the set.

Parameters:

filters (Filter | str) – A list of filters or strings representing raw filters.

Return type:

Set

where(*keys, **tags)

Adds filters “key”, ~”key”, “key”=”value”, or “key”~”value”.

Parameters:
  • keys (str | Regex) – List of keys the elements must have

  • tags (str | Regex) – List of key=”value” tags the elements must have.

Return type:

Set

within(area)

Filters the elements that are in the specified area.

Parameters:

area (tuple[float, float, float, float] | BoundingBox | Polygon | Area | Areas) – A bounding box (filter object or as a tuple), polygon filter, area filter or set of areas.

Return type:

Set

intersection(*others)

Returns the statement computing the intersection of this statement with the others.

Parameters:

others (Statement) – Other statements to intersect with

Return type:

Set

changed_since(date)

Filters the elements that were changed since the specified datetime.

Parameters:

date (datetime) – The lower bound of change dates to consider

Return type:

Set

changed_between(lower, upper)

Filters the elements that were changed between the two specified dates.

Parameters:
  • lower (datetime) – Lower bound datetime.

  • upper (datetime) – Upper bound datetime.

Return type:

Set

last_changed_by(*users)

Filters the elements that last changed by any of the given users.

Parameters:

users (str | int) – The list of user names or user ids.

Return type:

Set

outlines_of(area)

Filters the elements that are part of the outline of the given area.

Parameters:

area (Areas) – The area to consider to outline of.

Return type:

Set

around(radius, other=None, lats=None, lons=None)

Filters elements that are within a given radius of the elements of another set or a list of given coordinates (cannot specify both).

Parameters:
  • radius (float) – The radius to consider around each element (in meters).

  • other (Optional[Statement]) – Another statement.

  • lats (Optional[Iterable[float]]) – List of latitude of points.

  • lons (Optional[Iterable[float]]) – List of longitudes of points.

Return type:

Set

recursed_down()

Returns the recursed down (>) set.

Return type:

RecurseDown

recursed_down_rels()

Returns the recursed down relations (>>) set.

Return type:

RecurseDownRels

recursed_up()

Returns the recursed down (<) set.

Return type:

RecurseUp

recursed_up_rels()

Returns the recursed up relations (<<) set.

Return type:

RecurseUpRels

overlapping_areas()

Returns the set of areas that overlap at least one of the elements of this set.

Return type:

Areas

elements(*filters)

Returns the elements (nodes, ways, relations) in this set.

Return type:

Elements

nodes(*filters)

Returns the nodes in this set.

Return type:

Nodes

ways(*filters)

Returns the ways in this set.

Return type:

Ways

relations(*filters)

Returns the relations in this set.

Return type:

Relations

areas(*filters)

Returns the areas in this set.

Return type:

Areas

Statements

class overpassforge.statements.RawStatement(raw='', label=None, **dependencies)

Bases: Statement

Represents a raw Overpass query string. It can be formated to support dependency from other statements (raw or not).

Example:

>>> foo = Nodes().where(name="Foo")
>>> bar = Statement("node.{x}[amenity=\"bar\"]->.{:out_var};", x=foo)
>>> baz = Nodes(input_set=bar).within((50.6,7.0,50.8,7.3))
>>> print(build(baz))
node["name"="Foo"]->.set_0;
node.set_0[amenity="bar"]->.set_1;
node.set_1(50.6,7.0,50.8,7.3);
Parameters:
  • raw (str) – A formatable string of the query. Named placeholders (e.g. “{name}”) indicating dependency on other statements. An optional special {:out_var} placeholder indicates where the name of the output set must be placed.

  • dependencies (Statement) – The list of statements whose results it depends on Their keyword names must match the placeholders in the raw query string.

Raises:

ValueError – Invalid unamed placholder “{}”.

class overpassforge.statements.Elements(ids=None, label=None, *, bounding_box=None, input_set=None, within=None, around=None, filters=[], **tags)

Bases: Set

Represents a query set, e.g. node, rel, way…

Parameters:

label (Optional[str]) – A label for this statement which may be used as variable name for the result of this statement at compilation.

class overpassforge.statements.Nodes(ids=None, label=None, *, bounding_box=None, input_set=None, within=None, around=None, filters=[], **tags)

Bases: Elements

A node query.

Example:

>>> all_nodes = Nodes(bounding_box=(50.6,7.0,50.8,7.3))
>>> cinemas = all_nodes.where(amenity="cinema")
>>> non_cinemas = all_nodes - cinemas
>>> print(build(non_cinemas))
node(50.6,7.0,50.8,7.3)->.set_0;
(.set_0; - node.set_0["amenity"="cinema"];);
Parameters:

label (Optional[str]) – A label for this statement which may be used as variable name for the result of this statement at compilation.

class overpassforge.statements.Ways(ids=None, label=None, *, bounding_box=None, input_set=None, within=None, around=None, filters=[], **tags)

Bases: Elements

A way query.

Parameters:

label (Optional[str]) – A label for this statement which may be used as variable name for the result of this statement at compilation.

class overpassforge.statements.Relations(ids=None, label=None, *, bounding_box=None, input_set=None, within=None, around=None, filters=[], **tags)

Bases: Elements

A relation query.

Parameters:

label (Optional[str]) – A label for this statement which may be used as variable name for the result of this statement at compilation.

class overpassforge.statements.Areas(ids=None, label=None, *, bounding_box=None, input_set=None, within=None, around=None, filters=[], **tags)

Bases: Elements

An area query.

Parameters:

label (Optional[str]) – A label for this statement which may be used as variable name for the result of this statement at compilation.

elements_within(*filters)

Returns the elements within the areas.

Return type:

Elements

nodes_within(*filters)

Returns the nodes within the areas.

Return type:

Nodes

ways_within(*filters)

Returns the ways within the areas.

Return type:

Ways

relations_within(*filters)

Returns the relations within the areas.

Return type:

Relations

outline()

Returns the elements that are part of the outline of the areas.

Return type:

Elements

class overpassforge.statements.Union(*statements, label=None)

Bases: Combination

Represents a union of sets: (set_1; set_2; …);

Example:

>>> union = Union(Ways(42), Nodes(42))
>>> print(build(union))
(way(42); node(42));
>>> union = Ways(42) + Nodes(42)
>>> print(build(union))
(way(42); node(42));
Parameters:

statements (Set) – The list of statement whose results to combine.

class overpassforge.statements.Difference(a, b, label=None)

Bases: Combination

Represents the difference of two sets: (set_1 - set_2;);

Example:

>>> bbox_ways = Ways(bounding_box=(50.6,7.0,50.8,7.3))
>>> one_way = Ways(41)
>>> print(build(Difference(bbox_ways, one_way)))
(way(50.6,7.0,50.8,7.3); - way(42););
>>> print(build(bbox_ways - one_way))
(way(50.6,7.0,50.8,7.3); - way(42););
Parameters:
  • a (Statement) – The base statement.

  • b (Statement) – The statement whose results to remove from a.

class overpassforge.statements.RecurseDown(input_set, label=None)

Bases: _Recurse

Recurse down elements (>). Takes an input set and produces a set composed of: - all nodes that are part of a way which appears in the input set; plus - all nodes and ways that are members of a relation which appears in the input set; plus - all nodes that are part of a way which appears in the result set.

(taken from the Overpass QL documentation)

Parameters:

label (Optional[str]) – A label for this statement which may be used as variable name for the result of this statement at compilation.

class overpassforge.statements.RecurseDownRels(input_set, label=None)

Bases: _Recurse

Recurse down relations (>>). Similar to simple recurse down but also it continues to follow the membership links including nodes in ways until for every object in its input or result set all he members of that object are in the result set as well.

(taken from the Overpass QL documentation)

Parameters:

label (Optional[str]) – A label for this statement which may be used as variable name for the result of this statement at compilation.

class overpassforge.statements.RecurseUp(input_set, label=None)

Bases: _Recurse

Recurse up elements (<). Takes an input set and produces a set composed of: - all ways that have a node which appears in the input set; plus - all relations that have a node or way which appears in the input set; plus - all relations that have a way which appears in the result set.

(taken from the Overpass QL documentation)

Parameters:

label (Optional[str]) – A label for this statement which may be used as variable name for the result of this statement at compilation.

class overpassforge.statements.RecurseUpRels(input_set, label=None)

Bases: _Recurse

Recurse up relations (<<). Similar to simple recurse up but also it continues to follow backlinks onto the found relations until it contains all relations that point to an object in the input or result set.

(taken from the Overpass QL documentation)

Parameters:

label (Optional[str]) – A label for this statement which may be used as variable name for the result of this statement at compilation.

class overpassforge.statements.OverlappingAreas(lat=None, lon=None, input_set=None, label=None)

Bases: Areas

Corresponds to the is_in statement. Represents the areas and closed ways that cover: - the given coordinates (when specified) or - one or more nodes from the input set (when no coordinates are specified).

(taken from the Overpass QL documentation)

Parameters:

label (Optional[str]) – A label for this statement which may be used as variable name for the result of this statement at compilation.

filter(*filters)

Adds filters to the set.

Parameters:

filters (Filter | str) – A list of filters or strings representing raw filters.

Return type:

Areas

class overpassforge.statements.AsAreas(input_set, label=None)

Bases: Areas

Represents the map_to_area statement.

Parameters:

label (Optional[str]) – A label for this statement which may be used as variable name for the result of this statement at compilation.

filter(*filters)

Adds filters to the set.

Parameters:

filters (Filter | str) – A list of filters or strings representing raw filters.

Return type:

Areas

Filters

class overpassforge.filters.Filter(raw=None)

Bases: object

Represents a generic filter that can be applied on a query statement.

class overpassforge.filters.Regex(pattern)

Bases: object

Wrapper around a string to indicate it should be treated as a regex.

class overpassforge.filters.Tag(comparison, case_sensitive=True)

Bases: Filter

Represents a generic tag filter.

Parameters:
  • comparison (str) – the comparison expression string of the tag filter (e.g. “name”=”Foo”, !”tourism”)

  • case_sensitive – ignore case (e.g. if comparison is “name”=”Foo” then a tag “name”=”fOO” is also valid)

class overpassforge.filters.Key(key, case_sensitive=True)

Bases: Tag

Represents a key of an element’s tags. Used to build tag filter expressions.

Examples:

>>> Key("amenity")
<Tag "amenity", case=True>
>>> ~Key("amenity")
<Tag !"amenity", case=True>
>>> Key("amenity") == "cinema"
<Tag "amenity"="cinema", case=True>
>>> Key("name", case_sensitive=False) == Regex("^Foo$")
<Tag "name"~"^Foo$", case=False>
>>> Key(Regex("^addr:.*$")) != Regex("^Foo$")
<Tag ~"^addr:.*$"!~"^Foo$", case=True>
Parameters:

key (str | Regex) – The key string.

class overpassforge.filters.BoundingBox(south, west, north, east)

Bases: Filter

Bounding box filter on a query statement.

Parameters:
  • south (float) – Minimum latitude.

  • west (float) – Minimum longitude.

  • north (float) – Maximum latitude.

  • east (float) – Maximum longitude.

class overpassforge.filters.Ids(*ids)

Bases: Filter

Represents an id filter.

Parameters:

ids (int) – The list of OSM ids to select.

class overpassforge.filters.Intersect(*statements)

Bases: Filter

Intersection with other statement results.

Parameters:

statements (Statement) – The other statement whose results intersect with.

class overpassforge.filters.Newer(date)

Bases: Filter

Filter by newer change dates.

Parameters:

date (datetime) – The oldest when the element has been modified.

class overpassforge.filters.Changed(lower, upper=None)

Bases: Filter

Filter that selects elements that have been changed between two given dates. If only the lower date is given, the second is assumed to be the front date of the database.

Parameters:
  • lower (datetime) – Dates’ range lower bound.

  • upper (Optional[datetime]) – Dates’ range upper bound. If not given, it is assumed to be the front date of the database.

class overpassforge.filters.User(*users)

Bases: Filter

Filter the elements last edited by the specified users.

Parameters:

users (int | str) – A list of user names or user ids.

Raises:

ValueError – No user specified.

class overpassforge.filters.Area(input_area)

Bases: Filter

Filters the elements which are within the given area.

Parameters:

input_area (Areas) – The areas in which the elements must lay in.

class overpassforge.filters.Pivot(input_area)

Bases: Filter

Filters the elements which are part of the outline of the given area.

Parameters:

input_area (Areas) – The areas to consider the outlines of.

class overpassforge.filters.Around(radius, input_set=None, lats=None, lons=None)

Bases: Filter

Filters elements within a certain radius around elements in the input set.

Parameters:
  • radius (float) – The radius in meters around each elements.

  • input_set (Optional[Statement]) – The input set of elements around which to filter (cannot be used in combination with lats, lons).

  • lats (Optional[Iterable[float]]) – Latitude and longitudes of points around which to filter (cannot be used in combination with input_set).

  • lons (Optional[Iterable[float]]) – Latitude and longitudes of points around which to filter (cannot be used in combination with input_set).

class overpassforge.filters.Polygon(lats, lons)

Bases: Filter

Filters all elements that are inside the defined polygon.

Parameters:
  • lats (Iterable[float]) – Latitudes of the points describing the polygon.

  • lons (Iterable[float]) – Longitudes of the points describing the polygon.

Builder

class overpassforge.builder.Settings(out='json', timeout=25, maxsize=None, bbox=None, date=None, diff=None, csv_fields=None, csv_header_line=True, csv_separator=None)

Global settings of an Overpass query. See the Overpass reference for more details.

out: Literal['json', 'xml', 'csv'] = 'json'
timeout: Optional[int] = 25
maxsize: Optional[int] = None
bbox: Optional[tuple[float, float, float, float]] = None
date: Optional[datetime] = None
diff: UnionType[tuple[datetime], tuple[datetime, datetime], None] = None
csv_fields: Optional[tuple[str, ...]] = None
csv_header_line: bool = True
csv_separator: Optional[str] = None
overpassforge.builder.build(*statements, settings=None, beautify=False)

Builds the Overpass query string of the given statement, with the optional global settings.

Parameters:
  • statement – The statement to compile.

  • settings (Optional[Settings]) – Global query settings to append at the top of the generated query.

  • beautify (bool) – If True, generates a more readable query (indentations, line breaks…)

Return type:

str

Returns:

The compiled Overpass query.

Raises:
  • CircularDependencyError – One of the substatements requires its own result.

  • InvalidFilterAttributes – Invalid filter.

  • InvalidQuerySettings – Invalid query settings.

  • UnexpectedCompilationError – Unexpected internal compilation error.