Dear friends,
I create a script to extract coordinate from the exif of a number of images and create an excel file with thumbnail, latitude, longitude and other columns.
I was wondering if there was an (easy) way to find the closest placename from geonames given lat and long.
Thanks,
Gianluca

Dani AI

Generated

Short practical summary for : the cleanest approach is to call a reverse/geocoding webservice for each extracted lat/lon (GeoNames is lightweight and straightforward), parse the JSON result for the nearest placename, and only hit the remote API when you don’t already have a cached value. ’s pointers to available services are useful; below are concrete, small Python examples and a note on extracting the <isIn> value from a Gisgraphy XML response.

Use GeoNames’ "find nearby place" JSON service and request a single row (closest match). Example (requests + JSON):

import requests

def nearest_place(lat, lng, username, timeout=5):
    url = "http://api.geonames.org/findNearbyPlaceNameJSON"
    params = {'lat': lat, 'lng': lng, 'username': username, 'maxRows': 1}
    r = requests.get(url, params=params, timeout=timeout)
    r.raise_for_status()
    data = r.json()
    if data.get('geonames'):
        return data['geonames'][0]   # contains 'name', 'countryName', 'distance', etc.
    return None

GeoNames requires a registered username on each request and enforces daily/hourly credit limits, so register an account and avoid using the public "demo" user in production. (geonames.org)

If you’re using Gisgraphy and get XML output with an <isIn> element, parse it with ElementTree (or request JSON to simplify parsing). Example XML extractor:

import xml.etree.ElementTree as ET

def extract_isIn(xml_content):
    root = ET.fromstring(xml_content)
    node = root.find('.//isIn')
    return node.text if node is not None else None

Gisgraphy documents that isIn holds the city/municipality info and that the service can return JSON (use format=json for easier parsing). (gisgraphy.com)

Quick tips: cache results for identical coordinates; use maxRows=1, localCountry and cities filters on GeoNames to tune results; honor rate limits and back off on errors; prefer JSON where possible to avoid brittle XML parsing. (geonames.org)

Recommended Answers

All 5 Replies

Yes ;)

Oh you want to know what it is, why you did nolt ask that in first place :)

I did not prove this, but by Googling I got this: http://planbperformance.net/dan/blog/?p=135 I am unable to read that code in my mobile as it does not show indention showing the code.

For module supporting geonames, there seems to be (again un tested) https://github.com/gregrobbins/geonames-python

Basically you could also use for example Google Maps API to accomplish same kind of thing.

I found a nice tutorial in the meantime

The first example seems to work fine to me but if there is any other (better) way I can do it... please let me know.
Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.