Python
Ein Client für Python ab 3.9, der nur die Standardbibliothek braucht. Ein Modul, keine Abhängigkeiten.
Installieren
pip install findort
Python
import os
from findort import Findort
findort = Findort(os.environ["FINDORT_KEY"], lang="de")
place = findort.geocode("Bundesplatz 3, Bern", country="ch")
if place:
print(place.label, place.location.lat, place.location.lon)Auf dem Server nimmst du einen geheimen Key sk_….
Client erstellen
Python
Findort(key=None, *, base_url="https://api.findort.ch", lang=None, country=None, timeout=8.0, transport=None)| Argument | Typ | Vorgabe | Beschreibung |
|---|---|---|---|
key | str | API-Key | |
base_url | str | https://api.findort.ch | Adresse der API |
lang | str | Sprache für alle Anfragen: de, fr, it, en | |
country | str oder Liste | Länderfilter für alle Suchen: "ch" oder ["ch", "li"] | |
timeout | float | 8.0 | Zeitlimit in Sekunden |
transport | callable | Eigene HTTP-Funktion für Tests: (url, headers) -> (status, body_bytes) |
search
Python
places = findort.search(
"Bahnhofstr 1 Zür",
limit=5,
types=["address", "poi"],
near=(47.3769, 8.5417),
)
for place in places:
print(place.label)| Argument | Typ | Beschreibung |
|---|---|---|
limit | int | 1 bis 20, Vorgabe 5 |
lang | str | Sprache nur für diese Anfrage |
country | str oder Liste | Länderfilter nur für diese Anfrage |
near | (lat, lon) | Resultate in der Nähe bevorzugen |
bbox | (minLon, minLat, maxLon, maxLat) | hart auf ein Rechteck begrenzen |
types | str oder Liste | nur diese Typen |
reverse
Python
places = findort.reverse(46.9465, 7.4443, limit=1, types="address")
print(places[0].label if places else "nichts gefunden")Argumente: limit, lang, radius (Kilometer, 0.01 bis 50), types.
geocode
Der beste Treffer als Place oder None. Nimmt dieselben Argumente wie search.
Python
place = findort.geocode("Zytglogge, Bern")Place
Place ist eine unveränderliche Dataclass.
| Feld | Typ |
|---|---|
id, label, name, type, category | str |
address | dict mit den Adressfeldern |
location | Location mit lat und lon |
bbox | tuple als (minLon, minLat, maxLon, maxLat) oder None |
Dazu kommen die Abkürzungen street, housenumber, postcode, city (Stadt oder Ortsteil) und country_code. Sie sind None, wenn der Wert fehlt.
Location.distance_to() rechnet die Luftlinie in Kilometern:
Python
bern = findort.geocode("Bahnhof Bern")
thun = findort.geocode("Bahnhof Thun")
print(f"{bern.location.distance_to(thun.location):.1f} km")Fehler
Bei einer Fehlerantwort wirft der Client FindortError mit status (HTTP) und code. Ist der Server nicht erreichbar, ist code gleich network_error und status gleich 0.
Python
import time
from findort import FindortError
try:
findort.search("Bern")
except FindortError as e:
if e.code == "rate_limited":
time.sleep(2)Alle Codes stehen unter Fehler.