From e0c29325f608f5902d41da215d15db0f8b10d5a7 Mon Sep 17 00:00:00 2001 From: Akio Taniguchi Date: Thu, 3 Nov 2022 08:35:24 +0000 Subject: [PATCH] #36 Use EarthLocation.of_address to get location --- azely/location.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/azely/location.py b/azely/location.py index 1708bff..d8c5bd8 100644 --- a/azely/location.py +++ b/azely/location.py @@ -60,8 +60,8 @@ # dependent packages from astropy.coordinates import EarthLocation -from geopy import Nominatim -from geopy.exc import GeocoderServiceError +from astropy.coordinates.name_resolve import NameResolveError +from astropy.utils.data import conf from pytz import timezone from requests import ConnectionError, api from timezonefinder import TimezoneFinder @@ -87,7 +87,6 @@ # query instances tf = TimezoneFinder() -osm = Nominatim(user_agent="azely") # data classes @@ -188,12 +187,17 @@ def get_location_by_user(query: str) -> LocationDict: @cache_to(AZELY_LOCATION) def get_location_by_query(query: str, timeout: int) -> LocationDict: """Get location information from OpenStreetMap.""" + original_remote_timeout = conf.remote_timeout + try: - res = osm.geocode(query, timeout=timeout, namedetails=True).raw - except (AttributeError, GeocoderServiceError): + conf.remote_timeout = timeout + res = EarthLocation.of_address(query) + except NameResolveError: raise AzelyError(f"Failed to get location: {query}") + finally: + conf.remote_timeout = original_remote_timeout - return Location(res["namedetails"]["name"], res["lon"], res["lat"]).to_dict() + return Location(query, str(res.lon.value), str(res.lat.value)).to_dict() @cache_to(AZELY_LOCATION)