mirror of
https://github.com/wassname/geopandas.git
synced 2026-09-09 11:22:50 +08:00
Catch geocoding erros and fill with empty rows
Catch GeocoderResultError and ValueError. Fill the resulting GeoDataFrame with an empty row.
This commit is contained in:
+13
-3
@@ -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)
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user