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 / placemaker.py
100644 28 lines (24 sloc) 0.843 kb
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
26
27
28
from utils import make_nsfind, ET, partial2
import urllib
 
# http://developer.yahoo.com/geo/placemaker/guide/api_docs.html
 
def geocode(q, api_key):
    find = make_nsfind({
        'ns': 'http://wherein.yahooapis.com/v1/schema'
    })
    args = {
        'documentContent': q,
        'documentType': 'text/plain',
        'appid': api_key,
    }
    et = ET.parse(urllib.urlopen(
        'http://wherein.yahooapis.com/v1/document', urllib.urlencode(args))
    )
    place = find(et, 'ns:document/ns:placeDetails/ns:place')
    if place is None:
        return None, (None, None)
    else:
        name = find(place, 'ns:name').text.decode('utf8')
        lat = float(find(place, 'ns:centroid/ns:latitude').text)
        lon = float(find(place, 'ns:centroid/ns:longitude').text)
        return name, (lat, lon)
 
geocoder = partial2(geocode)