From 1dbf71064e41d1b919fec1f2df5ec4c2c4a51ca0 Mon Sep 17 00:00:00 2001 From: xbgmsharp Date: Fri, 9 Dec 2022 12:33:00 +0100 Subject: [PATCH] Update reverse_geocode_py_fn to return always data if name is null then fallback to address field road,neighbourhood,suburb --- initdb/02_3_3_signalk_public_functions_py.sql | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/initdb/02_3_3_signalk_public_functions_py.sql b/initdb/02_3_3_signalk_public_functions_py.sql index 8c861c9..7bdf16d 100644 --- a/initdb/02_3_3_signalk_public_functions_py.sql +++ b/initdb/02_3_3_signalk_public_functions_py.sql @@ -40,12 +40,23 @@ AS $reverse_geocode_py$ return None # Make the request to the geocoder API + # https://operations.osmfoundation.org/policies/nominatim/ payload = {"lon": lon, "lat": lat, "format": "jsonv2", "zoom": 18} r = requests.get(url, params=payload) # Return the full address or nothing if not found + # Option1: If name is null fallback to address field road,neighbourhood,suburb + # Option2: Return the json for future reference like country if r.status_code == 200 and "name" in r.json(): - return r.json()["name"] + r_dict = r.json() + if r_dict["name"]: + return r_dict["name"] + elif "address" in r_dict and r_dict["address"] and r_dict["address"]["road"]: + return r_dict["address"]["road"] + elif "address" in r_dict and r_dict["address"] and r_dict["address"]["neighbourhood"]: + return r_dict["address"]["neighbourhood"] + elif "address" in r_dict and r_dict["address"] and r_dict["address"]["suburb"]: + return r_dict["address"]["suburb"] else: plpy.error('Failed to received a geo full address %s', r.json()) return 'unknow'