-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeocode.py
42 lines (31 loc) · 1018 Bytes
/
geocode.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
'''Utilities for geocoding city names using PlaceFinder.
Example browser request:
http://where.yahooapis.com/geocode?q=Portland&appid=faypaS6k&flags=J
Python Yahoo! REST examples:
http://developer.yahoo.com/python/python-rest.html
geopy:
http://groups.google.com/group/geopy/browse_thread/thread/d06d5a2aefb0e63b
'''
import json
import urllib
import httplib2
def get_lat_long(location, appid = 'faypaS6k'):
http = httplib2.Http()
url = 'http://where.yahooapis.com/geocode?'
params = urllib.urlencode({
'appid': appid,
'q': location,
'flags': 'JC'
})
response, content = http.request(url + params, 'GET')
print location, response, content
content_json = json.loads(content)
resultset = content_json["ResultSet"]
assert resultset["Error"] == 0
assert resultset["Found"] == 1
result = resultset["Results"][0]
return {
"Latitude": result["latitude"],
"Longitude": result["longitude"]
}