TkinterMapView icon indicating copy to clipboard operation
TkinterMapView copied to clipboard

get_address returns 403 error

Open mke21 opened this issue 11 months ago • 2 comments

Running `map_widget.set_address("New York") does not work, returning the error:

Status code 403 from https://nominatim.openstreetmap.org/search: ERROR - 403 Client Error: Forbidden for url: https://nominatim.openstreetmap.org/search?q=New+York&format=jsonv2&addressdetails=1&limit=1

mke21 avatar Dec 23 '24 12:12 mke21

Hi, for me I just used a different geocoder to Nominatim. I used OpenCage with code like this:

def get_coordinates_opencage(address):
    url = f"https://api.opencagedata.com/geocode/v1/json?q={
        address}&key={OPENCAGE_KEY}"
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        if data["results"]:
            lat = data["results"][0]["geometry"]["lat"]
            lon = data["results"][0]["geometry"]["lng"]
            return lat, lon
        else:
            raise ValueError("No results found for the given address.")
    else:
        response.raise_for_status()


def getAddress():
    inputted_place = search_entry.get()
    if inputted_place:
        try:
            lat, lon = get_coordinates_opencage(inputted_place)
            map_widget.set_position(lat, lon)
            map_widget.set_marker(lat, lon, text=inputted_place)
            log_output.set(
                f"Found: {inputted_place} (Lat: {lat}, Lon: {lon})")
            map_widget.set_zoom(14)
        except Exception as e:
            messagebox.showerror(title="Error", message=f"Error: {
                                 e}", icon="cancel")
    else:
        messagebox.showerror(
            title="Error", message="Please enter a valid address", icon="warning")

I know my example is a bit more specialised to my case but you can figure out what's going on I'm sure. It's just using OpenCage to retrieve lat and long coordinates and then passing those into map_widget.set_position(), completely bypassing set_address(). Just get an API key and should work well. The API response has a load of other great info if you need it, try printing out the JSON.

finnonthemoon avatar Feb 15 '25 19:02 finnonthemoon

I think this is an example of one package doing too much and now being too difficult to maintain. Probably a better approach would be to use dependency injection, so a developer can use his own geocoder function. I am contemplating to create a simplified fork of this package, seeing the current dormant state.

mke21 avatar Apr 10 '25 07:04 mke21

FYI, as part of the query dictionary in geocoder osm.py, you need to add an email: '[email protected]' entry. They no longer allow searches without a more specific User-Agent. Adding an email seems to allow the url through outside of a normal browser.

EODEFX avatar Aug 07 '25 06:08 EODEFX