forked from simonw/geocoders
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeonames.py
More file actions
25 lines (21 loc) · 777 Bytes
/
geonames.py
File metadata and controls
25 lines (21 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from utils import simplejson, RichResult
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 RichResult((None, (None, None)), data=None)
place = data['geonames'][0]
name = place['name']
if place['adminName1'] and place['name'] != place['adminName1']:
name += ', ' + place['adminName1']
return RichResult((name, (place['lat'], place['lng'])), data=data)
# No API key required, but let's fulfil the contract anyway
geocoder = lambda x: geocode