-
Notifications
You must be signed in to change notification settings - Fork 9
/
halte-db.py
256 lines (241 loc) · 15.3 KB
/
halte-db.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
import uwsgi
import psycopg2
from psycopg2.pool import PersistentConnectionPool
import simplejson
from sphinxapi import *
from urlparse import urlparse,urlsplit,parse_qs
import re
COMMON_HEADERS = [('Content-Type', 'application/json'), ('Access-Control-Allow-Origin', '*'), ('Access-Control-Allow-Headers', 'Requested-With,Content-Type')]
pool = PersistentConnectionPool(1,20,"dbname='haltes'")
#update timingpoint set latitude = CAST(ST_Y(the_geom) AS NUMERIC(9,7)), longitude = CAST(ST_X(the_geom) AS NUMERIC(8,7)) FROM (select ST_Transform(st_setsrid(st_makepoint(locationx_ew, locationy_ns), 28992), 4326) AS the_geom from timingpoint as t2 where t2.timingpointcode = timingpointcode) AS W;
def notfound(start_response):
start_response('404 File Not Found', COMMON_HEADERS + [('Content-length', '2')])
yield '[]'
def searchStops(query):
reply = {'Columns' : ['TimingPointTown','TimingPointName', 'Name', 'Latitude', 'Longitude'] , 'Rows' : []}
if query == 'search_suggest_query' or query == None or query == '' or len(query) < 2:
return reply
cl = SphinxClient()
cl.SetServer('localhost', 9312)
cl.SetWeights ( [100, 1] )
cl.SetMatchMode ( SPH_MATCH_EXTENDED )
cl.SetIndexWeights({'haltes' : 10,'haltesstemmed' : 1,'haltes_metaphone' : 5})
cl.SetFieldWeights({'name' : 20, 'timingpointtown' : 10, 'timingpointname' : 1, 'namespaceless' : 4, 'timingpointtownspaceless' : 2, 'timingpointnamespaceless' : 1,'stopareacode' :1})
cl.Open()
try:
res = cl.Query ( query, 'haltes haltesstemmed haltes_metaphone' )
if res.has_key('matches'):
for match in res['matches']:
row = {}
for attr in res['attrs']:
attrname = attr[0]
value = match['attrs'][attrname]
row[attrname] = value
reply['Rows'].append([row['timingpointtown'],row['timingpointname'],row['name'],row['latitude'],row['longitude']])
except Exception as e:
print e
pass
cl.Close()
return reply
def search(conn,environ, start_response):
params = parse_qs(environ.get('QUERY_STRING',''))
reply = {'Columns' : ['TimingPointTown','TimingPointName', 'Name', 'Latitude','Longitude'], 'Rows' : []}
params = parse_qs(environ.get('QUERY_STRING',''))
query = params['query'][0]
if query == 'search_suggest_query' or query == None or query == '' or len(query) < 2:
return reply
cl = SphinxClient()
cl.SetServer('localhost', 9312)
cl.SetWeights ( [100, 1] )
if 'sgh' in params:
cl.SetFilter('sgh',[1])
cl.SetMatchMode ( SPH_MATCH_EXTENDED )
cl.SetIndexWeights({'haltes' : 10,'haltesstemmed' : 1,'haltes_metaphone' : 5})
cl.SetFieldWeights({'name' : 20, 'timingpointtown' : 10, 'timingpointname' : 1, 'namespaceless' : 4, 'timingpointtownspaceless' : 2, 'timingpointnamespaceless' : 1,'stopareacode' :1})
cl.Open()
try:
res = cl.Query ( query, 'haltes haltesstemmed haltes_metaphone' )
if res.has_key('matches'):
for match in res['matches']:
row = {}
for attr in res['attrs']:
attrname = attr[0]
value = match['attrs'][attrname]
row[attrname] = value
reply['Rows'].append([row['timingpointtown'],row['timingpointname'],row['name'],row['latitude'],row['longitude']])
except Exception as e:
print e
pass
cl.Close()
return reply
townreply = {'Columns' : ['TimingPointTown'], 'Rows' : []}
conn = pool.getconn()
cur = conn.cursor()
cur.execute("SELECT distinct timingpointtown FROM timingpoint ORDER BY timingpointtown", [])
rows = cur.fetchall()
for row in rows:
townreply['Rows'].append([row[0]])
cur.close()
pool.putconn(conn)
def queryTowns(conn,environ, start_response):
params = parse_qs(environ.get('QUERY_STRING',''))
if 'sgh' in params:
reply = {'Columns' : ['TimingPointTown'], 'Rows' : []}
cur = conn.cursor()
cur.execute("SELECT distinct timingpointtown FROM timingpoint where sgh = true ORDER BY timingpointtown", [])
rows = cur.fetchall()
for row in rows:
reply['Rows'].append([row[0]])
cur.close()
return reply
else:
return townreply
def queryStops(conn,environ, start_response):
params = parse_qs(environ.get('QUERY_STRING',''))
reply = {'Columns' : ['TimingPointTown', 'Name', 'Latitude', 'Longitude' , 'StopAreaCode'] , 'Rows' : []}
cur = conn.cursor()
if 'town' in params:
cur.execute("SELECT distinct on (timingpointtown,name) timingpointtown,name,latitude,longitude,stopareacode FROM timingpoint WHERE timingpointtown = %s ORDER BY name", [params['town'][0]])
elif 'tpc' in params:
cur.execute("SELECT distinct on (timingpointtown,name) timingpointtown,name,latitude,longitude,stopareacode FROM timingpoint WHERE timingpointcode = %s ORDER BY name", [params['tpc'][0]])
elif 'bottomright' in params and 'topleft' in params and 'sgh' in params:
minLatitude, maxLongitude = params['bottomright'][0].split(',')
maxLatitude, minLongitude = params['topleft'][0].split(',')
cur.execute("SELECT distinct on (timingpointtown,name) timingpointtown,name,avg(latitude),avg(longitude),stopareacode FROM timingpoint WHERE latitude between %s AND %s AND longitude between %s and %s and sgh = true group by timingpointtown,name,stopareacode", [minLatitude,maxLatitude,minLongitude,maxLongitude])
elif 'bottomright' in params and 'topleft' in params:
minLatitude, maxLongitude = params['bottomright'][0].split(',')
maxLatitude, minLongitude = params['topleft'][0].split(',')
cur.execute("SELECT distinct on (timingpointtown,name) timingpointtown,name,avg(latitude),avg(longitude),stopareacode FROM timingpoint WHERE latitude between %s AND %s AND longitude between %s and %s group by timingpointtown,name,stopareacode", [minLatitude,maxLatitude,minLongitude,maxLongitude])
elif 'near' in params and 'sgh' in params:
latitude, longitude = params['near'][0].split(',')
limit = '100'
if 'limit' in params:
limit = params['limit'][0]
cur = conn.cursor()
geomconstant = 'SRID=4326;POINT('+longitude+' '+latitude+')'
cur.execute("SELECT timingpointtown,name,latitude,longitude,stopareacode FROM (SELECT distinct on (timingpointtown,name) timingpointtown,name,latitude,longitude,stopareacode,distance FROM (select timingpointtown,name,latitude,longitude,stopareacode, ST_Distance(the_geom, st_setsrid(st_makepoint(%s, %s),4326)) as distance from timingpoint where sgh = true ORDER by the_geom <-> %s::geometry LIMIT %s) as x) as y order by distance;", [longitude,latitude,geomconstant,limit])
elif 'near' in params:
latitude, longitude = params['near'][0].split(',')
limit = '100'
if 'limit' in params:
limit = params['limit'][0]
cur = conn.cursor()
geomconstant = 'SRID=4326;POINT('+longitude+' '+latitude+')'
cur.execute("SELECT timingpointtown,name,latitude,longitude,stopareacode FROM (SELECT distinct on (timingpointtown,name) timingpointtown,name,latitude,longitude,stopareacode,distance FROM (select timingpointtown,name,latitude,longitude,stopareacode, ST_Distance(the_geom, st_setsrid(st_makepoint(%s, %s),4326)) as distance from timingpoint ORDER by the_geom <-> %s::geometry LIMIT %s) as x) as y order by distance;", [longitude,latitude,geomconstant,limit])
elif 'search' in params:
return searchStops(params['search'][0])
else:
return '404'
rows = cur.fetchall()
for row in rows:
if len(reply['Columns']) == 5:
reply['Rows'].append([row[0],row[1],row[2],row[3],row[4]])
if len(reply['Columns']) == 6:
reply['Rows'].append([row[0],row[1],row[2],row[3],row[4],row[5]])
cur.close()
return reply
def queryStopAreas(conn,environ, start_response):
params = parse_qs(environ.get('QUERY_STRING',''))
reply = {'Columns' : ['TimingPointTown','TimingPointName','Name', 'Latitude', 'Longitude', 'StopAreaCode'] , 'Rows' : []}
cur = conn.cursor()
if 'near' in params:
latitude, longitude = params['near'][0].split(',')
limit = '100'
if 'limit' in params:
limit = params['limit'][0]
limit5 = int(limit)*5
cur = conn.cursor()
geomconstant = 'SRID=4326;POINT('+longitude+' '+latitude+')'
cur.execute("SELECT timingpointtown,timingpointname,name,latitude,longitude,stopareacode FROM (select distinct on (stopareacode) * from (select *, ST_Distance(the_geom, st_setsrid(st_makepoint(%s, %s),4326)) as distance from timingpoint ORDER by the_geom <-> %s::geometry LIMIT %s) as x) as y order by distance LIMIT %s;", [latitude,longitude,geomconstant,limit5,limit])
else:
return '404'
rows = cur.fetchall()
for row in rows:
reply['Rows'].append([row[0],row[1],row[2],row[3],row[4],row[5]])
cur.close()
return reply
def queryAccessibility(conn,environ, start_response):
params = parse_qs(environ.get('QUERY_STRING',''))
reply = {'Columns' : ['TimingPointTown', 'TimingPointName', 'Name', 'TimingPointCode', 'kv78turbo','TimingPointWheelChairAccessible', 'TimingPointVisualAccessible', 'Steps','Latitude', 'Longitude'] , 'Rows' : []}
cur = conn.cursor()
if 'tpc' in params:
cur.execute("SELECT timingpointtown, timingpointname,name,t.timingpointcode,kv78turbo,motorisch,visueel,trap,t.latitude,t.longitude FROM timingpoint as t left join haltescan as h on (t.timingpointcode = h.timingpointcode) WHERE t.timingpointcode = %s", [params['tpc'][0]])
else:
return '404'
rows = cur.fetchall()
for row in rows:
reply['Rows'].append([row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9]])
cur.close()
return reply
def queryTimingPoints(conn,environ, start_response):
params = parse_qs(environ.get('QUERY_STRING',''))
reply = {'Columns' : ['TimingPointTown', 'TimingPointName', 'Name', 'TimingPointCode', 'StopAreaCode', 'kv55', 'kv78turbo', 'arriva55'] , 'Rows' : []}
cur = conn.cursor()
if 'town' in params and 'timingpointname' in params:
cur.execute("SELECT timingpointtown,timingpointname,name,timingpointcode,stopareacode, kv55, kv78turbo, arriva55 FROM timingpoint WHERE timingpointtown = %s AND timingpoointname = %s", [params['town'][0], params['timingpointname'][0]])
elif 'town' in params and 'name' in params:
cur.execute("SELECT timingpointtown,timingpointname,name,timingpointcode,stopareacode, kv55, kv78turbo, arriva55 FROM timingpoint WHERE timingpointtown = %s AND name = %s", [params['town'][0], params['name'][0]])
elif 'town' in params:
cur.execute("SELECT timingpointtown,timingpointname,name,timingpointcode,stopareacode, kv55, kv78turbo, arriva55 FROM timingpoint WHERE timingpointtown = %s", [params['town'][0]])
elif 'tpc' in params:
cur.execute("SELECT timingpointtown,timingpointname,name,timingpointcode,stopareacode, kv55, kv78turbo, arriva55 FROM timingpoint AS t1 WHERE EXISTS (select 1 FROM timingpoint AS t2 WHERE timingpointcode = %s and t1.name = t2.name AND t1.timingpointtown = t2.timingpointtown)", [params['tpc'][0]])
elif 'near' in params:
latitude, longitude = params['near'][0].split(',')
limit = '150'
if 'limit' in params:
limit = params['limit'][0]
geomconstant = 'SRID=4326;POINT('+longitude+' '+latitude+')'
cur = conn.cursor()
if 'destinations' in params and 'accessibility' in params:
reply['Columns'].extend(['DestinationName50','LinePlanningNumber','LinePublicNumber','TimingPointWheelChairAccessible','TimingPointVisualAccessible','Latitude','Longitude'])
cur.execute("SELECT timingpointtown, timingpointname, name,x.timingpointcode,stopareacode,kv55,kv78turbo,arriva55,destinationname50,lineplanningnumber,linepublicnumber,motorisch,visueel,x.latitude,x.longitude from (SELECT latitude,longitude, timingpointtown,timingpointname,name,t.timingpointcode,stopareacode, kv55, kv78turbo, arriva55,destinationname50,lineplanningnumber,linepublicnumber, the_geom FROM (select * from timingpoint ORDER by the_geom <-> %s::geometry LIMIT %s) as t,destinationuserstop as d where d.timingpointcode = t.timingpointcode and destinationcode is not null) as x left join haltescan as h on (h.timingpointcode = x.timingpointcode) ORDER by the_geom <-> %s::geometry LIMIT %s;", [geomconstant,limit,geomconstant,limit])
elif 'destinations' in params:
reply['Columns'].extend(['DestinationName50','LinePlanningNumber','LinePublicNumber','Latitude','Longitude'])
cur.execute("SELECT timingpointtown,timingpointname,name,t.timingpointcode,stopareacode, kv55, kv78turbo, arriva55,destinationname50,lineplanningnumber,linepublicnumber,latitude,longitude FROM (select * from timingpoint ORDER by the_geom <-> %s::geometry LIMIT %s) as t,destinationuserstop as d where d.timingpointcode = t.timingpointcode and destinationcode is not null ORDER by the_geom <-> %s::geometry LIMIT %s;", [geomconstant,limit,geomconstant,limit])
else:
cur.execute("SELECT timingpointtown,timingpointname,name,timingpointcode,stopareacode, kv55, kv78turbo, arriva55 FROM timingpoint ORDER by the_geom <-> %s::geometry LIMIT %s;", [geomconstant,limit])
else:
return '404'
rows = cur.fetchall()
for row in rows:
if len(reply['Columns']) == 8:
reply['Rows'].append([row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7]])
elif len(reply['Columns']) == 13:
reply['Rows'].append([row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10],row[11],row[12]])
elif len(reply['Columns']) == 15:
reply['Rows'].append([row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10],row[11],row[12],row[13],row[14]])
cur.close()
return reply
def HalteDB(environ, start_response):
url = environ['PATH_INFO'][1:]
if len(url) > 0 and url[-1] == '/':
url = url[:-1]
arguments = url.split('/')
reply = None
try:
conn = pool.getconn()
if arguments[0] == 'towns':
reply = queryTowns(conn,environ, start_response)
elif arguments[0] == 'stops':
reply = queryStops(conn,environ, start_response)
elif arguments[0] == 'stopareas':
reply = queryStopAreas(conn,environ, start_response)
elif arguments[0] == 'timingpoints':
reply = queryTimingPoints(conn,environ, start_response)
elif arguments[0] == 'accessibility':
reply = queryAccessibility(conn,environ, start_response)
elif arguments[0] == 'search':
reply = search(conn,environ, start_response)
else:
pool.putconn(conn)
return notfound(start_response)
pool.putconn(conn)
except Exception as e:
print e
pool.putconn(conn,close=True)
if reply == '404':
return notfound(start_response)
reply = simplejson.dumps(reply)
start_response('200 OK', COMMON_HEADERS + [('Content-length', str(len(reply)))])
return reply
uwsgi.applications = {'': HalteDB}