Fetching local placenames from Geonames

82 Views Asked by At

I am trying to extract the placename in an official local language from Geonames, since I do not want to always prefer/use the English version.

I am fetching the place, the country code and the alternate names list with the following function call:

def countrycode_from_id(geonameId, username):
    base_url = "http://api.geonames.org/getJSON"
    params = {
        "geonameId": geonameId,  # geonameId of the place
        "username": username,  # your geonames username
    }
    response = requests.get(base_url, params=params, timeout=10)
    data = response.json()
    # Retrieve country code
    country_code = data.get("countryCode")
    altnameslist = data["alternateNames"]
    return country_code, altnameslist

so that a call such as:

country, altnames = countrycode_from_id(1275339, "username")

will populate the country variable with the ISO country code ("IN" for India in this case) and the altnames will be a list of dictionaries such as:

{'name': 'Bombay', 'lang': 'it', 'isHistoric': True}

which will specify that the usual Italian (it) version of this place names is 'Bombay'.

I am grappling with where could I find an association between an ISO country code and the official language(s) spoken there?

Or is there some other Geonames API "trick" to fetch the preferred (as in official language version) placename?

Thanks

0

There are 0 best solutions below