4

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
Cherry Wu
  • 3,844
  • 9
  • 43
  • 63
  • Looks like a network error more than there's issue with the API. – user1157751 Feb 06 '16 at 05:18
  • It happens every time and my network works fine.... @user1157751 – Cherry Wu Feb 06 '16 at 18:01
  • 3
    it happens also to me. I'm inclined to believe I was blacklisted due to too many requests. – Hazam Mar 04 '16 at 10:44
  • @CherryWu I got same error :/ How did you solve it? Probably as Hazam said because of too many request it happened. – Mehmet Kagan Kayaalp May 31 '16 at 12:44
  • Hi @waterkinq, I later solved the problems by using Google Geo API. If you simply want to get the visualized location, you can use my IPython Notebook code here https://github.com/hanhanwu/Hanhan_Play_With_Social_Media/blob/master/Geo_Visualization.png And if you want to get detailed location, please check the code I am writing below. It works. But you may need to change the `if` clause because different location has different label. – Cherry Wu Jun 01 '16 at 03:17

2 Answers2

2
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 ''
Cherry Wu
  • 3,844
  • 9
  • 43
  • 63
0

The google map API above worked in 2016, today (2017-08-30) when I tried to use it, it's no longer able to return location through coordinates. But! I found a pretty good one here and it works well now, requires much less lines of code too. https://chrisalbon.com/python/geocoding_and_reverse_geocoding.html

Cherry Wu
  • 3,844
  • 9
  • 43
  • 63