2016-01-08 23:49:06 +01:00
|
|
|
"""Look up geolocation information for media objects."""
|
2016-03-12 20:09:28 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import division
|
|
|
|
from future import standard_library
|
|
|
|
from past.utils import old_div
|
2016-01-08 23:49:06 +01:00
|
|
|
|
2016-11-08 05:34:25 +01:00
|
|
|
standard_library.install_aliases() # noqa
|
|
|
|
|
2015-10-08 11:21:31 +02:00
|
|
|
from os import path
|
2016-08-22 08:10:45 +02:00
|
|
|
|
2015-10-08 11:21:31 +02:00
|
|
|
import requests
|
2016-08-22 08:10:45 +02:00
|
|
|
import urllib.request
|
|
|
|
import urllib.parse
|
|
|
|
import urllib.error
|
2015-10-08 11:21:31 +02:00
|
|
|
|
2017-01-03 05:58:52 +01:00
|
|
|
from elodie.config import load_config
|
2015-10-28 08:19:21 +01:00
|
|
|
from elodie import constants
|
2016-11-09 07:41:00 +01:00
|
|
|
from elodie import log
|
2015-12-24 20:39:28 +01:00
|
|
|
from elodie.localstorage import Db
|
2015-10-28 08:19:21 +01:00
|
|
|
|
2016-09-14 05:10:29 +02:00
|
|
|
__KEY__ = None
|
2017-03-30 16:13:34 +02:00
|
|
|
__DEFAULT_LOCATION__ = 'Unknown Location'
|
2019-01-22 05:27:38 +01:00
|
|
|
__PREFER_ENGLISH_NAMES__ = None
|
2016-09-14 05:10:29 +02:00
|
|
|
|
2016-01-02 08:23:06 +01:00
|
|
|
|
2015-10-14 05:26:55 +02:00
|
|
|
def coordinates_by_name(name):
|
2015-12-29 09:07:50 +01:00
|
|
|
# Try to get cached location first
|
|
|
|
db = Db()
|
|
|
|
cached_coordinates = db.get_location_coordinates(name)
|
|
|
|
if(cached_coordinates is not None):
|
|
|
|
return {
|
2016-01-02 18:34:43 +01:00
|
|
|
'latitude': cached_coordinates[0],
|
|
|
|
'longitude': cached_coordinates[1]
|
2015-12-29 09:07:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# If the name is not cached then we go ahead with an API lookup
|
2016-10-26 07:10:48 +02:00
|
|
|
geolocation_info = lookup(location=name)
|
2015-10-14 05:26:55 +02:00
|
|
|
|
|
|
|
if(geolocation_info is not None):
|
2016-01-02 08:23:06 +01:00
|
|
|
if(
|
|
|
|
'results' in geolocation_info and
|
|
|
|
len(geolocation_info['results']) != 0 and
|
|
|
|
'locations' in geolocation_info['results'][0] and
|
|
|
|
len(geolocation_info['results'][0]['locations']) != 0
|
|
|
|
):
|
|
|
|
|
|
|
|
# By default we use the first entry unless we find one with
|
|
|
|
# geocodeQuality=city.
|
|
|
|
geolocation_result = geolocation_info['results'][0]
|
|
|
|
use_location = geolocation_result['locations'][0]['latLng']
|
|
|
|
# Loop over the locations to see if we come accross a
|
|
|
|
# geocodeQuality=city.
|
2015-10-14 05:26:55 +02:00
|
|
|
# If we find a city we set that to the use_location and break
|
2016-01-02 08:23:06 +01:00
|
|
|
for location in geolocation_result['locations']:
|
|
|
|
if(
|
|
|
|
'latLng' in location and
|
|
|
|
'lat' in location['latLng'] and
|
|
|
|
'lng' in location['latLng'] and
|
|
|
|
location['geocodeQuality'].lower() == 'city'
|
|
|
|
):
|
2015-10-14 05:26:55 +02:00
|
|
|
use_location = location['latLng']
|
|
|
|
break
|
2016-01-02 08:23:06 +01:00
|
|
|
|
2015-10-14 05:26:55 +02:00
|
|
|
return {
|
2016-01-02 18:34:43 +01:00
|
|
|
'latitude': use_location['lat'],
|
|
|
|
'longitude': use_location['lng']
|
2015-10-14 05:26:55 +02:00
|
|
|
}
|
2016-01-02 08:23:06 +01:00
|
|
|
|
2015-10-14 05:26:55 +02:00
|
|
|
return None
|
|
|
|
|
2016-01-02 08:23:06 +01:00
|
|
|
|
2016-06-21 20:19:40 +02:00
|
|
|
def decimal_to_dms(decimal):
|
|
|
|
decimal = float(decimal)
|
|
|
|
decimal_abs = abs(decimal)
|
2016-06-24 06:31:58 +02:00
|
|
|
minutes, seconds = divmod(decimal_abs*3600, 60)
|
|
|
|
degrees, minutes = divmod(minutes, 60)
|
2016-06-21 20:19:40 +02:00
|
|
|
degrees = degrees
|
|
|
|
sign = 1 if decimal >= 0 else -1
|
2016-06-24 06:31:58 +02:00
|
|
|
return (degrees, minutes, seconds, sign)
|
2015-10-14 05:26:55 +02:00
|
|
|
|
2016-01-02 08:23:06 +01:00
|
|
|
|
|
|
|
def dms_to_decimal(degrees, minutes, seconds, direction=' '):
|
|
|
|
sign = 1
|
2016-02-12 20:22:26 +01:00
|
|
|
if(direction[0] in 'WSws'):
|
2016-01-02 08:23:06 +01:00
|
|
|
sign = -1
|
|
|
|
return (
|
2016-08-22 08:10:45 +02:00
|
|
|
float(degrees) + old_div(float(minutes), 60) +
|
|
|
|
old_div(float(seconds), 3600)
|
2016-01-02 08:23:06 +01:00
|
|
|
) * sign
|
|
|
|
|
|
|
|
|
2016-06-21 20:19:40 +02:00
|
|
|
def dms_string(decimal, type='latitude'):
|
|
|
|
# Example string -> 38 deg 14' 27.82" S
|
|
|
|
dms = decimal_to_dms(decimal)
|
|
|
|
if type == 'latitude':
|
|
|
|
direction = 'N' if decimal >= 0 else 'S'
|
|
|
|
elif type == 'longitude':
|
|
|
|
direction = 'E' if decimal >= 0 else 'W'
|
|
|
|
return '{} deg {}\' {}" {}'.format(dms[0], dms[1], dms[2], direction)
|
|
|
|
|
|
|
|
|
2015-10-14 05:26:55 +02:00
|
|
|
def get_key():
|
2016-09-14 05:10:29 +02:00
|
|
|
global __KEY__
|
|
|
|
if __KEY__ is not None:
|
|
|
|
return __KEY__
|
|
|
|
|
2019-04-01 09:56:37 +02:00
|
|
|
if constants.mapquest_key is not None:
|
|
|
|
__KEY__ = constants.mapquest_key
|
|
|
|
return __KEY__
|
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
config = load_config(constants.CONFIG_FILE)
|
|
|
|
if('Geolocation' not in config):
|
2015-10-08 11:21:31 +02:00
|
|
|
return None
|
2016-01-02 08:23:06 +01:00
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
__KEY__ = config['Geolocation']['mapquest_key']
|
2016-09-14 05:10:29 +02:00
|
|
|
return __KEY__
|
2015-10-08 11:21:31 +02:00
|
|
|
|
2019-01-22 05:27:38 +01:00
|
|
|
def get_prefer_english_names():
|
|
|
|
global __PREFER_ENGLISH_NAMES__
|
|
|
|
if __PREFER_ENGLISH_NAMES__ is not None:
|
|
|
|
return __PREFER_ENGLISH_NAMES__
|
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
config = load_config(constants.CONFIG_FILE)
|
|
|
|
if('prefer_english_names' not in config['Geolocation']):
|
2019-01-22 05:27:38 +01:00
|
|
|
return False
|
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
__PREFER_ENGLISH_NAMES__ = bool(config['Geolocation']['prefer_english_names'])
|
2019-01-22 05:27:38 +01:00
|
|
|
return __PREFER_ENGLISH_NAMES__
|
2016-01-02 08:23:06 +01:00
|
|
|
|
2015-10-08 11:21:31 +02:00
|
|
|
def place_name(lat, lon):
|
2017-03-30 16:13:34 +02:00
|
|
|
lookup_place_name_default = {'default': __DEFAULT_LOCATION__}
|
|
|
|
if(lat is None or lon is None):
|
|
|
|
return lookup_place_name_default
|
|
|
|
|
2016-04-21 07:23:57 +02:00
|
|
|
# Convert lat/lon to floats
|
2017-03-30 16:13:34 +02:00
|
|
|
if(not isinstance(lat, float)):
|
2016-04-21 07:23:57 +02:00
|
|
|
lat = float(lat)
|
2017-03-30 16:13:34 +02:00
|
|
|
if(not isinstance(lon, float)):
|
2016-04-21 07:23:57 +02:00
|
|
|
lon = float(lon)
|
2015-12-24 20:39:28 +01:00
|
|
|
|
|
|
|
# Try to get cached location first
|
|
|
|
db = Db()
|
|
|
|
# 3km distace radious for a match
|
2016-01-02 08:23:06 +01:00
|
|
|
cached_place_name = db.get_location_name(lat, lon, 3000)
|
2017-01-03 05:58:52 +01:00
|
|
|
# We check that it's a dict to coerce an upgrade of the location
|
|
|
|
# db from a string location to a dictionary. See gh-160.
|
|
|
|
if(isinstance(cached_place_name, dict)):
|
2015-12-24 20:39:28 +01:00
|
|
|
return cached_place_name
|
|
|
|
|
2017-01-03 05:58:52 +01:00
|
|
|
lookup_place_name = {}
|
2016-10-26 07:10:48 +02:00
|
|
|
geolocation_info = lookup(lat=lat, lon=lon)
|
2017-03-30 16:13:34 +02:00
|
|
|
if(geolocation_info is not None and 'address' in geolocation_info):
|
|
|
|
address = geolocation_info['address']
|
2020-10-23 09:48:19 +02:00
|
|
|
# gh-386 adds support for town
|
|
|
|
# taking precedence after city for backwards compatability
|
|
|
|
for loc in ['city', 'town', 'state', 'country']:
|
2017-03-30 16:13:34 +02:00
|
|
|
if(loc in address):
|
|
|
|
lookup_place_name[loc] = address[loc]
|
|
|
|
# In many cases the desired key is not available so we
|
|
|
|
# set the most specific as the default.
|
|
|
|
if('default' not in lookup_place_name):
|
|
|
|
lookup_place_name['default'] = address[loc]
|
2017-01-03 05:58:52 +01:00
|
|
|
|
2017-09-08 01:59:16 +02:00
|
|
|
if(lookup_place_name):
|
2015-12-24 20:39:28 +01:00
|
|
|
db.add_location(lat, lon, lookup_place_name)
|
|
|
|
# TODO: Maybe this should only be done on exit and not for every write.
|
|
|
|
db.update_location_db()
|
2017-04-07 22:40:55 +02:00
|
|
|
|
|
|
|
if('default' not in lookup_place_name):
|
|
|
|
lookup_place_name = lookup_place_name_default
|
|
|
|
|
2015-12-24 20:39:28 +01:00
|
|
|
return lookup_place_name
|
2015-10-14 05:26:55 +02:00
|
|
|
|
|
|
|
|
2016-10-26 07:10:48 +02:00
|
|
|
def lookup(**kwargs):
|
|
|
|
if(
|
|
|
|
'location' not in kwargs and
|
|
|
|
'lat' not in kwargs and
|
|
|
|
'lon' not in kwargs
|
|
|
|
):
|
2015-10-14 05:26:55 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
key = get_key()
|
2019-01-22 05:27:38 +01:00
|
|
|
prefer_english_names = get_prefer_english_names()
|
2015-10-14 05:26:55 +02:00
|
|
|
|
2016-10-08 19:02:16 +02:00
|
|
|
if(key is None):
|
|
|
|
return None
|
|
|
|
|
2015-10-14 05:26:55 +02:00
|
|
|
try:
|
2016-10-26 07:10:48 +02:00
|
|
|
params = {'format': 'json', 'key': key}
|
|
|
|
params.update(kwargs)
|
|
|
|
path = '/geocoding/v1/address'
|
|
|
|
if('lat' in kwargs and 'lon' in kwargs):
|
|
|
|
path = '/nominatim/v1/reverse.php'
|
2019-04-01 09:56:37 +02:00
|
|
|
url = '%s%s?%s' % (
|
|
|
|
constants.mapquest_base_url,
|
2016-10-26 07:10:48 +02:00
|
|
|
path,
|
|
|
|
urllib.parse.urlencode(params)
|
|
|
|
)
|
2019-01-22 05:27:38 +01:00
|
|
|
headers = {}
|
|
|
|
if(prefer_english_names):
|
|
|
|
headers = {'Accept-Language':'en-EN,en;q=0.8'}
|
|
|
|
r = requests.get(url, headers=headers)
|
2016-10-26 07:10:48 +02:00
|
|
|
return parse_result(r.json())
|
2015-10-14 05:26:55 +02:00
|
|
|
except requests.exceptions.RequestException as e:
|
2016-11-09 07:41:00 +01:00
|
|
|
log.error(e)
|
2015-10-14 05:26:55 +02:00
|
|
|
return None
|
|
|
|
except ValueError as e:
|
2016-11-09 07:41:00 +01:00
|
|
|
log.error(r.text)
|
|
|
|
log.error(e)
|
2015-10-14 05:26:55 +02:00
|
|
|
return None
|
|
|
|
|
2016-01-02 08:23:06 +01:00
|
|
|
|
2016-10-26 07:10:48 +02:00
|
|
|
def parse_result(result):
|
|
|
|
if('error' in result):
|
2015-10-14 05:26:55 +02:00
|
|
|
return None
|
|
|
|
|
2016-10-26 07:10:48 +02:00
|
|
|
if(
|
|
|
|
'results' in result and
|
|
|
|
len(result['results']) > 0 and
|
|
|
|
'locations' in result['results'][0]
|
|
|
|
and len(result['results'][0]['locations']) > 0 and
|
|
|
|
'latLng' in result['results'][0]['locations'][0]
|
|
|
|
):
|
|
|
|
latLng = result['results'][0]['locations'][0]['latLng']
|
|
|
|
if(latLng['lat'] == 39.78373 and latLng['lng'] == -100.445882):
|
|
|
|
return None
|
|
|
|
|
|
|
|
return result
|