Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d0ab53185c |
25
TASK.md
25
TASK.md
@ -1,20 +1,9 @@
|
|||||||
# TASK 5:
|
# TASK 6:
|
||||||
|
|
||||||
* Jetzt wäre doch ein guter Zeitpunkt um anstelle der print-Statements das Logging einzubauen -> verwendet das
|
* bbox für Schweiz scheint zu gross und wirft einen error ... Lösungsmöglichkeiten?
|
||||||
Logging-Modul in Python und ersetzt die print-statements!
|
-> Wir können die Schweiz (Koordinaten) in Unterregionen aufsplitten. Macht das bitte.
|
||||||
Hier haben wir einen weiteren Query für restaurants -> bildet ein neues Modul 'queries' und baut dort sowohl den
|
-> entfernt dazu die bbox für 'davos', nehmt die 'schweiz' und splittet sie in 4, 9 oder 16 Koordinaten-Tuples auf.
|
||||||
Bergbahn- als auch den Restaurant-Query ein:
|
|
||||||
|
|
||||||
```
|
* Speichert und loggt in welchen Koordinaten-Tuples ein Fehler auftritt (gebt am Schluss eine Zusammenfassung
|
||||||
RESTAURANT_QUERY = """
|
dieser fehlerhaften Queries aus)
|
||||||
[out:json][timeout:5][maxsize:500000];
|
* Bildet ein neues Modul `storage.py` und baut den Code, welcher zum Speichern der POIS als .json auf der Festplatte nötig ist.
|
||||||
(
|
|
||||||
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,5 +1,23 @@
|
|||||||
from overpass import load_pois, OverpassApiError
|
from overpass import load_pois, OverpassApiError
|
||||||
from models import POI
|
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
|
# Konfiguration
|
||||||
@ -10,33 +28,28 @@ BBOXEN = {
|
|||||||
"schweiz": (45.8, 5.9, 47.8, 10.5),
|
"schweiz": (45.8, 5.9, 47.8, 10.5),
|
||||||
}
|
}
|
||||||
|
|
||||||
QUERY = """
|
QUERY = {"bergbahn": 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;
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Hauptlogik
|
# Hauptlogik
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
|
|
||||||
|
query_name = list(QUERY.keys())[0]
|
||||||
|
|
||||||
for name, bbox in BBOXEN.items():
|
for name, bbox in BBOXEN.items():
|
||||||
|
logger.info(f"Starte Abfrage für Query: {query_name}, '{name}' mit bbox={bbox}")
|
||||||
try:
|
try:
|
||||||
pois: list[POI] = load_pois(overpass_query=QUERY, bbox=bbox)
|
pois: list[POI] = load_pois(overpass_query=QUERY.get(query_name,""), bbox=bbox)
|
||||||
except OverpassApiError as exc:
|
except OverpassApiError as exc:
|
||||||
print(f"Fehler bei '{name}': {exc}")
|
logger.error(f"Fehler bei '{name}': {exc}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print(f"\n{name}: {len(pois)} POIs gefunden")
|
logger.info(f"\n{name}: {len(pois)} POIs gefunden")
|
||||||
for poi in pois:
|
for poi in pois:
|
||||||
print(f" {poi.id}: ({poi.lat}, {poi.lon})")
|
logger.info(f" {poi.id}: ({poi.lat}, {poi.lon})")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@ -1,6 +1,10 @@
|
|||||||
import requests
|
import requests
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from models import POI
|
from models import POI
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
OVERPASS_URL = "https://overpass-api.de/api/interpreter"
|
OVERPASS_URL = "https://overpass-api.de/api/interpreter"
|
||||||
|
|
||||||
@ -71,7 +75,7 @@ def _fetch_overpass(overpass_query: str, bbox: tuple) -> dict:
|
|||||||
response = requests.post(
|
response = requests.post(
|
||||||
OVERPASS_URL,
|
OVERPASS_URL,
|
||||||
data={"data": query},
|
data={"data": query},
|
||||||
timeout=5,
|
timeout=15,
|
||||||
headers={"User-Agent": "CDS Exercise"},
|
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)
|
response.raise_for_status() # prüft den HTTP-Statuscode der Antwort und wirft eine Exception, wenn es ein Fehler war (requests.HTTPError)
|
||||||
@ -119,7 +123,7 @@ def _parse_pois(raw: dict) -> list[POI]:
|
|||||||
try:
|
try:
|
||||||
pois.append(_parse_poi(element))
|
pois.append(_parse_poi(element))
|
||||||
except OverpassApiError as exc:
|
except OverpassApiError as exc:
|
||||||
print(f"POI übersprungen (id={element.get('id', '?')}): {exc}")
|
logger.warning(f"POI übersprungen (id={element.get('id', '?')}): {exc}")
|
||||||
return pois
|
return pois
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
BIN
queries/__pycache__/bergbahn.cpython-313.pyc
Normal file
BIN
queries/__pycache__/bergbahn.cpython-313.pyc
Normal file
Binary file not shown.
BIN
queries/__pycache__/restaurant.cpython-313.pyc
Normal file
BIN
queries/__pycache__/restaurant.cpython-313.pyc
Normal file
Binary file not shown.
13
queries/bergbahn.py
Normal file
13
queries/bergbahn.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# -> 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;
|
||||||
|
"""
|
||||||
12
queries/restaurant.py
Normal file
12
queries/restaurant.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# -> 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