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)
ArgumentTypVorgabeBeschreibung
keystrAPI-Key
base_urlstrhttps://api.findort.chAdresse der API
langstrSprache für alle Anfragen: de, fr, it, en
countrystr oder ListeLänderfilter für alle Suchen: "ch" oder ["ch", "li"]
timeoutfloat8.0Zeitlimit in Sekunden
transportcallableEigene HTTP-Funktion für Tests: (url, headers) -> (status, body_bytes)
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)
ArgumentTypBeschreibung
limitint1 bis 20, Vorgabe 5
langstrSprache nur für diese Anfrage
countrystr oder ListeLä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
typesstr oder Listenur 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.

FeldTyp
id, label, name, type, categorystr
addressdict mit den Adressfeldern
locationLocation mit lat und lon
bboxtuple 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.