-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeocoder.py
303 lines (257 loc) · 9.48 KB
/
geocoder.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
try:
import json
except ImportError:
import simplejson as json
import urllib, csv, time
#If using the Yahoo or Bing APIs, specify your API key here. No API key needed to use the default OpenStreetMap API.
_Y_api = ''
_bing_api = ''
#The following aren't loaded up until they are needed. We could change
#that if needed.
_state_lookup_table = {}
_code_to_country = {}
_country_to_code = {}
####--------------------------------------------------------------------
# This is the main function that actually does (reverse) geocoding.
def geocode(query='Yellowstone', site='osm', key='', verbose=False, chop=True):
# Must encode all locations in utf-8 before sending through a URL-based API
query = query.encode('utf-8')
url = geturl(query, site, key)
#print url
failures = 0
while failures >= 0:
try:
page = urllib.urlopen(url)
failures = -1
except IOError,e :
secs = 2**failures
print e
print " retrying in %i seconds..." % secs
time.sleep(secs)
failures += 1
s = page.read()
J = json.loads(s)
if site=='osm':
if J:
J = J[0]
else:
J = {}
J.setdefault('name', query)
if chop: #Reprocess the response into the common format
response = chopshop(J, site)
return response
else: #Return the raw JSON response dict
return J
####--------------------------------------------------------------------
# This function takes a query and crafts the url to submit to the
# specified API.
def geturl(query='Yellowstone', site='osm', key=''):
#Check if the input is a lat/lon pair
try:
suffix = query.split(':')[-1]
(lat,lon) = [float(x) for x in suffix.strip('( )').split(',')]
reverse = True
query = "%s,%s" % (lat, lon)
except ValueError:
reverse = False
pass
if site=='google':
site_url = 'http://maps.google.com/maps/geo?%s'
params = {'q': query,
'output': 'json'
}
if reverse:
params['sensor']='false'
# OpenStreetMap Nominatim API
elif site=='osm':
site_url = 'http://nominatim.openstreetmap.org/search?%s'
params = {'q': query,
'format': 'json',
'addressdetails': '1',
'accept-language': 'en'
}
#----------------
elif site=='bing':
if not key:
key = _bing_api
if reverse:
site_url = 'http://dev.virtualearth.net/REST/v1/Locations/' + query + '?%s'
params = {'key' : key}
else:
site_url = 'http://dev.virtualearth.net/REST/v1/Locations?%s'
params = {'query': query,
'key': key
}
#----------------
elif site=='yahoo':
site_url = 'http://where.yahooapis.com/geocode?%s'
if not key:
key = _Y_api
params = {'location': query,
'appid': key,
'flags': 'J'
}
if reverse:
params['gflags'] = 'R'
#----------------
elif site=='yahoo_n':
site_url = 'http://where.yahooapis.com/geocode?%s'
if not key:
key = _Y_api
params = {'name': query,
'appid': key,
'flags': 'J'
}
#----------------
else:
raise NameError('No such site name')
url = site_url % urllib.urlencode(params)
return url
####--------------------------------------------------------------------
# This function takes the JSON output from the different APIs and returns
# a smaller dict with a common structure, suitable for comparison.
def chopshop(J, site, verbose=False):
keys = [u'latitude', u'longitude', u'city', u'state',
u'statecode', u'country', u'countrycode'] #also name
#u'regioncode', u'continentcode']
resp = dict(zip(keys,['']*len(keys)))
if site[:5]=='yahoo':
try:
first = J['ResultSet']['Results'][0]
except (KeyError, IndexError):
if verbose:
print "No results found for %s" % J['name']
return resp
resp[u'name'] = J['name']
for k in keys:
resp[k] = first[k]
elif site == 'osm':
try:
address = J['address']
except (KeyError, IndexError):
if verbose:
print "No results found for %s" % J['name']
return resp
resp[u'name'] = J['display_name']
resp['latitude'] = J['lat']
resp['longitude'] = J['lon']
try:
resp['city'] = address['city']
except KeyError:
pass
resp['statecode'] = ''
try:
resp['state'] = address['state']
except KeyError:
pass
try:
resp['country'] = address['country']
resp['countrycode'] = address['country_code']
except KeyError:
pass
elif site == 'bing':
try:
first = J['resourceSets'][0]['resources'][0]
except (KeyError, IndexError):
if verbose:
print "No results found for %s" % J['name']
return resp
resp[u'name'] = first['name'] #What is the name field, exactly?
(resp['latitude'], resp['longitude']) = first['point']['coordinates']
resp['city'] = first['address'].get('locality','')
resp['statecode'] = first['address'].get('adminDistrict','')
resp['state'] = lookupState(resp['statecode'])
resp['country'] = first['address'].get('countryRegion','')
resp['countrycode'] = lookupCountry(resp['country'])
elif site == 'google':
try:
first = J['Placemark'][0]
except (KeyError, IndexError):
if verbose:
print "No results found for %s" % J['name']
return resp
resp[u'name'] = J['name']
(resp['longitude'], resp['latitude'], z) = first['Point']['coordinates']
try:
resp['city'] = first['AddressDetails']['Country']['AdministrativeArea']['Locality']['LocalityName']
except KeyError:
try:
resp['city'] = first['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['SubAdministrativeAreaName']
except KeyError:
pass
try:
resp['statecode'] = first['AddressDetails']['Country']['AdministrativeArea']['AdministrativeAreaName'].upper()
resp['state'] = lookupState(resp['statecode'])
except KeyError:
pass
try:
resp['country'] = first['AddressDetails']['Country']['CountryName']
resp['countrycode'] = first['AddressDetails']['Country']['CountryNameCode']
except KeyError:
pass
else:
return J
return resp
####--------------------------------------------------------------------
# The remaining functions concern the state/country lookup tables which
# convert to and from two-letter abbreviations.
def lookupState(query):
if not _state_lookup_table:
_load_state_lookup_table()
try:
toreturn = _state_lookup_table[query]
except KeyError:
toreturn = ''
#print "%s not in state lookup table. FIX IT!" % query
return toreturn
def _load_state_lookup_table(fname='geo_tables/states.csv'):
states = []
codes = []
table = {}
DR = csv.DictReader(open(fname), delimiter=':', quotechar='\"') #HAS HEADER
for d in DR:
states.append(d['State'])
codes.append(d['Code'])
for (state, code) in zip(states, codes):
_state_lookup_table[code] = state
_state_lookup_table[state] = code
#Manual addition that isn't in the table:
_state_lookup_table[''] = ''
def lookupCountry(query):
if not _country_to_code:
_load_country_lookup_table()
if len(query) == 2:
table = _code_to_country
tname = 'code-to-country'
else:
table = _country_to_code
tname = 'country-to-code'
try:
toreturn = table[query]
except KeyError:
toreturn = ''
print "%s not in %s table. FIX IT!" % (query, tname)
return toreturn
def _load_country_lookup_table():
code_country_fname='geo_tables/code_to_country.csv'
country_code_fname='geo_tables/country_to_code.csv'
countries = []
codes = []
table = {}
DR = csv.DictReader(open(code_country_fname), delimiter=':', quotechar='\"') #HAS HEADER
for d in DR:
#data = line.split(':')
countries.append(d['Country'])
codes.append(d['Code'])
for (country, code) in zip(countries, codes):
_code_to_country[code] = country
DR = csv.DictReader(open(country_code_fname), delimiter=':', quotechar='\"') #HAS HEADER
for d in DR:
#data = line.split(':')
countries.append(d['Country'])
codes.append(d['Code'])
for (country, code) in zip(countries, codes):
_country_to_code[country] = code
#Manual addition that isn't in the table:
_code_to_country[''] = ''
_country_to_code[''] = ''