-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sensor.py
298 lines (240 loc) · 13.7 KB
/
Sensor.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
import HighwayEdges
import Utils
import psycopg2
class Sensor(object):
def __init__(self, cursor, fileout):
self.cursor = cursor
self.fileout = fileout
def find_first_last_sensor(self, onstreet, start_street, end_street, direction):
#find the first and the last sensor then enlarge the range by 0.5 mile for each end to include all possible sensors
sql = "select postmile from highway_congestion_config where onstreet like '%"+onstreet+"%' and fromstreet like '%"+start_street+"%' and direction = '" + str(direction) + "' order by last_seen_at desc limit 1"
self.cursor.execute(sql)
start_postmile = self.cursor.fetchall()[0][0]
sql = "select postmile from highway_congestion_config where onstreet like '%"+onstreet+"%' and fromstreet like '%"+end_street+"%' and direction = '" + str(direction) + "' order by last_seen_at desc limit 1"
self.cursor.execute(sql)
end_postmile = self.cursor.fetchall()[0][0]
if start_postmile > end_postmile:
start_postmile += 0.5
end_postmile -= 0.5
else:
start_postmile -= 0.5
end_postmile += 0.5
print "For " + onstreet + " Start_postmile is " + str(start_postmile)+ " and end_postmile is " + str(end_postmile)
return start_postmile, end_postmile
def find_all_sensors(self, onstreet, start_postmile, end_postmile, direction):
#find all sensors between these two postmiles
sql = "select distinct sensor_id, ST_AsText(start_lat_long) from highway_congestion_config where last_seen_at >= '2015-01-01' and last_seen_at < '2016-01-01' and onstreet like '%" + onstreet + "%' and postmile >= " + str(min(start_postmile, end_postmile)) + " and postmile <= " + str(max(start_postmile, end_postmile)) + " and direction = '" + str(direction) +"'"
self.cursor.execute(sql)
sensors = self.cursor.fetchall()
new_sensors = []
for s in sensors:
t = [s[0], Utils.extract_loc_from_geometry(s[1])]
if t not in new_sensors:
new_sensors.append(t)
return new_sensors
def find_arterial_sensors(self, onstreet, direction):
#find all sensors on arterials
sql = "select distinct sensor_id, ST_AsText(start_lat_long) from arterial_congestion_config where last_seen_at >= '2015-01-01' and last_seen_at < '2016-01-01' and upper(onstreet) like '%" + onstreet + "%' and direction = '" + str(direction) +"'"
self.cursor.execute(sql)
sensors = self.cursor.fetchall()
new_sensors = []
for s in sensors:
t = [s[0], Utils.extract_loc_from_geometry(s[1])]
if t not in new_sensors:
new_sensors.append(t)
return new_sensors
def dict_road(self, link_loc, path, sensors, direction):
#build the dictionary of sensors on roads
dict_road = {}
used_s = []
for link in path:
dict_road[link] = []
dist1 = 99999
dist2 = 99999
lon1, lat1 = link_loc[link][0]
lon2, lat2 = link_loc[link][1]
for sensor in sensors:
lon_sen, lat_sen = sensor[1]
if Utils.is_in_bbox(lon1,lat1,lon2,lat2,lon_sen,lat_sen, direction) and Utils.map_dist(lon1,lat1,lon_sen,lat_sen) < 5000 and len(dict_road[link]) == 0:
dict_road[link].append(sensor)
if sensor not in used_s:
used_s.append(sensor)
continue
if direction == 0 or direction == 1:
d = Utils.map_dist(lon_sen, lat_sen, lon1, lat1)
if d < dist1 and (lat_sen-lat1)*(lat1-lat2) > 0:
sen1 = sensor
dist1 = d
d = Utils.map_dist(lon_sen, lat_sen, lon2, lat2)
if d < dist2 and (lat_sen-lat2)*(lat2-lat1) > 0:
sen2 = sensor
dist2 = d
else:
d = Utils.map_dist(lon_sen, lat_sen, lon1, lat1)
if d < dist1 and (lon_sen-lon1)*(lon1-lon2) >0:
sen1 = sensor
dist1 = d
d = Utils.map_dist(lon_sen, lat_sen, lon2, lat2)
if d < dist2 and (lon_sen-lon2)*(lon2-lon1) >0:
sen2 = sensor
dist2 = d
if len(dict_road[link]) == 0:
if dist1 < 8000:
dict_road[link].append(sen1)
if sen1 not in used_s:
used_s.append(sen1)
if dist2 < 8000:
dict_road[link].append(sen2)
if sen2 not in used_s:
used_s.append(sen2)
for s in used_s:
self.fileout.write(str(s[1][1])+','+ str(s[1][0])+'\n')
for s in used_s:
self.fileout.write(str(s[0])+'\n')
print "There are ", len(used_s), " sensors"
return dict_road
def map_sensor_highway(self, lamap, road_name, function_class_numeric, direction, start_loc, end_loc, start_street, end_street):
link_loc = lamap.locate_links(road_name, function_class_numeric)
bearing_links = lamap.filter_bearing(link_loc, direction)
start_link, end_link = lamap.find_start_end_link(link_loc, bearing_links, start_loc[0], start_loc[1], end_loc[0], end_loc[1])
path = lamap.fill_path(link_loc, bearing_links, start_link, end_link, direction)
for p in path:
self.fileout.write(str(link_loc[p][0][1])+','+str(link_loc[p][0][0])+'\n')
self.fileout.write("\n\n")
for p in path:
self.fileout.write(str(p)+'\n')
self.fileout.write("\n\n")
print "There are " + str(len(path)) + " links"
start_postmile, end_postmile = self.find_first_last_sensor(road_name, start_street, end_street, direction)
sensors = self.find_all_sensors(road_name, start_postmile, end_postmile, direction)
dict_sensors_roads = self.dict_road(link_loc, path, sensors, direction)
'''
for link in dict_sensors_roads:
for s in dict_sensors_roads[link]:
self.fileout.write(str(link_loc[link][0][1])+','+str(link_loc[link][0][0])+' '+str(link_loc[link][1][1])+','+str(link_loc[link][1][0])+'; '+str(s[1][1])+','+str(s[1][0])+'\n')
'''
return dict_sensors_roads
def map_sensor_arterial(self, lamap, road_name, function_class_numeric, direction, start_loc, end_loc):
link_loc = lamap.locate_links(road_name, function_class_numeric)
bearing_links = lamap.filter_bearing(link_loc, direction)
start_link, end_link = lamap.find_start_end_link(link_loc, bearing_links, start_loc[0], start_loc[1], end_loc[0], end_loc[1])
path = lamap.fill_path(link_loc, bearing_links, start_link, end_link, direction)
for p in path:
self.fileout.write(str(link_loc[p][0][1])+','+str(link_loc[p][0][0])+'\n')
self.fileout.write("\n\n")
for p in path:
self.fileout.write(str(p)+'\n')
self.fileout.write("\n\n")
print "There are " + str(len(path)) + " links"
sensors = self.find_arterial_sensors(road_name, direction)
dict_sensors_roads = self.dict_road(link_loc, path, sensors, direction)
'''
for link in dict_sensors_roads:
for s in dict_sensors_roads[link]:
self.fileout.write(str(link_loc[link][0][1])+','+str(link_loc[link][0][0])+' '+str(link_loc[link][1][1])+','+str(link_loc[link][1][0])+'; '+str(s[1][1])+','+str(s[1][0])+'\n')
'''
return dict_sensors_roads
if __name__ == '__main__':
lamap = HighwayEdges.Map()
fileout = open('path.txt', 'w')
lasensor = Sensor(lamap.cursor, fileout)
'''
#First Highway Section
road_name = "I-105"
function_class_numeric = 1
direction = 2 #0:N 1:S 2:E 3:W
start_loc = (-118.321807, 33.925263) #Crenshaw Blvd
end_loc = (-118.25863, 33.92744) #Central Avenue
start_street = "CRENSHAW"
end_street = "CENTRAL"
dict_sen = lasensor.map_sensor_highway(lamap, road_name, function_class_numeric, direction, start_loc, end_loc, start_street, end_street)
for link in dict_sen:
for sen in dict_sen[link]:
if len(dict_sen[link]) == 1:
sql = "insert into SS_SENSOR_MAPPING(link_id, sensor_id, one_to_one, section_id) values(%d,%d,%s,%s)"%(link, sen[0], "'Y'", "'Section 1'")
lasensor.cursor.execute(sql)
else:
sql = "insert into SS_SENSOR_MAPPING(link_id, sensor_id, one_to_many, section_id) values(%d,%d,%s,%s)"%(link, sen[0], "'Y'", "'Section 1'")
lasensor.cursor.execute(sql)
#Second Highway Section
road_name = "I-405"
function_class_numeric = 1
direction = 0 #0:N 1:S 2:E 3:W
start_loc = (-118.474060, 34.188839) #Victory Blvd
end_loc = (-118.472978, 34.220024) #Roscoe Blvd
start_street = "VICTORY"
end_street = "ROSCOE"
dict_sen = lasensor.map_sensor_highway(lamap, road_name, function_class_numeric, direction, start_loc, end_loc, start_street, end_street)
for link in dict_sen:
for sen in dict_sen[link]:
if len(dict_sen[link]) == 1:
sql = "insert into SS_SENSOR_MAPPING(link_id, sensor_id, one_to_one, section_id) values(%d,%d,%s,%s)"%(link, sen[0], "'Y'", "'Section 2'")
lasensor.cursor.execute(sql)
else:
sql = "insert into SS_SENSOR_MAPPING(link_id, sensor_id, one_to_many, section_id) values(%d,%d,%s,%s)"%(link, sen[0], "'Y'", "'Section 2'")
lasensor.cursor.execute(sql)
#Third Highway Section
road_name = "I-710"
function_class_numeric = 1
direction = 0 #0:N 1:S 2:E 3:W
start_loc = (-118.177954, 33.932878) #Imperial Hwy
end_loc = (-118.168525, 33.965665) #Florence Ave
start_street = "IMPERIAL"
end_street = "FLORENCE"
dict_sen = lasensor.map_sensor_highway(lamap, road_name, function_class_numeric, direction, start_loc, end_loc, start_street, end_street)
for link in dict_sen:
for sen in dict_sen[link]:
if len(dict_sen[link]) == 1:
sql = "insert into SS_SENSOR_MAPPING(link_id, sensor_id, one_to_one, section_id) values(%d,%d,%s,%s)"%(link, sen[0], "'Y'", "'Section 3'")
lasensor.cursor.execute(sql)
else:
sql = "insert into SS_SENSOR_MAPPING(link_id, sensor_id, one_to_many, section_id) values(%d,%d,%s,%s)"%(link, sen[0], "'Y'", "'Section 3'")
lasensor.cursor.execute(sql)
#First arterial Section
road_name = "NORMANDIE"
function_class_numeric = 3
direction = 1 #0:N 1:S 2:E 3:W
start_loc = (-118.300218, 34.032798) #W Adams Blvd
end_loc = (-118.300300, 34.010890) #Martin Luther King Jr Blvd
dict_sen = lasensor.map_sensor_arterial(lamap, road_name, function_class_numeric, direction, start_loc, end_loc)
for link in dict_sen:
for sen in dict_sen[link]:
if len(dict_sen[link]) == 1:
sql = "insert into SS_SENSOR_MAPPING(link_id, sensor_id, one_to_one, section_id) values(%d,%d,%s,%s)"%(link, sen[0], "'Y'", "'Section 4'")
lasensor.cursor.execute(sql)
else:
sql = "insert into SS_SENSOR_MAPPING(link_id, sensor_id, one_to_many, section_id) values(%d,%d,%s,%s)"%(link, sen[0], "'Y'", "'Section 4'")
lasensor.cursor.execute(sql)
#Second arterial Section
road_name = "BEVERLY"
function_class_numeric = 3
direction = 3 #0:N 1:S 2:E 3:W
start_loc = (-118.338534, 34.076152) #N Highland Ave
end_loc = (-118.361478, 34.076148) #Fairfax Ave
dict_sen = lasensor.map_sensor_arterial(lamap, road_name, function_class_numeric, direction, start_loc, end_loc)
for link in dict_sen:
for sen in dict_sen[link]:
if len(dict_sen[link]) == 1:
sql = "insert into SS_SENSOR_MAPPING(link_id, sensor_id, one_to_one, section_id) values(%d,%d,%s,%s)"%(link, sen[0], "'Y'", "'Section 5'")
lasensor.cursor.execute(sql)
else:
sql = "insert into SS_SENSOR_MAPPING(link_id, sensor_id, one_to_many, section_id) values(%d,%d,%s,%s)"%(link, sen[0], "'Y'", "'Section 5'")
lasensor.cursor.execute(sql)
#Third arterial Section
road_name = "GRAND"
function_class_numeric = 3
direction = 1 #0:N 1:S 2:E 3:W
start_loc = (-118.260674, 34.043035) #W Olympic Blvd
end_loc = (-118.266278, 34.035738) #Venice Blvd
dict_sen = lasensor.map_sensor_arterial(lamap, road_name, function_class_numeric, direction, start_loc, end_loc)
for link in dict_sen:
for sen in dict_sen[link]:
if len(dict_sen[link]) == 1:
sql = "insert into SS_SENSOR_MAPPING(link_id, sensor_id, one_to_one, section_id) values(%d,%d,%s,%s)"%(link, sen[0], "'Y'", "'Section 6'")
lasensor.cursor.execute(sql)
else:
sql = "insert into SS_SENSOR_MAPPING(link_id, sensor_id, one_to_many, section_id) values(%d,%d,%s,%s)"%(link, sen[0], "'Y'", "'Section 6'")
lasensor.cursor.execute(sql)
'''
fileout.close()
lamap.close_db()