aio_overpass.ql
Overpass QL helpers.
1"""Overpass QL helpers.""" 2 3import itertools 4 5from shapely.geometry import Polygon 6 7 8__docformat__ = "google" 9__all__ = ( 10 "poly_clause", 11 "one_of_filter", 12) 13 14 15def poly_clause(shp: Polygon) -> str: 16 """ 17 Poly region clause. 18 19 This ``(poly:...)`` clause includes results that occur within the exterior of the given 20 polygon. The input shape should be simplified, since a larger number of coordinates will 21 slow down the query. 22 23 References: 24 - https://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide#Select_region_by_polygon 25 """ 26 flattened_coords = itertools.chain.from_iterable(shp.exterior.coords[:-1]) 27 bounds = " ".join(map(str, flattened_coords)) 28 return f'(poly:"{bounds}")' 29 30 31def one_of_filter(tag: str, *values: str) -> str: 32 """ 33 Tag filter that requires elements to have a tag value that is in the list of the given ones. 34 35 * returns no filter if ``values`` is empty 36 * returns a simple ``[key="value1"]`` filter if ``values`` has one item 37 * returns a regex filter ``[key~"^value1$|^value2$|..."]`` filter 38 if ``values`` has multiple items 39 40 References: 41 - https://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide#Tag_request_clauses_(or_%22tag_filters%22) 42 """ 43 if not values: 44 return "" 45 46 if len(values) == 1: 47 return f'[{tag}="{values[0]}"]' 48 49 regex = "|".join(f"^{v}$" for v in values) 50 return f'[{tag}~"{regex}"]'
def
poly_clause(shp: shapely.geometry.polygon.Polygon) -> str:
16def poly_clause(shp: Polygon) -> str: 17 """ 18 Poly region clause. 19 20 This ``(poly:...)`` clause includes results that occur within the exterior of the given 21 polygon. The input shape should be simplified, since a larger number of coordinates will 22 slow down the query. 23 24 References: 25 - https://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide#Select_region_by_polygon 26 """ 27 flattened_coords = itertools.chain.from_iterable(shp.exterior.coords[:-1]) 28 bounds = " ".join(map(str, flattened_coords)) 29 return f'(poly:"{bounds}")'
Poly region clause.
This (poly:...)
clause includes results that occur within the exterior of the given
polygon. The input shape should be simplified, since a larger number of coordinates will
slow down the query.
References:
def
one_of_filter(tag: str, *values: str) -> str:
32def one_of_filter(tag: str, *values: str) -> str: 33 """ 34 Tag filter that requires elements to have a tag value that is in the list of the given ones. 35 36 * returns no filter if ``values`` is empty 37 * returns a simple ``[key="value1"]`` filter if ``values`` has one item 38 * returns a regex filter ``[key~"^value1$|^value2$|..."]`` filter 39 if ``values`` has multiple items 40 41 References: 42 - https://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide#Tag_request_clauses_(or_%22tag_filters%22) 43 """ 44 if not values: 45 return "" 46 47 if len(values) == 1: 48 return f'[{tag}="{values[0]}"]' 49 50 regex = "|".join(f"^{v}$" for v in values) 51 return f'[{tag}~"{regex}"]'
Tag filter that requires elements to have a tag value that is in the list of the given ones.
- returns no filter if
values
is empty - returns a simple
[key="value1"]
filter ifvalues
has one item - returns a regex filter
[key~"^value1$|^value2$|..."]
filter ifvalues
has multiple items