-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpelias.py
237 lines (191 loc) · 6.64 KB
/
pelias.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
"""All functionalities to call Pelias API
Raises:
PeliasException: raised when some unexcepted event occurs when calling Pelias
"""
import urllib
import time
import json
from utils import (log, vlog)
# Pelias functions/classes
class PeliasException(Exception):
"""
Exceptions related to Pelias
"""
class Pelias:
"""
Class calling Pelias REST API
"""
def __init__(
self,
domain,
scheme="http",
):
self.geocode_path = '/v1/search'
self.geocode_struct_path = '/v1/search/structured'
self.interpolate_path = '/search/geojson'
self.verbose = False
self.scheme = scheme
self.domain = domain.strip('/')
self.geocode_api = (
f'{self.scheme}://{self.domain}{self.geocode_path}'
)
self.geocode_struct_api = (
f'{self.scheme}://{self.domain}{self.geocode_struct_path}'
)
self.interpolate_api = (
f'{self.scheme}://{self.domain.replace("4000", "4300")}{self.interpolate_path}'
)
self.elastic_api = (
f'{self.scheme}://{self.domain.replace("4000", "9200")}'
)
def call_service(self, url, nb_attempts=6):
"""
Call URL. If something went wrong, wait a short delay, and try again,
up to nb_attempts times
Parameters
----------
url : TYPE
DESCRIPTION.
nb_attempts : TYPE, optional
DESCRIPTION. The default is 6.
Raises
------
PeliasException
If a valid answer is not received after nb_attempts .
Returns
-------
dict
Pelias result.
"""
delay = 1
while nb_attempts > 0:
try:
with urllib.request.urlopen(url) as response:
res = response.read()
res = json.loads(res)
return res
except urllib.error.HTTPError as exc:
if exc.code == 400 and self.interpolate_api in url: # bad request, typically bad house number format
log(f"Error 400 ({url}): {exc}")
return {}
if nb_attempts == 1:
log(f"Cannot get Pelias results after several attempts({url}): {exc}")
raise PeliasException(f"Cannot get Pelias results after several attempts ({url}): {exc}") from exc
nb_attempts -= 1
log(f"Cannot get Pelias results ({url}): {exc}. Try again in {delay} seconds...")
time.sleep(delay)
delay += 0.5
except ConnectionRefusedError as exc:
raise PeliasException(f"Cannot connect to Pelias, service probably down ({url}): {exc}") from exc
except urllib.error.URLError as exc:
raise PeliasException(f"Cannot connect to Pelias, service probably down ({url}): {exc}") from exc
except Exception as exc:
log(f"Cannot get Pelias results ({url}): {exc}")
raise exc
def geocode(self, query, layers=None):
"""
Call Pelias geocoder
Parameters
----------
query : dict or str
if dict, should contain "address", "locality" and "postalcode" fields
if str, should contain an address
Raises
------
PeliasException
If anything went wrong while calling Pelias.
Returns
-------
res : str
Pelias result.
"""
if isinstance(query, dict):
struct = True
params = {
'address': query['address'],
'locality': query['locality']
}
if 'postalcode' in query:
params["postalcode"] = query['postalcode']
else:
struct = False
params = {'text': query}
if layers:
params["layers"] = layers
url = self.geocode_struct_api if struct else self.geocode_api
params = urllib.parse.urlencode(params)
url = f"{url}?{params}"
vlog(f"Call to Pelias: {url}")
return self.call_service(url)
def interpolate(self, lat, lon, number, street):
"""
Call Pelias interpolate service
Parameters
----------
lat: float
Approximate latitude
lon: float
Approximate longiture
number: str
House number to interpolate
street: str
Street name where the number should be interpolate
Raises
------
PeliasException
If anything went wrong while calling Pelias.
Returns
-------
res : str
Pelias result.
"""
url = self.interpolate_api
params = urllib.parse.urlencode({"lat": lat, "lon": lon, "number": number, "street": street})
url = f"{url}?{params}"
vlog(f"Call to interpolate: {url}")
return self.call_service(url)
def check(self, city_test_from="Bruxelles"):
"""
Check that Pelias server is up&running
Returns
-------
Object
True: Everything is fine
False: Server does not answer
list of dict: answer from Nominatim if it does not contain the expected values
"""
try:
pelias_res = self.geocode(city_test_from)
if city_test_from.lower() == pelias_res["geocoding"]["query"]["text"].lower():
return True # Everything is fine
return pelias_res # Server answers, but gives an unexpected result
except PeliasException as exc:
vlog("Exception occured: ")
vlog(exc)
return False # Server does not answer
def wait(self, city_test_from="Bruxelles"):
"""
Wait for Pelias to be up & running. Give up after 10 attempts, with a delay
starting at 2 seconds, being increased by 0.5 second each round.
Returns
-------
None.
"""
delay = 2
for i in range(10):
pel = self.check(city_test_from)
if pel is True:
log("Pelias working properly")
break
log("Pelias not up & running")
log(f"Try again in {delay} seconds")
if pel is not False:
log("Answer:")
log(pel)
log(f"Pelias host: {self.geocode_api }")
# raise e
time.sleep(delay)
delay += 0.5
if i == 9:
log("Pelias not up & running !")
log(f"Pelias: {self.geocode_api }")