From 86efec07868a7d34dd91ffae40b4cb468f802fa3 Mon Sep 17 00:00:00 2001 From: Jaisen Mathai Date: Tue, 29 Dec 2015 00:07:50 -0800 Subject: [PATCH] gh-52 Add cache lookup by name and include in ./elodie.py update command --- elodie/geolocation.py | 10 ++++++++++ elodie/localstorage.py | 20 ++++++++++++++++---- elodie/tests/localstorage_test.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/elodie/geolocation.py b/elodie/geolocation.py index e83232d..41ec7e2 100644 --- a/elodie/geolocation.py +++ b/elodie/geolocation.py @@ -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): diff --git a/elodie/localstorage.py b/elodie/localstorage.py index eb343b5..9e9af3a 100644 --- a/elodie/localstorage.py +++ b/elodie/localstorage.py @@ -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: diff --git a/elodie/tests/localstorage_test.py b/elodie/tests/localstorage_test.py index af52cc2..a7077df 100644 --- a/elodie/tests/localstorage_test.py +++ b/elodie/tests/localstorage_test.py @@ -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