CamHack25/engine.py

67 lines
1.5 KiB
Python
Executable file

#!/usr/bin/env python
import overpy
# brandname : overpass query filters
BRANDS: dict[str, str] = {
"greggs": "[\"brand:wikidata\"=\"Q3403981\"]",
}
EncodedLocation = list[tuple[float, list[float]]]
def fetch_data(brand: str) -> list[tuple[float | None, float | None]]:
"""Fetch a list of locations from OSM."""
api = overpy.Overpass()
filters = BRANDS[brand]
query = api.query(f"nwr{filters}; out center;")
result = []
for way in query.ways:
result.append((way.center_lat, way.center_lon))
for node in query.nodes:
result.append((node.lat, node.lon))
for (lat, lon) in result:
if (lat is None) or (lon is None):
raise ValueError("Item missing coords!")
return result
def encode(location: tuple[float, float]) -> EncodedLocation:
"""Encode a location."""
# Stub
return [
(5., [1., 2., 3.]),
(6., [4., 5., 6.]),
]
def decode(location: EncodedLocation) -> tuple[float, float]:
"""Decode into a location."""
# Stub
return (0.091659, 52.210796)
def format_location(location: EncodedLocation) -> str:
"""Format an encoded location as a string."""
return ";".join([f"{a}:{','.join(map(str, b))}" for (a, b) in location])
def main():
"""Testing."""
print("Running query...")
greggs = fetch_data("greggs")
print(f"Query done - got {len(greggs)} Greggs!")
print(format_location(encode((0.091659, 52.210796))))
if __name__ == "__main__":
main()