|
1 | 1 | """Utility functions for the library.""" |
| 2 | +import os |
| 3 | +import logging |
| 4 | +from typing import Tuple, Dict, Union |
| 5 | +import csv |
| 6 | + |
2 | 7 | from geopy.exc import GeocoderUnavailable, GeocoderTimedOut, GeocoderServiceError # type: ignore |
3 | 8 | from geopy.geocoders import Nominatim # type: ignore |
4 | 9 | from tzwhere import tzwhere # type: ignore |
| 10 | +import backoff # type: ignore |
| 11 | + |
5 | 12 | from .errors import ParserError |
6 | 13 |
|
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | +dirname = os.path.dirname(__file__) |
| 17 | + |
| 18 | + |
| 19 | +class Geolocator: |
| 20 | + """Class to obtain Geo Location coordinates.""" |
| 21 | + |
| 22 | + # Keeping caching of local DB and timezone in the class |
| 23 | + db_location: Dict[Union[Tuple[str, str], str], Tuple[float, float]] = {} |
| 24 | + timezone = None |
| 25 | + |
| 26 | + def __init__(self): |
| 27 | + """Initialize instance.""" |
| 28 | + self.load_db_location() |
| 29 | + self.load_timezone() |
| 30 | + |
| 31 | + @classmethod |
| 32 | + def load_timezone(cls): |
| 33 | + """Load the timezone resolver.""" |
| 34 | + if cls.timezone is None: |
| 35 | + cls.timezone = tzwhere.tzwhere() |
| 36 | + logger.info("Loaded local timezone resolver.") |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def load_db_location(cls): |
| 40 | + """Load the localtions DB from CSV into a Dict.""" |
| 41 | + with open(os.path.join(dirname, "data", "worldcities.csv")) as csvfile: |
| 42 | + reader = csv.DictReader(csvfile) |
| 43 | + for row in reader: |
| 44 | + # Index by city and country |
| 45 | + cls.db_location[(row["city_ascii"], row["country"])] = (float(row["lat"]), float(row["lng"])) |
| 46 | + # Index by city (first entry wins if duplicated names) |
| 47 | + if row["city_ascii"] not in cls.db_location: |
| 48 | + cls.db_location[row["city_ascii"]] = (float(row["lat"]), float(row["lng"])) |
7 | 49 |
|
8 | | -def city_timezone(city: str) -> str: |
9 | | - """Get the timezone for a given city. |
| 50 | + def get_location(self, city: str) -> Tuple[float, float]: |
| 51 | + """Get location.""" |
| 52 | + try: |
| 53 | + location_coordinates = self.get_location_from_local_file(city) |
| 54 | + except ValueError: |
| 55 | + location_coordinates = self.get_location_from_api(city) |
10 | 56 |
|
11 | | - Args: |
12 | | - city (str): Geographic location name |
13 | | - """ |
14 | | - try: |
| 57 | + logger.debug( |
| 58 | + "Resolved city %s to coordinates: lat %s - lon %s", city, location_coordinates[0], location_coordinates[1], |
| 59 | + ) |
| 60 | + return location_coordinates |
| 61 | + |
| 62 | + def get_location_from_local_file(self, city: str) -> Tuple[float, float]: |
| 63 | + """Get location from Local DB.""" |
| 64 | + city_name = city.split(", ")[0] |
| 65 | + country = city.split(", ")[-1] |
| 66 | + |
| 67 | + lat, lng = self.db_location.get((city_name, country), self.db_location.get(city_name, (None, None))) |
| 68 | + if lat and lng: |
| 69 | + logger.debug("Resolved %s to lat %s, lon %sfrom local locations DB.", city, lat, lng) |
| 70 | + return (lat, lng) |
| 71 | + |
| 72 | + logger.debug("City %s was not resolvable in the local locations DB.", city) |
| 73 | + raise ValueError |
| 74 | + |
| 75 | + @staticmethod |
| 76 | + @backoff.on_exception( |
| 77 | + backoff.expo, (GeocoderUnavailable, GeocoderTimedOut, GeocoderServiceError), max_time=10, logger=logger, |
| 78 | + ) |
| 79 | + def get_location_from_api(city: str) -> Tuple[float, float]: |
| 80 | + """Get location from API.""" |
15 | 81 | geolocator = Nominatim(user_agent="circuit_maintenance") |
16 | 82 | location = geolocator.geocode(city) # API call to OpenStreetMap web service |
17 | | - timezone = ( |
18 | | - tzwhere.tzwhere() |
19 | | - ) # TODO: Offline loading of timezone location data is quite slow. Look for better alternative |
20 | | - return timezone.tzNameAt(location.latitude, location.longitude) |
21 | | - except (GeocoderUnavailable, GeocoderTimedOut, GeocoderServiceError): |
22 | | - raise ParserError( # pylint: disable=raise-missing-from |
23 | | - "Cannot connect to the remote Geolocator API to determine timezone" |
24 | | - ) |
| 83 | + logger.debug("Resolved %s to %s from OpenStreetMap webservice.", city, location) |
| 84 | + return (location.latitude, location.longitude) |
| 85 | + |
| 86 | + def city_timezone(self, city: str) -> str: |
| 87 | + """Get the timezone for a given city. |
| 88 | +
|
| 89 | + Args: |
| 90 | + city (str): Geographic location name |
| 91 | + """ |
| 92 | + if self.timezone is not None: |
| 93 | + try: |
| 94 | + latitude, longitude = self.get_location(city) |
| 95 | + timezone = self.timezone.tzNameAt(latitude, longitude) |
| 96 | + if not timezone: |
| 97 | + # In some cases, given a latitued and longitued, the tzwhere library returns |
| 98 | + # an empty timezone, so we try with the coordinates from the API as an alternative |
| 99 | + latitude, longitude = self.get_location_from_api(city) |
| 100 | + timezone = self.timezone.tzNameAt(latitude, longitude) |
| 101 | + |
| 102 | + if timezone: |
| 103 | + logger.debug("Matched city %s to timezone %s", city, timezone) |
| 104 | + return timezone |
| 105 | + except Exception as exc: |
| 106 | + logger.error("Cannot obtain the timezone for city %s: %s", city, exc) |
| 107 | + raise ParserError( # pylint: disable=raise-missing-from |
| 108 | + f"Cannot obtain the timezone for city {city}: {exc}" |
| 109 | + ) |
| 110 | + raise ParserError("Timezone resolution not properly initalized.") |
25 | 111 |
|
26 | 112 |
|
27 | 113 | def rgetattr(obj, attr): |
|
0 commit comments