Browse files

Fallback on Google geocoding if Nominatim fails

  • Loading branch information...
1 parent 82ef4ee commit 3540b49c90f91ec22a8667d84c2390c95659b235 @julienr committed Feb 27, 2013
Showing with 32 additions and 7 deletions.
  1. +4 −2 ziviscrap/geocode.py
  2. +1 −1 ziviscrap/postprocess.py
  3. +27 −4 ziviscrap/test/geocode_test.py
View
6 ziviscrap/geocode.py
@@ -104,11 +104,13 @@ def get_locality(resaddr):
raise GeocodingError('Keyerror : %s' %e, url=url)
def geocode_address(raw_address):
- #address = _google_geocode(raw_address)
if raw_address in geocache:
return geocache[raw_address]
else:
- address = _nominatim_geocode(raw_address)
+ try:
+ address = _nominatim_geocode(raw_address)
+ except GeocodingError:
+ address = _google_geocode(raw_address)
geocache[raw_address] = address
return address
View
2 ziviscrap/postprocess.py
@@ -67,7 +67,7 @@ def span_text(id):
data['institution_number'] = span_text("EIBNumberValue")
data['activity_domain'] = translations.to_activity_domain(
span_text("fieldOfActivityValue"))
- data['priority_program'] = span_text("schwerpunktProgramValue")
+ data['priority_program'] = span_text("schwerpunktProgrammValue")
data['job_function_category'] = span_text("functionsValue")
data['place_of_work'] = span_text("placeOfWorkValue")
data['work_abroad'] = span_text("workAbroadValue")
View
31 ziviscrap/test/geocode_test.py
@@ -8,10 +8,13 @@
datapath = os.path.dirname(testdata.__file__)
+def get_contents(fname):
+ with open(os.path.join(datapath, fname)) as f:
+ return f.read()
+
class TestNominatimGeocode(unittest.TestCase):
def test_sample_data(self):
- with open(os.path.join(datapath, 'geocode_nominatim.json')) as f:
- jsondata = f.read()
+ jsondata = get_contents('geocode_nominatim.json')
with patch('urllib2.urlopen') as mock:
mock.return_value = StringIO(jsondata)
address = geocode._nominatim_geocode('')
@@ -24,8 +27,7 @@ def test_sample_data(self):
class TestGoogleGeocode(unittest.TestCase):
def test_sample_data(self):
- with open(os.path.join(datapath, 'geocode_google.json')) as f:
- jsondata = f.read()
+ jsondata = get_contents('geocode_google.json')
with patch('urllib2.urlopen') as mock:
mock.return_value = StringIO(jsondata)
address = geocode._google_geocode('')
@@ -35,3 +37,24 @@ def test_sample_data(self):
assert address['longitude'] == 6.847530000000001
assert address['formatted_address'] == '1470 Estavayer-le-Lac, '\
'Switzerland'
+
+class TestGoogleFallback(unittest.TestCase):
+ """When nominatim geocoding fails, we want to use Google as a fallback"""
+ def test_sample_data(self):
+ nominatimdata = '[]'
+ googledata = get_contents('geocode_google.json')
+ # Consecutive calls will return results in order
+ results = [StringIO(nominatimdata), StringIO(googledata)]
+ def side_effect(*args):
+ return results.pop(0)
+ with patch('urllib2.urlopen', side_effect=side_effect) as mock:
+ address = geocode.geocode_address('')
+ assert address['canton'] == 'Canton of Fribourg'
+ assert address['locality'] == 'Estavayer-le-Lac'
+ assert address['latitude'] == 46.849340
+ assert address['longitude'] == 6.847530000000001
+ assert address['formatted_address'] == '1470 Estavayer-le-Lac, '\
+ 'Switzerland'
+
+
+

0 comments on commit 3540b49

Please sign in to comment.