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)
|
return fractions.Fraction.from_float(value).limit_denominator(99999)
|
||||||
|
|
||||||
def coordinates_by_name(name):
|
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)
|
geolocation_info = lookup(name)
|
||||||
|
|
||||||
if(geolocation_info is not None):
|
if(geolocation_info is not None):
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import os
|
|
||||||
from math import radians, cos, sqrt
|
from math import radians, cos, sqrt
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
from elodie import constants
|
from elodie import constants
|
||||||
|
|
||||||
class Db(object):
|
class Db(object):
|
||||||
|
@ -90,6 +92,8 @@ class Db(object):
|
||||||
self.update_location_db()
|
self.update_location_db()
|
||||||
|
|
||||||
def get_location_name(self, latitude, longitude,threshold_m):
|
def get_location_name(self, latitude, longitude,threshold_m):
|
||||||
|
last_d = sys.maxint
|
||||||
|
name = None
|
||||||
for data in self.location_db:
|
for data in self.location_db:
|
||||||
# As threshold is quite smal use simple math
|
# 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
|
# 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
|
y = lat2 - lat1
|
||||||
d = R * sqrt( x*x + y*y )
|
d = R * sqrt( x*x + y*y )
|
||||||
# Use if closer then threshold_km reuse lookup
|
# 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
|
#print "Found in cached location dist: %d m" % d
|
||||||
return data['name'];
|
name = data['name'];
|
||||||
return None
|
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):
|
def update_location_db(self):
|
||||||
with open(constants.location_db, 'w') as f:
|
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)
|
retrieved_name = db.get_location_name(new_latitude, new_longitude, 800)
|
||||||
|
|
||||||
assert retrieved_name is None
|
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