public
Fork of simonw/geocoders
Description: Ultra simple API for geocoding a single string against various web services.
Homepage:
Clone URL: git://github.com/jacobian/geocoders.git
geocoders / geonames.py
100644 23 lines (19 sloc) 0.625 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from utils import simplejson
import urllib
 
# http://www.geonames.org/export/geonames-search.html
 
def geocode(q):
    data = simplejson.load(urllib.urlopen(
        'http://ws.geonames.org/searchJSON?' + urllib.urlencode({
            'q': q,
            'maxRows': 1,
            'lang': 'en',
            'style': 'full'
        })
    ))
    if not data['geonames']:
        return None, (None, None)
    
    place = data['geonames'][0]
    name = place['name']
    if place['adminName1'] and place['name'] != place['adminName1']:
        name += ', ' + place['adminName1']
    return name, (place['lat'], place['lng'])