ordigi/elodie/geolocation.py

204 lines
5.9 KiB
Python
Raw Normal View History

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
2015-10-08 11:21:31 +02:00
from os import path
2016-03-12 20:09:28 +01:00
from configparser import ConfigParser
2016-08-22 08:10:45 +02:00
standard_library.install_aliases() # noqa
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
from elodie import constants
from elodie.localstorage import Db
__KEY__ = None
def coordinates_by_name(name):
# Try to get cached location first
db = Db()
cached_coordinates = db.get_location_coordinates(name)
if(cached_coordinates is not None):
return {
'latitude': cached_coordinates[0],
'longitude': cached_coordinates[1]
}
# 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)
if(geolocation_info is not None):
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.
# If we find a city we set that to the use_location and break
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'
):
use_location = location['latLng']
break
return {
'latitude': use_location['lat'],
'longitude': use_location['lng']
}
return None
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)
degrees = degrees
sign = 1 if decimal >= 0 else -1
2016-06-24 06:31:58 +02:00
return (degrees, minutes, seconds, sign)
def dms_to_decimal(degrees, minutes, seconds, direction=' '):
sign = 1
2016-02-12 20:22:26 +01:00
if(direction[0] in 'WSws'):
sign = -1
return (
2016-08-22 08:10:45 +02:00
float(degrees) + old_div(float(minutes), 60) +
old_div(float(seconds), 3600)
) * sign
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)
def get_key():
global __KEY__
if __KEY__ is not None:
return __KEY__
config_file = '%s/config.ini' % constants.application_directory
if not path.exists(config_file):
2015-10-08 11:21:31 +02:00
return None
2015-10-08 11:21:31 +02:00
config = ConfigParser()
config.read(config_file)
2015-10-08 11:21:31 +02:00
if('MapQuest' not in config.sections()):
return None
__KEY__ = config.get('MapQuest', 'key')
return __KEY__
2015-10-08 11:21:31 +02:00
2015-10-08 11:21:31 +02:00
def place_name(lat, lon):
# Convert lat/lon to floats
if not isinstance(lat, float):
lat = float(lat)
if not isinstance(lon, float):
lon = float(lon)
# Try to get cached location first
db = Db()
# 3km distace radious for a match
cached_place_name = db.get_location_name(lat, lon, 3000)
if(cached_place_name is not None):
return cached_place_name
lookup_place_name = None
2016-10-26 07:10:48 +02:00
geolocation_info = lookup(lat=lat, lon=lon)
2015-10-08 11:21:31 +02:00
if(geolocation_info is not None):
if('address' in geolocation_info):
address = geolocation_info['address']
if('city' in address):
lookup_place_name = address['city']
2015-10-08 11:21:31 +02:00
elif('state' in address):
lookup_place_name = address['state']
2015-10-08 11:21:31 +02:00
elif('country' in address):
lookup_place_name = address['country']
if(lookup_place_name is not None):
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()
return lookup_place_name
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
):
return None
key = get_key()
if(key is None):
return None
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'
url = 'http://open.mapquestapi.com%s?%s' % (
path,
urllib.parse.urlencode(params)
)
if(constants.debug is True):
print(url)
r = requests.get(url)
return parse_result(r.json())
except requests.exceptions.RequestException as e:
if(constants.debug is True):
2016-03-12 20:09:28 +01:00
print(e)
return None
except ValueError as e:
if(constants.debug is True):
2016-03-12 20:09:28 +01:00
print(r.text)
print(e)
return None
2016-10-26 07:10:48 +02:00
def parse_result(result):
if('error' in result):
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