When I am using geopy to calculate the distances between 2 addresses based on their longitude and the latitude, it works fine on individual pair of data. But when there is more data, it always gives me this error:
File "/Library/Python/2.7/site-packages/geopy/geocoders/osm.py", line 193, in geocode self._call_geocoder(url, timeout=timeout), exactly_one File "/Library/Python/2.7/site-packages/geopy/geocoders/base.py", line 171, in _call_geocoder raise GeocoderServiceError(message) geopy.exc.GeocoderServiceError: urlopen error [Errno 65] No route to host
Do you know how can I avoid this problem?
My code is simple: (The data input for this has many pairs of data)
from geopy.geocoders import Nominatim
from geopy.distance import vincenty
def calculate_distance(add1, add2):
geolocator = Nominatim()
location1 = geolocator.geocode(add1)
al1 = (location1.latitude, location1.longitude)
location2 = geolocator.geocode(add2)
al2 = (location2.latitude, location2.longitude)
distce = vincenty(al1, al2).miles
return distce
def get_level1_locations(lat_lng):
elems = lat_lng.split(',')
url = "http://maps.googleapis.com/maps/api/geocode/json?"
url += "latlng=%s,%s&sensor=false" % (float(elems[0]), float(elems[1]))
v = urlopen(url).read()
j = json.loads(v)
if len(j['results'])>0:
components = j['results'][0]['address_components']
location_list=[]
for c in components:
if "locality" in c['types']:
location_list.append(c['long_name'])
if "administrative_area_level_1" in c['types']:
location_list.append(c['long_name'])
if "country" in c['types']:
location_list.append(c['long_name'])
location = ', '.join(filter(None, location_list))
return location
return ''