aboutsummaryrefslogtreecommitdiffstats
path: root/fnme/geo.py
blob: ec102377569055aad5ffb7f9994d05dd5fc41a87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from geopy import exc
from geopy.geocoders import Nominatim
from geopy.location import Location

from fnme.exceptions import LocationError


def get_location(address: str) -> tuple[float, float]:
    geolocator = Nominatim(user_agent="FuelNearMe")

    try:
        result = geolocator.geocode(address)
    except exc.GeopyError as e:
        raise LocationError(message=f"Location service error: {e}")

    if not isinstance(result, Location):
        raise LocationError(message=f"Unknown location: '{address}'")
    return (result.latitude, result.longitude)
git.ajschof.me — hosted by ajschofield — powered by cgit