2

The duty of my function is to calculate the distance from my business to any address (at the end (print fucntion) I put an example of an address)). I don´t know why it is not working, it says something about traceback error.

The first part of my function, converts from address to coordinates. The second part calculate the distance.

THIS IS THE CODE

import math
from geopy.geocoders import Nominatim

def Distancia(direccion_domicilio):
    geolocator = Nominatim(user_agent="specify_your_app_name_here")
    location = geolocator.geocode(direccion_domicilio)
    latylon=(location.latitude, location.longitude)


    rad=math.pi/180
    dif_lat = 20.6072848-(latylon[0])
    dif_long = -103.4160099-(latylon[1])
    radio=6372.795477598
    a=(math.sin(rad*dif_lat/2))**2 + math.cos(rad*location.latitud)*math.cos(rad*20.6072848)*(math.sin(rad*dif_long/2)**2)
    distancia=2*radio*math.asin(math.sqrt(a))
    return distancia

print(Distancia("Avenida Guadalupe, Real Guadalupe, Jardines de Chapalita, Zapopan, Jalisco, 45030, México"))

THIS IS THE ERROR MESSAGE

Traceback (most recent call last):
  File "C:\Python37\lib\urllib\request.py", line 1317, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "C:\Python37\lib\http\client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Python37\lib\http\client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Python37\lib\http\client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Python37\lib\http\client.py", line 1016, in _send_output
    self.send(msg)
  File "C:\Python37\lib\http\client.py", line 956, in send
    self.connect()
  File "C:\Python37\lib\http\client.py", line 1392, in connect
    server_hostname=server_hostname)
  File "C:\Python37\lib\ssl.py", line 412, in wrap_socket
    session=session
  File "C:\Python37\lib\ssl.py", line 850, in _create
    self.do_handshake()
  File "C:\Python37\lib\ssl.py", line 1108, in do_handshake
    self._sslobj.do_handshake()
socket.timeout: _ssl.c:1029: The handshake operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python37\lib\site-packages\geopy\geocoders\base.py", line 344, in _call_geocoder
    page = requester(req, timeout=timeout, **kwargs)
  File "C:\Python37\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
  File "C:\Python37\lib\urllib\request.py", line 543, in _open
    '_open', req)
  File "C:\Python37\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Python37\lib\urllib\request.py", line 1360, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "C:\Python37\lib\urllib\request.py", line 1319, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error _ssl.c:1029: The handshake operation timed out>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Dell\Documents\Python programas\Ejercicios extras\PROYECTOdistancia.py", line 18, in <module>
    print(Distancia("Avenida Guadalupe, Real Guadalupe, Jardines de Chapalita, Zapopan, Jalisco, 45030, México"))
  File "C:\Users\Dell\Documents\Python programas\Ejercicios extras\PROYECTOdistancia.py", line 6, in Distancia
    location = geolocator.geocode(direccion_domicilio)
  File "C:\Python37\lib\site-packages\geopy\geocoders\osm.py", line 307, in geocode
    self._call_geocoder(url, timeout=timeout), exactly_one
  File "C:\Python37\lib\site-packages\geopy\geocoders\base.py", line 367, in _call_geocoder
    raise GeocoderTimedOut('Service timed out')
geopy.exc.GeocoderTimedOut: Service timed out

1 Answers1

1

You did not perform a whole installation process. Traceback tells you what is happening. It says that there is a problem with SSL certificate. geopy library retrieves data from web (Nomitatiom in your case). Take a look at this documentation: https://geopy.readthedocs.io/en/stable/#geopy.geocoders.options and option called default_ssl_context. You should use this code at the beginning of yours code (from the above documentation):

  • to use SSL use:
    import ssl
    import certifi
    import geopy.geocoders
    ctx = ssl.create_default_context(cafile=certifi.where())
    geopy.geocoders.options.default_ssl_context = ctx
  • to disable SSL (not recommended, may not even work for some geo providers):
    import ssl
    import geopy.geocoders
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    geopy.geocoders.options.default_ssl_context = ctx
maQ
  • 496
  • 3
  • 8