-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopencage.py
More file actions
439 lines (369 loc) · 11.7 KB
/
Copy pathopencage.py
File metadata and controls
439 lines (369 loc) · 11.7 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/python
# coding: utf8
from __future__ import absolute_import
import logging
from geocoder.location import BBox
from geocoder.base import OneResult, MultipleResultsQuery
from geocoder.keys import opencage_key
class OpenCageResult(OneResult):
def __init__(self, json_content):
# create safe shortcuts
self._geometry = json_content.get('geometry', {})
self._components = json_content.get('components', {})
self._annotations = json_content.get('annotations', {})
self._bounds = json_content.get('bounds', {})
# proceed with super.__init__
super(OpenCageResult, self).__init__(json_content)
@property
def lat(self):
return self._geometry.get('lat')
@property
def lng(self):
return self._geometry.get('lng')
@property
def address(self):
return self.raw.get('formatted')
@property
def housenumber(self):
return self._components.get('house_number')
@property
def house_aliases(self):
house = self._components.get('house')
building = self._components.get('building')
public_building = self._components.get('public_building')
if house: # Priority can be rearranged
return house
elif building:
return building
elif public_building:
return public_building
@property
def house(self):
house = self._components.get('house')
if house:
return house
else:
return self.house_aliases
@property
def building(self):
building = self._components.get('building')
if building:
return building
else:
return self.house_aliases
@property
def public_building(self):
public_building = self._components.get('public_building')
if public_building:
return public_building
else:
return self.house_aliases
@property
def street_aliases(self):
street = self._components.get('street')
road = self._components.get('road')
footway = self._components.get('footway')
street_name = self._components.get('street_name')
residential = self._components.get('residential')
path = self._components.get('path')
pedestrian = self._components.get('pedestrian')
if street:
return street
elif road:
return road
elif footway:
return footway
elif street_name:
return street_name
elif residential:
return residential
elif path:
return path
elif pedestrian:
return pedestrian
@property
def street(self):
street = self._components.get('street')
if street:
return street
else:
return self.street_aliases
@property
def footway(self):
footway = self._components.get('footway')
if footway:
return footway
else:
return self.street_aliases
@property
def road(self):
road = self._components.get('road')
if road:
return road
else:
return self.street_aliases
@property
def street_name(self):
street_name = self._components.get('street_name')
if street_name:
return street_name
else:
return self.street_aliases
@property
def residential(self):
residential = self._components.get('residential')
if residential:
return residential
else:
return self.street_aliases
@property
def path(self):
path = self._components.get('path')
if path:
return path
else:
return self.street_aliases
@property
def pedestrian(self):
pedestrian = self._components.get('pedestrian')
if pedestrian:
return pedestrian
else:
return self.street_aliases
@property
def neighbourhood_aliases(self):
neighbourhood = self._components.get('neighbourhood')
suburb = self._components.get('suburb')
city_district = self._components.get('city_district')
if neighbourhood: # Priority can be rearranged
return neighbourhood
elif suburb:
return suburb
elif city_district:
return city_district
@property
def neighbourhood(self):
neighbourhood = self._components.get('neighbourhood')
if neighbourhood:
return neighbourhood
else:
return self.neighbourhood_aliases
@property
def suburb(self):
suburb = self._components.get('suburb')
if suburb:
return suburb
else:
return self.neighbourhood_aliases
@property
def city_district(self):
city_district = self._components.get('city_district')
if city_district:
return city_district
else:
return self.neighbourhood_aliases
@property
def city_aliases(self):
city = self._components.get('city')
town = self._components.get('town')
if city: # Priority can be rearranged
return city
elif town:
return town
else: # if nothing in city_aliases, then return village aliases
return self.village_aliases
@property
def city(self):
city = self._components.get('city')
if city:
return city
else:
return self.city_aliases
@property
def town(self):
town = self._components.get('town')
if town:
return town
else:
return self.city_aliases
@property
def county(self):
return self._components.get('county')
@property
def village_aliases(self):
village = self._components.get('village')
hamlet = self._components.get('hamlet')
locality = self._components.get('locality')
if village: # Priority can be rearranged
return village
elif hamlet:
return hamlet
elif locality:
return locality
@property
def village(self):
village = self._components.get('village')
if village:
return village
else:
return self.village_aliases
@property
def hamlet(self):
hamlet = self._components.get('hamlet')
if hamlet:
return hamlet
else:
return self.village_aliases
@property
def locality(self):
locality = self._components.get('locality')
if locality:
return locality
else:
return self.village_aliases
@property
def state_aliases(self):
state = self._components.get('state')
province = self._components.get('province')
state_code = self._components.get('state_code')
if state: # Priority can be rearranged
return state
elif province:
return province
elif state_code:
return state_code
@property
def state(self):
state = self._components.get('state')
if state:
return state
else:
return self.state_aliases
@property
def province(self):
province = self._components.get('province')
if province:
return province
else:
return self.state_aliases
@property
def state_code(self):
state_code = self._components.get('state_code')
if state_code:
return state_code
else:
return self.state_aliases
@property
def state_district(self):
return self._components.get('state_district')
@property
def country(self):
country = self._components.get('country')
if country:
return country
else:
return self._components.get('country_name')
@property
def country_code(self):
return self._components.get('country_code')
@property
def postal(self):
return self._components.get('postcode')
@property
def postcode(self):
return self._components.get('postcode')
@property
def continent(self):
return self._components.get('continent')
@property
def island(self):
return self._components.get('island')
@property
def region(self):
return self._components.get('region')
@property
def confidence(self):
return self.raw.get('confidence')
@property
def w3w(self):
return self._annotations.get('what3words', {}).get('words')
@property
def mgrs(self):
return self._annotations.get('MGRS')
@property
def geohash(self):
return self._annotations.get('geohash')
@property
def callingcode(self):
return self._annotations.get('callingcode')
@property
def Maidenhead(self):
return self._annotations.get('Maidenhead')
@property
def DMS(self):
return self._annotations.get('DMS')
@property
def Mercator(self):
return self._annotations.get('Mercator')
@property
def bbox(self):
south = self._bounds.get('southwest', {}).get('lat')
north = self._bounds.get('northeast', {}).get('lat')
west = self._bounds.get('southwest', {}).get('lng')
east = self._bounds.get('northeast', {}).get('lng')
if all([south, west, north, east]):
return BBox.factory([south, west, north, east]).as_dict
class OpenCageQuery(MultipleResultsQuery):
"""
OpenCage Geocoding Services
===========================
OpenCage Geocoder simple, easy, and open geocoding for the entire world
Our API combines multiple geocoding systems in the background.
Each is optimized for different parts of the world and types of requests.
We aggregate the best results from open data sources and algorithms so you don't have to.
Each is optimized for different parts of the world and types of requests.
API Reference
-------------
https://geocoder.opencagedata.com/api
"""
provider = 'opencage'
method = 'geocode'
_URL = 'http://api.opencagedata.com/geocode/v1/json'
_RESULT_CLASS = OpenCageResult
_KEY = opencage_key
def _build_params(self, location, provider_key, **kwargs):
base_params = {
'query': location,
'key': provider_key,
'limit': kwargs.get('maxRows', 1)
}
language = kwargs.get('language', None)
if language:
base_params['language'] = language
countrycode = kwargs.get('countrycode', None)
if countrycode:
base_params['countrycode'] = countrycode
return base_params
def _catch_errors(self, json_response):
status = json_response.get('status')
if status and status.get('code') != 200:
self.status_code = status.get('code')
self.error = status.get('message')
return self.error
def _adapt_results(self, json_response):
# special license attribute
self.license = json_response['licenses']
# Shows the limit and how many remaining calls you have on your
# API Key. Optional for paid OpenCage accounts
if json_response.get('rate'):
self.remaining_api_calls = json_response['rate']['remaining']
self.limit_api_calls = json_response['rate']['limit']
else:
self.remaining_api_calls = 999999
self.limit_api_calls = 999999
# return geo results
return json_response['results']
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
g = OpenCageQuery('1552 Payette dr., Ottawa')
g.debug()