Fix geolocation

This commit is contained in:
Cédric Leporcq 2021-08-13 19:12:42 +02:00
parent 1936231ea2
commit 2e9161183d
2 changed files with 8 additions and 4 deletions

View File

@ -39,7 +39,9 @@ def get_path_definition(config):
def get_geocoder(): def get_geocoder():
config = load_config(constants.CONFIG_FILE) config = load_config(constants.CONFIG_FILE)
if 'Geolocation' in config and 'geocoder' in config['Geolocation']: if 'Geolocation' in config and 'geocoder' in config['Geolocation']:
return config['Geolocation']['geocoder'] geocoder = config['Geolocation']['geocoder']
if geocoder in ('Nominatim', ):
return geocoder
return constants.default_geocoder return constants.default_geocoder

View File

@ -35,6 +35,8 @@ def coordinates_by_name(name, db):
'latitude': geolocation_info.latitude, 'latitude': geolocation_info.latitude,
'longitude': geolocation_info.longitude 'longitude': geolocation_info.longitude
} }
else:
raise NameError(geocoder)
return None return None
@ -108,7 +110,7 @@ def place_name(lat, lon, db, cache=True, logger=logging.getLogger()):
if geocoder == 'Nominatim': if geocoder == 'Nominatim':
geolocation_info = lookup_osm(lat, lon, logger) geolocation_info = lookup_osm(lat, lon, logger)
else: else:
return None raise NameError(geocoder)
if(geolocation_info is not None and 'address' in geolocation_info): if(geolocation_info is not None and 'address' in geolocation_info):
address = geolocation_info['address'] address = geolocation_info['address']
@ -136,7 +138,6 @@ def place_name(lat, lon, db, cache=True, logger=logging.getLogger()):
def lookup_osm(lat, lon, logger=logging.getLogger()): def lookup_osm(lat, lon, logger=logging.getLogger()):
prefer_english_names = get_prefer_english_names() prefer_english_names = get_prefer_english_names()
from geopy.geocoders import Nominatim
try: try:
locator = Nominatim(user_agent='myGeocoder') locator = Nominatim(user_agent='myGeocoder')
coords = (lat, lon) coords = (lat, lon)
@ -148,7 +149,8 @@ def lookup_osm(lat, lon, logger=logging.getLogger()):
except geopy.exc.GeocoderUnavailable as e: except geopy.exc.GeocoderUnavailable as e:
logger.error(e) logger.error(e)
return None return None
except ValueError as e: # Fix *** TypeError: `address` must not be None
except (TypeError, ValueError) as e:
logger.error(e) logger.error(e)
return None return None