Compare commits
No commits in common. "main" and "Task_5" have entirely different histories.
25
TASK.md
25
TASK.md
@ -1,9 +1,20 @@
|
||||
# TASK 6:
|
||||
# TASK 5:
|
||||
|
||||
* bbox für Schweiz scheint zu gross und wirft einen error ... Lösungsmöglichkeiten?
|
||||
-> Wir können die Schweiz (Koordinaten) in Unterregionen aufsplitten. Macht das bitte.
|
||||
-> entfernt dazu die bbox für 'davos', nehmt die 'schweiz' und splittet sie in 4, 9 oder 16 Koordinaten-Tuples auf.
|
||||
* Jetzt wäre doch ein guter Zeitpunkt um anstelle der print-Statements das Logging einzubauen -> verwendet das
|
||||
Logging-Modul in Python und ersetzt die print-statements!
|
||||
Hier haben wir einen weiteren Query für restaurants -> bildet ein neues Modul 'queries' und baut dort sowohl den
|
||||
Bergbahn- als auch den Restaurant-Query ein:
|
||||
|
||||
* Speichert und loggt in welchen Koordinaten-Tuples ein Fehler auftritt (gebt am Schluss eine Zusammenfassung
|
||||
dieser fehlerhaften Queries aus)
|
||||
* Bildet ein neues Modul `storage.py` und baut den Code, welcher zum Speichern der POIS als .json auf der Festplatte nötig ist.
|
||||
```
|
||||
RESTAURANT_QUERY = """
|
||||
[out:json][timeout:5][maxsize:500000];
|
||||
(
|
||||
node["amenity"="restaurant"]({bbox});
|
||||
way["amenity"="restaurant"]({bbox});
|
||||
node["amenity"="cafe"]({bbox});
|
||||
way["amenity"="cafe"]({bbox});
|
||||
);
|
||||
out center body;
|
||||
"""
|
||||
```
|
||||
|
||||
Binary file not shown.
43
main.py
43
main.py
@ -1,23 +1,5 @@
|
||||
from overpass import load_pois, OverpassApiError
|
||||
from models import POI
|
||||
import logging
|
||||
from queries.bergbahn import BERGBAHN_QUERY
|
||||
from queries.restaurant import RESTAURANT_QUERY
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Logging konfigurieren
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
# Erinnerung: Log-Levels -> DEBUG, INFO, WARNING, ERROR, CRITICAL
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s [%(levelname)s] %(message)s",
|
||||
datefmt="%H:%M:%S",
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Konfiguration
|
||||
@ -28,28 +10,33 @@ BBOXEN = {
|
||||
"schweiz": (45.8, 5.9, 47.8, 10.5),
|
||||
}
|
||||
|
||||
QUERY = {"bergbahn": BERGBAHN_QUERY}
|
||||
|
||||
QUERY = """
|
||||
[out:json][timeout:2][maxsize:500000];
|
||||
(
|
||||
node["aerialway"="station"]({bbox});
|
||||
way["aerialway"="station"]({bbox});
|
||||
node["railway"="funicular"]({bbox});
|
||||
way["railway"="funicular"]({bbox});
|
||||
node["railway"="station"]["funicular"="yes"]({bbox});
|
||||
);
|
||||
out center body;
|
||||
"""
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Hauptlogik
|
||||
# ---------------------------------------------------------------------------
|
||||
def main() -> None:
|
||||
|
||||
query_name = list(QUERY.keys())[0]
|
||||
|
||||
for name, bbox in BBOXEN.items():
|
||||
logger.info(f"Starte Abfrage für Query: {query_name}, '{name}' mit bbox={bbox}")
|
||||
try:
|
||||
pois: list[POI] = load_pois(overpass_query=QUERY.get(query_name,""), bbox=bbox)
|
||||
pois: list[POI] = load_pois(overpass_query=QUERY, bbox=bbox)
|
||||
except OverpassApiError as exc:
|
||||
logger.error(f"Fehler bei '{name}': {exc}")
|
||||
print(f"Fehler bei '{name}': {exc}")
|
||||
continue
|
||||
|
||||
logger.info(f"\n{name}: {len(pois)} POIs gefunden")
|
||||
print(f"\n{name}: {len(pois)} POIs gefunden")
|
||||
for poi in pois:
|
||||
logger.info(f" {poi.id}: ({poi.lat}, {poi.lon})")
|
||||
print(f" {poi.id}: ({poi.lat}, {poi.lon})")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@ -1,10 +1,6 @@
|
||||
import requests
|
||||
from pprint import pprint
|
||||
from models import POI
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
OVERPASS_URL = "https://overpass-api.de/api/interpreter"
|
||||
|
||||
@ -75,7 +71,7 @@ def _fetch_overpass(overpass_query: str, bbox: tuple) -> dict:
|
||||
response = requests.post(
|
||||
OVERPASS_URL,
|
||||
data={"data": query},
|
||||
timeout=15,
|
||||
timeout=5,
|
||||
headers={"User-Agent": "CDS Exercise"},
|
||||
)
|
||||
response.raise_for_status() # prüft den HTTP-Statuscode der Antwort und wirft eine Exception, wenn es ein Fehler war (requests.HTTPError)
|
||||
@ -123,7 +119,7 @@ def _parse_pois(raw: dict) -> list[POI]:
|
||||
try:
|
||||
pois.append(_parse_poi(element))
|
||||
except OverpassApiError as exc:
|
||||
logger.warning(f"POI übersprungen (id={element.get('id', '?')}): {exc}")
|
||||
print(f"POI übersprungen (id={element.get('id', '?')}): {exc}")
|
||||
return pois
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@ -1,13 +0,0 @@
|
||||
# -> Prinzip der Trennung von Daten und Logik!
|
||||
|
||||
BERGBAHN_QUERY = """
|
||||
[out:json][timeout:2][maxsize:500000];
|
||||
(
|
||||
node["aerialway"="station"]({bbox});
|
||||
way["aerialway"="station"]({bbox});
|
||||
node["railway"="funicular"]({bbox});
|
||||
way["railway"="funicular"]({bbox});
|
||||
node["railway"="station"]["funicular"="yes"]({bbox});
|
||||
);
|
||||
out center body;
|
||||
"""
|
||||
@ -1,12 +0,0 @@
|
||||
# -> Prinzip der Trennung von Daten und Logik!
|
||||
|
||||
RESTAURANT_QUERY = """
|
||||
[out:json][timeout:10][maxsize:500000];
|
||||
(
|
||||
node["amenity"="restaurant"]({bbox});
|
||||
way["amenity"="restaurant"]({bbox});
|
||||
node["amenity"="cafe"]({bbox});
|
||||
way["amenity"="cafe"]({bbox});
|
||||
);
|
||||
out center body;
|
||||
"""
|
||||
Loading…
x
Reference in New Issue
Block a user