Stefano
Stefano

Reputation: 199

Streamlit: Max Retries Exceeded with get_geocode Function Using OpenStreetMap API

I am developing a Streamlit application where I need to convert addresses into geographical coordinates. I have a function, get_geocode, which uses the OpenStreetMap Nominatim API to achieve this. Here's the code for the function:

import requests

def get_geocode(address):
    url = f"https://nominatim.openstreetmap.org/search?format=json&q={address}"
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        if len(data) > 0:
            lat = data[0]['lat']
            lon = data[0]['lon']
            return lat, lon

When I manually insert a query URL like "https://nominatim.openstreetmap.org/search?format=json&q=via%20Cavour%205%20Roma" into a browser, I get the expected result without any issues. However, when I run this function within Streamlit, I encounter the following error:

HTTPSConnectionPool(host='nominatim.openstreetmap.org', port=443): Max retries exceeded with url: /search?format=json&q=via%20cavour%2064%205%20roma (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fe9d0820a90>: Failed to establish a new connection: [Errno 111] Connection refused'))

This error seems to indicate a connection issue, but I'm not sure why it's occurring only within the Streamlit application and not when the URL is accessed directly through a browser.

Any suggestions on how to resolve this error or why it's occurring specifically in the Streamlit context would be greatly appreciated. Thank you!

Upvotes: 0

Views: 65

Answers (0)

Related Questions