Skip to content

Commit

Permalink
#40 Merge pull request from astropenguin/astropenguin/issue36
Browse files Browse the repository at this point in the history
Use Astropy's EarthLocation.of_address to get location
  • Loading branch information
astropenguin authored Nov 3, 2022
2 parents ab04682 + e0c2932 commit 2a7707a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 42 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
36 changes: 1 addition & 35 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ astropy = [
{ version = ">=4.3, <5.0", python = ">=3.7.1, <3.8" },
{ version = "^5.0", python = ">=3.8, <3.11" },
]
geopy = "^1.23"
numpy = [
{ version = ">=1.20, <1.22", python = ">=3.7.1, <3.8" },
{ version = "^1.20", python = ">=3.8, <3.11" },
Expand Down

0 comments on commit 2a7707a

Please sign in to comment.