From 2b02fc3dffc6c1d1db81d4fc02ceee3260fe3a00 Mon Sep 17 00:00:00 2001 From: Jacob Wasserman Date: Sun, 20 Oct 2013 20:37:39 -0400 Subject: [PATCH] Catch geocoding erros and fill with empty rows Catch GeocoderResultError and ValueError. Fill the resulting GeoDataFrame with an empty row. --- geopandas/geocode.py | 16 +++++++++++++--- tests/test_geocode.py | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/geopandas/geocode.py b/geopandas/geocode.py index 335311b..3853ea1 100644 --- a/geopandas/geocode.py +++ b/geopandas/geocode.py @@ -2,6 +2,7 @@ from collections import defaultdict import fiona import geopy +from geopy.geocoders.base import GeocoderResultError import numpy as np import pandas as pd from shapely.geometry import Point @@ -51,8 +52,10 @@ def geocode(strings, provider='googlev3', **kwargs): coder = coders[provider](**kwargs) results = {} for i, s in strings.iteritems(): - # Probably want some try/catch here, but what to do on exception? - results[i] = coder.geocode(s) + try: + results[i] = coder.geocode(s) + except (GeocoderResultError, ValueError): + results[i] = (None, None) df = _prepare_geocode_result(results) return df @@ -73,7 +76,14 @@ def _prepare_geocode_result(results): address, loc = s # loc is lat, lon and we want lon, lat - p = Point(loc[1], loc[0]) + if loc is None: + p = Point() + else: + p = Point(loc[1], loc[0]) + + if address is None: + address = pd.np.nan + d['geometry'].append(p) d['address'].append(address) index.append(i) diff --git a/tests/test_geocode.py b/tests/test_geocode.py index e0df4d8..de5e777 100644 --- a/tests/test_geocode.py +++ b/tests/test_geocode.py @@ -1,6 +1,7 @@ import unittest import fiona +import pandas as pd from shapely.geometry import Point import geopandas as gpd @@ -32,5 +33,20 @@ class TestGeocode(unittest.TestCase): self.assertAlmostEqual(coords[0], test[1]) self.assertAlmostEqual(coords[1], test[0]) + def test_prepare_result_none(self): + p0 = Point(12.3, -45.6) # Treat these as lat/lon + d = {'a': ('address0', p0.coords[0]), + 'b': (None, None)} + + df = _prepare_geocode_result(d) + assert type(df) is gpd.GeoDataFrame + self.assertEqual(fiona.crs.from_epsg(4326), df.crs) + self.assertEqual(len(df), 2) + self.assert_('address' in df) + + row = df.loc['b'] + self.assertEqual(len(row['geometry'].coords), 0) + self.assert_(pd.np.isnan(row['address'])) + def test_bad_provider(self): self.assertRaises(ValueError, geocode, ['cambridge, ma'], 'badprovider')