This repository was archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup.py
94 lines (76 loc) · 2.26 KB
/
lookup.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
import re
from flask import current_app
from database import get_db
from geocoder import geocode
def _query_data(lon, lat):
db = get_db()
cursor = db.execute(
'select data from grids where (? between x1 and x2) and (? between y3 and y1)',
(lon, lat)
)
res = cursor.fetchone()
if res:
return res[0]
def _query_adress(street_name, housenr):
db = get_db()
cursor = db.execute(
'select lon, lat from adresses where street_name=? and housenr=?',
(street_name, housenr)
)
return cursor.fetchone()
def _query_street(street_name):
db = get_db()
cursor = db.execute(
'select lon, lat from streets where street_name=?',
(street_name,)
)
return cursor.fetchone()
def query(args):
street_name = args['street'].lower()
housenr = re.sub('[^0-9]', '', args['nr'])
adress = ' '.join((args['street'], args['nr']))
match = None
# try adress lookup in our database
coords = _query_adress(street_name, housenr)
if coords:
data = _query_data(*coords)
if data:
match = 'exact'
return data, match
# try street name lookup
coords = _query_street(street_name)
data = _query_data(*coords)
if data:
match = 'street'
return data, match
else:
# try street name lookup
coords = _query_street(street_name)
if coords:
data = _query_data(*coords)
if data:
match = 'street'
return data, match
# geocode
if current_app.debug:
print('geocoding "%s" ...' % adress)
loc = geocode(adress)
if loc:
data = _query_data(loc.longitude, loc.latitude)
if data:
match = 'geocode'
return data, match
if current_app.debug:
print('can\'t find results for "%s"' % adress)
return None, None
def suggest(value):
value = value.lower().strip()
if value:
db = get_db()
cursor = db.execute(
'select street_name from streets where street_name like ?',
(value + '%',)
)
data = cursor.fetchall()
return [d[0].title() for d in data]
return []