Skip to content

Commit

Permalink
FIX: Export for administrative boundaries without administrative boun…
Browse files Browse the repository at this point in the history
…daries child nodes
  • Loading branch information
AlexanderKaschta committed May 24, 2024
1 parent c5e8563 commit eb3188c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
45 changes: 34 additions & 11 deletions mcp_tools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


def main() -> None:

task_choices = ["OpenStreetMaps-Export", "Anwendung beenden"]

questions = [inquirer.List("task",
Expand All @@ -21,7 +20,6 @@ def main() -> None:


def osm_export() -> None:

export_street_options = ["Straßen", "Parks", "Plätze"]
yes_no_options = ["Ja", "Nein"]

Expand Down Expand Up @@ -52,23 +50,48 @@ def osm_export() -> None:

response = request.json()

boundaries = response["elements"]

if len(boundaries) == 0:
# Do another check if the boundary is a root boundary
overpass_backup_query = f"[out:json];area[name=\"{answers['city']}\"];out;"

request = requests.get(OVERPASS_API_URL, params={"data": overpass_backup_query})

try:
request.raise_for_status()
except requests.exceptions.HTTPError:
print("Fehler: Fehlerhafter Status-Code der HTTP-Anfrage!")
pass

response = request.json()

boundaries = []

for element in response["elements"]:
if element["type"] == "area":
if element["tags"]["type"] == "boundary" and element["tags"]["admin_level"] in ("8", "9", "10"):
boundaries.append(element)

if len(boundaries) == 0:
print("Fehler: Es konnten keine passenden Daten gefunden werden!")
pass

sections = []
city_section = {"name": answers["city"], "addresses": [], "objects": []}

counter = 1

if len(response["elements"]) == 0:
print("Fehler: Es konnten keine passenden Daten gefunden werden!")
pass

for item in response["elements"]:
for item in boundaries:
streets = set()

print(f"Lade {item['tags']['name']}")

area_id = item['id'] if 3700000000 > item['id'] > 3600000000 else 3600000000 + item['id']

if export_street_options[0] in answers["options"]:
# Get all streets in the area
overpass_street_query = (f"[out:json];area({3600000000 + item['id']});"
overpass_street_query = (f"[out:json];area({area_id});"
f"way[highway~\"^(motorway|trunk|primary|secondary|tertiary|unclassified|"
f"residential|living_street|pedestrian)$\"][name](area);out;")
request = requests.get(OVERPASS_API_URL, params={"data": overpass_street_query})
Expand All @@ -80,7 +103,7 @@ def osm_export() -> None:

if export_street_options[1] in answers["options"]:
# Get all parks in the area
overpass_park_query = (f"[out:json];area({3600000000 + item['id']});"
overpass_park_query = (f"[out:json];area({area_id});"
f"(way[\"leisure\"=\"park\"](area);relation[\"leisure\"=\"park\"](area););out;")
request = requests.get(OVERPASS_API_URL, params={"data": overpass_park_query})

Expand All @@ -94,7 +117,7 @@ def osm_export() -> None:

if export_street_options[2] in answers["options"]:
# Get all squares in the area
overpass_square_query = (f"[out:json];area({3600000000 + item['id']});"
overpass_square_query = (f"[out:json];area({area_id});"
f"(way[\"place\"=\"square\"](area);relation[\"place\"=\"square\"](area););out;")
request = requests.get(OVERPASS_API_URL, params={"data": overpass_square_query})

Expand All @@ -111,7 +134,7 @@ def osm_export() -> None:

if answers["buildings"] == yes_no_options[0]:
# Export buildings
overpass_building_query = (f"[out:json];area({3600000000 + item['id']});"
overpass_building_query = (f"[out:json];area({area_id});"
f"(way[\"building\"][\"name\"][\"addr:street\"](area);"
f"relation[\"building\"][\"name\"][\"addr:street\"](area););out;")

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "mcp_tools"
version = "1.2.1"
version = "1.3.0"
description = "A tool collection for MCP"
authors = [
{ name = "Alexander Kaschta" },
Expand Down

0 comments on commit eb3188c

Please sign in to comment.