Skip to content

Commit

Permalink
#36 Use EarthLocation.of_address to get location
Browse files Browse the repository at this point in the history
  • Loading branch information
astropenguin committed Nov 3, 2022
1 parent e0fc067 commit e0c2932
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions azely/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -87,7 +87,6 @@

# query instances
tf = TimezoneFinder()
osm = Nominatim(user_agent="azely")


# data classes
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e0c2932

Please sign in to comment.