2016-03-12 20:09:28 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from builtins import range
|
|
|
|
from past.utils import old_div
|
2015-12-19 04:19:48 +01:00
|
|
|
# Project imports
|
2016-10-26 07:10:48 +02:00
|
|
|
import mock
|
2015-12-19 04:19:48 +01:00
|
|
|
import os
|
2015-12-19 09:04:44 +01:00
|
|
|
import random
|
2015-12-19 04:19:48 +01:00
|
|
|
import re
|
2015-12-19 09:04:44 +01:00
|
|
|
import sys
|
2016-10-08 19:02:16 +02:00
|
|
|
from mock import patch
|
2017-01-03 05:58:52 +01:00
|
|
|
from tempfile import gettempdir
|
2015-12-19 04:19:48 +01:00
|
|
|
|
2021-06-27 07:00:04 +02:00
|
|
|
# sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))
|
2015-12-19 04:19:48 +01:00
|
|
|
|
2016-03-12 20:09:28 +01:00
|
|
|
from . import helper
|
2015-12-19 04:19:48 +01:00
|
|
|
from elodie import geolocation
|
2021-06-27 07:00:04 +02:00
|
|
|
from elodie.localstorage import Db
|
2015-12-19 04:19:48 +01:00
|
|
|
|
|
|
|
os.environ['TZ'] = 'GMT'
|
|
|
|
|
2021-06-27 07:00:04 +02:00
|
|
|
ELODIE_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
2016-06-21 20:19:40 +02:00
|
|
|
|
2015-12-19 04:19:48 +01:00
|
|
|
def test_decimal_to_dms():
|
|
|
|
|
2015-12-19 09:04:44 +01:00
|
|
|
for x in range(0, 1000):
|
|
|
|
target_decimal_value = random.uniform(0.0, 180.0)
|
|
|
|
if(x % 2 == 1):
|
|
|
|
target_decimal_value = target_decimal_value * -1
|
2016-03-12 20:09:28 +01:00
|
|
|
|
2015-12-19 09:04:44 +01:00
|
|
|
dms = geolocation.decimal_to_dms(target_decimal_value)
|
2016-06-21 20:19:40 +02:00
|
|
|
|
|
|
|
check_value = (dms[0] + dms[1] / 60 + dms[2] / 3600) * dms[3]
|
2015-12-19 09:04:44 +01:00
|
|
|
|
|
|
|
target_decimal_value = round(target_decimal_value, 8)
|
|
|
|
check_value = round(check_value, 8)
|
2015-12-19 04:19:48 +01:00
|
|
|
|
2015-12-19 09:04:44 +01:00
|
|
|
assert target_decimal_value == check_value, '%s does not match %s' % (check_value, target_decimal_value)
|
2015-12-27 09:10:19 +01:00
|
|
|
|
2016-09-13 07:23:24 +02:00
|
|
|
def test_dms_to_decimal_positive_sign():
|
|
|
|
decimal = geolocation.dms_to_decimal(10, 20, 100, 'NE')
|
|
|
|
assert helper.isclose(decimal, 10.3611111111)
|
|
|
|
|
|
|
|
decimal = geolocation.dms_to_decimal(10, 20, 100, 'ne')
|
|
|
|
assert helper.isclose(decimal, 10.3611111111)
|
|
|
|
|
|
|
|
def test_dms_to_decimal_negative_sign():
|
|
|
|
decimal = geolocation.dms_to_decimal(10, 20, 100, 'SW')
|
|
|
|
assert helper.isclose(decimal, -10.3611111111)
|
|
|
|
|
|
|
|
decimal = geolocation.dms_to_decimal(10, 20, 100, 'sw')
|
|
|
|
assert helper.isclose(decimal, -10.3611111111)
|
|
|
|
|
2016-06-21 20:19:40 +02:00
|
|
|
def test_dms_string_latitude():
|
2015-12-27 09:10:19 +01:00
|
|
|
|
2016-06-21 20:19:40 +02:00
|
|
|
for x in range(0, 5):
|
|
|
|
target_decimal_value = random.uniform(0.0, 180.0)
|
|
|
|
if(x % 2 == 1):
|
|
|
|
target_decimal_value = target_decimal_value * -1
|
2016-03-12 20:09:28 +01:00
|
|
|
|
2016-06-21 20:19:40 +02:00
|
|
|
dms = geolocation.decimal_to_dms(target_decimal_value)
|
|
|
|
dms_string = geolocation.dms_string(target_decimal_value, 'latitude')
|
2015-12-27 09:10:19 +01:00
|
|
|
|
2016-06-21 20:19:40 +02:00
|
|
|
check_value = 'N' if target_decimal_value >= 0 else 'S'
|
|
|
|
|
|
|
|
assert check_value in dms_string, '%s not in %s' % (check_value, dms_string)
|
|
|
|
assert str(dms[0]) in dms_string, '%s not in %s' % (dms[0], dms_string)
|
|
|
|
|
|
|
|
def test_dms_string_longitude():
|
|
|
|
|
|
|
|
for x in range(0, 5):
|
|
|
|
target_decimal_value = random.uniform(0.0, 180.0)
|
|
|
|
if(x % 2 == 1):
|
|
|
|
target_decimal_value = target_decimal_value * -1
|
2016-03-12 20:09:28 +01:00
|
|
|
|
2016-06-21 20:19:40 +02:00
|
|
|
dms = geolocation.decimal_to_dms(target_decimal_value)
|
|
|
|
dms_string = geolocation.dms_string(target_decimal_value, 'longitude')
|
2015-12-27 09:10:19 +01:00
|
|
|
|
2016-06-21 20:19:40 +02:00
|
|
|
check_value = 'E' if target_decimal_value >= 0 else 'W'
|
2015-12-27 09:10:19 +01:00
|
|
|
|
2016-06-21 20:19:40 +02:00
|
|
|
assert check_value in dms_string, '%s not in %s' % (check_value, dms_string)
|
|
|
|
assert str(dms[0]) in dms_string, '%s not in %s' % (dms[0], dms_string)
|
2016-09-14 05:10:29 +02:00
|
|
|
|
2016-10-26 07:10:48 +02:00
|
|
|
def test_reverse_lookup_with_valid_key():
|
2021-04-16 20:22:27 +02:00
|
|
|
res = geolocation.lookup_osm(lat=37.368, lon=-122.03)
|
2016-10-26 07:10:48 +02:00
|
|
|
assert res['address']['city'] == 'Sunnyvale', res
|
|
|
|
|
|
|
|
def test_reverse_lookup_with_invalid_lat_lon():
|
2021-04-16 20:22:27 +02:00
|
|
|
res = geolocation.lookup_osm(lat=999, lon=999)
|
2016-10-26 07:10:48 +02:00
|
|
|
assert res is None, res
|
|
|
|
|
|
|
|
@mock.patch('elodie.geolocation.__KEY__', 'invalid_key')
|
2016-09-14 05:10:29 +02:00
|
|
|
def test_reverse_lookup_with_invalid_key():
|
2021-04-16 20:22:27 +02:00
|
|
|
res = geolocation.lookup_mapquest(lat=37.368, lon=-122.03)
|
2016-09-14 05:10:29 +02:00
|
|
|
assert res is None, res
|
|
|
|
|
2016-10-26 07:10:48 +02:00
|
|
|
def test_lookup_with_valid_key():
|
2021-04-16 20:22:27 +02:00
|
|
|
res = geolocation.lookup_mapquest(location='Sunnyvale, CA')
|
2016-10-26 07:10:48 +02:00
|
|
|
latLng = res['results'][0]['locations'][0]['latLng']
|
|
|
|
assert latLng['lat'] == 37.36883, latLng
|
|
|
|
assert latLng['lng'] == -122.03635, latLng
|
|
|
|
|
|
|
|
def test_lookup_with_invalid_location():
|
2021-04-16 20:22:27 +02:00
|
|
|
res = geolocation.lookup_mapquest(location='foobar dne')
|
2016-10-08 19:02:16 +02:00
|
|
|
assert res is None, res
|
|
|
|
|
2016-10-30 07:54:20 +01:00
|
|
|
def test_lookup_with_invalid_location():
|
2021-04-16 20:22:27 +02:00
|
|
|
res = geolocation.lookup_mapquest(location='foobar dne')
|
2016-10-30 07:54:20 +01:00
|
|
|
assert res is None, res
|
|
|
|
|
|
|
|
def test_lookup_with_valid_key():
|
2021-04-16 20:22:27 +02:00
|
|
|
res = geolocation.lookup_mapquest(location='Sunnyvale, CA')
|
2016-10-30 07:54:20 +01:00
|
|
|
latLng = res['results'][0]['locations'][0]['latLng']
|
|
|
|
assert latLng['lat'] == 37.36883, latLng
|
|
|
|
assert latLng['lng'] == -122.03635, latLng
|
|
|
|
|
2019-01-22 05:27:38 +01:00
|
|
|
@mock.patch('elodie.geolocation.__PREFER_ENGLISH_NAMES__', True)
|
|
|
|
def test_lookup_with_prefer_english_names_true():
|
2021-04-16 20:22:27 +02:00
|
|
|
res = geolocation.lookup_osm(lat=55.66333, lon=37.61583)
|
|
|
|
assert res['address']['city'] == 'Moscow', res
|
2019-01-22 05:27:38 +01:00
|
|
|
|
|
|
|
@mock.patch('elodie.geolocation.__PREFER_ENGLISH_NAMES__', False)
|
|
|
|
def test_lookup_with_prefer_english_names_false():
|
2021-04-16 20:22:27 +02:00
|
|
|
res = geolocation.lookup_osm(lat=55.66333, lon=37.61583)
|
|
|
|
assert res['address']['city'] == 'Москва', res
|
2019-01-22 05:27:38 +01:00
|
|
|
|
2017-01-03 05:58:52 +01:00
|
|
|
@mock.patch('elodie.constants.location_db', '%s/location.json-cached' % gettempdir())
|
|
|
|
def test_place_name_deprecated_string_cached():
|
|
|
|
# See gh-160 for backwards compatability needed when a string is stored instead of a dict
|
|
|
|
with open('%s/location.json-cached' % gettempdir(), 'w') as f:
|
|
|
|
f.write("""
|
|
|
|
[{"lat": 37.3667027222222, "long": -122.033383611111, "name": "OLDVALUE"}]
|
|
|
|
"""
|
|
|
|
)
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(ELODIE_DIR)
|
2021-06-12 19:49:29 +02:00
|
|
|
place_name = geolocation.place_name(37.3667027222222, -122.033383611111,
|
|
|
|
db)
|
2017-01-03 05:58:52 +01:00
|
|
|
|
|
|
|
assert place_name['city'] == 'Sunnyvale', place_name
|
|
|
|
|
|
|
|
@mock.patch('elodie.constants.location_db', '%s/location.json-cached' % gettempdir())
|
|
|
|
def test_place_name_cached():
|
|
|
|
with open('%s/location.json-cached' % gettempdir(), 'w') as f:
|
|
|
|
f.write("""
|
|
|
|
[{"lat": 37.3667027222222, "long": -122.033383611111, "name": {"city": "UNITTEST"}}]
|
|
|
|
"""
|
|
|
|
)
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(ELODIE_DIR)
|
2021-06-12 19:49:29 +02:00
|
|
|
place_name = geolocation.place_name(37.3667027222222, -122.033383611111,
|
|
|
|
db)
|
2017-01-03 05:58:52 +01:00
|
|
|
|
|
|
|
assert place_name['city'] == 'UNITTEST', place_name
|
|
|
|
|
2017-03-16 06:43:21 +01:00
|
|
|
def test_place_name_no_default():
|
|
|
|
# See gh-160 for backwards compatability needed when a string is stored instead of a dict
|
2021-06-27 07:00:04 +02:00
|
|
|
db = Db(ELODIE_DIR)
|
2021-06-12 19:49:29 +02:00
|
|
|
place_name = geolocation.place_name(123456.000, 123456.000, db)
|
2017-03-16 06:43:21 +01:00
|
|
|
|
|
|
|
assert place_name['default'] == 'Unknown Location', place_name
|
|
|
|
|
2016-10-26 07:10:48 +02:00
|
|
|
@mock.patch('elodie.geolocation.__KEY__', 'invalid_key')
|
2016-09-14 05:10:29 +02:00
|
|
|
def test_lookup_with_invalid_key():
|
2021-04-16 20:22:27 +02:00
|
|
|
res = geolocation.lookup_mapquest(location='Sunnyvale, CA')
|
2016-10-26 07:10:48 +02:00
|
|
|
assert res is None, res
|
|
|
|
|
|
|
|
@mock.patch('elodie.geolocation.__KEY__', '')
|
|
|
|
def test_lookup_with_no_key():
|
2021-04-16 20:22:27 +02:00
|
|
|
res = geolocation.lookup_mapquest(location='Sunnyvale, CA')
|
2016-10-26 07:10:48 +02:00
|
|
|
assert res is None, res
|
|
|
|
|
|
|
|
def test_parse_result_with_error():
|
|
|
|
res = geolocation.parse_result({'error': 'foo'})
|
2016-09-14 05:10:29 +02:00
|
|
|
assert res is None, res
|
2016-10-08 19:02:16 +02:00
|
|
|
|
2016-10-26 07:10:48 +02:00
|
|
|
def test_parse_result_with_city():
|
|
|
|
# http://open.mapquestapi.com/nominatim/v1/reverse.php?lat=37.368&lon=-122.03&key=key_goes_here&format=json
|
|
|
|
results = {
|
|
|
|
"place_id": "60197412",
|
|
|
|
"osm_type": "way",
|
|
|
|
"osm_id": "30907961",
|
|
|
|
"lat": "37.36746105",
|
|
|
|
"lon": "-122.030237558742",
|
|
|
|
"display_name": "111, East El Camino Real, Sunnyvale, Santa Clara County, California, 94087, United States of America",
|
|
|
|
"address": {
|
|
|
|
"house_number": "111",
|
|
|
|
"road": "East El Camino Real",
|
|
|
|
"city": "Sunnyvale",
|
|
|
|
"county": "Santa Clara County",
|
|
|
|
"state": "California",
|
|
|
|
"postcode": "94087",
|
|
|
|
"country": "United States of America",
|
|
|
|
"country_code": "us"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = geolocation.parse_result(results)
|
|
|
|
assert res == results, res
|
|
|
|
|
|
|
|
def test_parse_result_with_lat_lon():
|
|
|
|
# http://open.mapquestapi.com/geocoding/v1/address?location=abcdefghijklmnopqrstuvwxyz&key=key_goes_here&format=json
|
|
|
|
results = {
|
|
|
|
"results": [
|
|
|
|
{
|
|
|
|
"locations": [
|
|
|
|
{
|
|
|
|
"latLng": {
|
|
|
|
"lat": 123.00,
|
|
|
|
"lng": -142.99
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
res = geolocation.parse_result(results)
|
|
|
|
assert res == results, res
|
|
|
|
|
|
|
|
def test_parse_result_with_unknown_lat_lon():
|
|
|
|
# http://open.mapquestapi.com/geocoding/v1/address?location=abcdefghijklmnopqrstuvwxyz&key=key_goes_here&format=json
|
|
|
|
results = {
|
|
|
|
"results": [
|
|
|
|
{
|
|
|
|
"locations": [
|
|
|
|
{
|
|
|
|
"latLng": {
|
|
|
|
"lat": 39.78373,
|
|
|
|
"lng": -100.445882
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
res = geolocation.parse_result(results)
|
2016-10-08 19:02:16 +02:00
|
|
|
assert res is None, res
|