gh-52 Add cache lookup by name and include in ./elodie.py update command
This commit is contained in:
parent
f8c4f35923
commit
86efec0786
|
@ -23,6 +23,16 @@ class Fraction(fractions.Fraction):
|
|||
return fractions.Fraction.from_float(value).limit_denominator(99999)
|
||||
|
||||
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
|
||||
geolocation_info = lookup(name)
|
||||
|
||||
if(geolocation_info is not None):
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from math import radians, cos, sqrt
|
||||
import os
|
||||
import sys
|
||||
|
||||
from elodie import constants
|
||||
|
||||
class Db(object):
|
||||
|
@ -90,6 +92,8 @@ class Db(object):
|
|||
self.update_location_db()
|
||||
|
||||
def get_location_name(self, latitude, longitude,threshold_m):
|
||||
last_d = sys.maxint
|
||||
name = None
|
||||
for data in self.location_db:
|
||||
# As threshold is quite smal use simple math
|
||||
# From http://stackoverflow.com/questions/15736995/how-can-i-quickly-estimate-the-distance-between-two-latitude-longitude-points
|
||||
|
@ -102,11 +106,19 @@ class Db(object):
|
|||
y = lat2 - lat1
|
||||
d = R * sqrt( x*x + y*y )
|
||||
# Use if closer then threshold_km reuse lookup
|
||||
if(d<=threshold_m):
|
||||
if(d <= threshold_m and d < last_d):
|
||||
#print "Found in cached location dist: %d m" % d
|
||||
return data['name'];
|
||||
return None
|
||||
name = data['name'];
|
||||
last_d = d
|
||||
|
||||
return name
|
||||
|
||||
def get_location_coordinates(self, name):
|
||||
for data in self.location_db:
|
||||
if data['name'] == name:
|
||||
return (data['lat'], data['long'])
|
||||
|
||||
return None
|
||||
|
||||
def update_location_db(self):
|
||||
with open(constants.location_db, 'w') as f:
|
||||
|
|
|
@ -177,3 +177,33 @@ def test_get_location_name_outside_threshold():
|
|||
retrieved_name = db.get_location_name(new_latitude, new_longitude, 800)
|
||||
|
||||
assert retrieved_name is None
|
||||
|
||||
def test_get_location_coordinates_exists():
|
||||
db = Db()
|
||||
|
||||
latitude, longitude, name = helper.get_test_location()
|
||||
|
||||
name = '%s-%s' % (name, helper.random_string(10))
|
||||
latitude = helper.random_coordinate(latitude, 1)
|
||||
longitude = helper.random_coordinate(longitude, 1)
|
||||
|
||||
db.add_location(latitude, longitude, name)
|
||||
|
||||
location = db.get_location_coordinates(name)
|
||||
|
||||
assert location is not None
|
||||
assert location[0] == latitude
|
||||
assert location[1] == longitude
|
||||
|
||||
def test_get_location_coordinates_does_not_exists():
|
||||
db = Db()
|
||||
|
||||
latitude, longitude, name = helper.get_test_location()
|
||||
|
||||
name = '%s-%s' % (name, helper.random_string(10))
|
||||
latitude = helper.random_coordinate(latitude, 1)
|
||||
longitude = helper.random_coordinate(longitude, 1)
|
||||
|
||||
location = db.get_location_coordinates(name)
|
||||
|
||||
assert location is None
|
||||
|
|
Loading…
Reference in New Issue