Skip to content

Instantly share code, notes, and snippets.

@do-me
Created December 27, 2023 15:21
Show Gist options
  • Save do-me/bb960b4648865e4c6516f9fd9ecb5b9c to your computer and use it in GitHub Desktop.
Save do-me/bb960b4648865e4c6516f9fd9ecb5b9c to your computer and use it in GitHub Desktop.
Photon (komoot) helper functions in Python
import requests
from requests.auth import HTTPBasicAuth
base_url = "https://photon.yourserver.de"
# Create an HTTP session with basic authentication
username = "username"
password = "password"
session = requests.Session()
session.auth = HTTPBasicAuth(username, password)
def send_request(endpoint, params):
try:
url = f"{base_url}/{endpoint}"
response = session.get(url, params=params)
response.raise_for_status() # Raise an exception for non-200 status codes
return response.json()
except requests.exceptions.RequestException as e:
raise Exception("An error occurred while making the request: " + str(e))
def search(query, lon=None, lat=None, zoom=None, location_bias_scale=None, limit=None, lang=None, bbox=None):
try:
endpoint = "api"
params = {'q': query}
if lon is not None and lat is not None:
params['lon'] = lon
params['lat'] = lat
if zoom is not None:
params['zoom'] = zoom
if location_bias_scale is not None:
params['location_bias_scale'] = location_bias_scale
if limit is not None:
params['limit'] = limit
if lang is not None:
params['lang'] = lang
if bbox is not None:
params['bbox'] = bbox
return send_request(endpoint, params)
except Exception as e:
raise Exception("An error occurred in the search function: " + str(e))
def reverse_geocode(lon, lat):
try:
endpoint = "reverse"
params = {'lon': lon, 'lat': lat}
return send_request(endpoint, params)
except Exception as e:
raise Exception("An error occurred in the reverse_geocode function: " + str(e))
# Tests
reverse_geocode(6.3452321, 48.72385)
search("mittenheimer straße münchen", limit=1, lang="de")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment