-
Notifications
You must be signed in to change notification settings - Fork 2
/
icone.py
406 lines (339 loc) · 14.1 KB
/
icone.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
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
import argparse
import json
from datetime import datetime
import logging
from datetime import datetime, timedelta
import glob
from typing import Literal
from ..tools import combination, wzdx_translator, geospatial_tools, date_tools
PROGRAM_NAME = "ExperimentalCombinationIcone"
PROGRAM_VERSION = "1.0"
ISO_8601_FORMAT_STRING = "%Y-%m-%dT%H:%M:%SZ"
START_TIME_THRESHOLD_MILLISECONDS = 1000 * 60 * 60 * 24 * 31 # 31 days
END_TIME_THRESHOLD_MILLISECONDS = 1000 * 60 * 60 * 24 * 31 # 31 days
def main(outputPath="./tests/data/output/wzdx_icone_combined.json"):
wzdxFile, iconeDirectory, output_dir, updateDates = parse_rtdh_arguments()
wzdx = json.loads(open(wzdxFile, "r").read())
icone = [
json.loads(open(f_name).read())
for f_name in glob.glob(f"${iconeDirectory}/*.json")
]
outputPath = output_dir + "/wzdx_experimental.geojson"
if updateDates == "true":
for i in icone:
i["rtdh_timestamp"] = date_tools.get_current_ts_millis() / 1000
i["event"]["header"]["start_timestamp"] = (
date_tools.get_iso_string_from_datetime(
datetime.now() - timedelta(days=1)
)
)
i["features"][0]["properties"]["end_date"] = (
date_tools.get_iso_string_from_datetime(
datetime.now() + timedelta(hours=4)
)
)
wzdx[0]["features"][0]["properties"]["start_date"] = (
date_tools.get_iso_string_from_datetime(datetime.now() - timedelta(days=2))
)
wzdx[0]["features"][0]["properties"]["end_date"] = (
date_tools.get_iso_string_from_datetime(datetime.now() - timedelta(hours=2))
)
combined_events = get_combined_events(icone, wzdx)
if len(combined_events) == 0:
print("No overlapping events found between WZDx and iCone data. See logs for more information.")
else:
with open(outputPath, "w+") as f:
f.write(json.dumps(combined_events, indent=2))
print(f"Successfully wrote combined WZDx file to: {outputPath}")
# parse script command line arguments
def parse_rtdh_arguments() -> tuple[str, str, str, str]:
"""Parse command line arguments for icone WZDx combination script
Returns:
str: WZDx file path
str: iCone directory path
str: output directory path
str: updateDates flag (true/false)
"""
parser = argparse.ArgumentParser(
description="Combine WZDx and iCone arrow board data"
)
parser.add_argument(
"--version", action="version", version=f"{PROGRAM_NAME} {PROGRAM_VERSION}"
)
parser.add_argument("wzdxFile", help="planned event file path")
parser.add_argument("iconeJsonDirectory", help="planned event file path")
parser.add_argument(
"--outputDir", required=False, default="./", help="output directory"
)
parser.add_argument(
"--updateDates",
required=False,
default="false",
help="Boolean (true/false), Update dates to the current date to pass time filter",
)
args = parser.parse_args()
return args.wzdxFile, args.iconeDirectory, args.outputDir, args.updateDates
def get_direction_from_route_details(route_details: dict) -> str:
"""Get direction from GIS route details
Args:
route_details (dict): GIS route details
Returns:
str: direction | "unknown"
"""
return route_details.get("Direction")
def get_direction(
street: str, coords: list[list[float]], route_details: dict = None
) -> str:
"""Get road direction from street name, coordinates, or route details
Args:
street (str): Street name, like "I-25 NB"
coords (list[list[float]]): List of coordinates, to pull direction from
route_details (dict, optional): GIS route details, defaults to None
Returns:
Literal['unknown', 'eastbound', 'westbound', 'northbound', 'southbound']: Road direction
"""
direction = wzdx_translator.parse_direction_from_street_name(street)
if not direction and route_details:
direction = get_direction_from_route_details(route_details)
if not direction:
direction = geospatial_tools.get_road_direction_from_coordinates(coords)
return direction
def get_combined_events(
icone_standard_msgs: list[dict], wzdx_msgs: list[dict]
) -> list[dict]:
"""Combine/integrate overlapping iCone messages into WZDx messages
Args:
icone_standard_msgs (list[dict]): iCone RTDH standard messages
wzdx_msgs (list[dict]): WZDx messages
Returns:
list[dict]: Combined WZDx messages
"""
combined_events = []
filtered_wzdx_msgs = wzdx_translator.filter_wzdx_by_event_status(
wzdx_msgs, ["pending", "completed_recently"]
)
for i in identify_overlapping_features_icone(
icone_standard_msgs, filtered_wzdx_msgs
):
icone_msg, wzdx_msg = i
event_status = wzdx_translator.get_event_status(wzdx_msg["features"][0])
if event_status in ["pending", "completed_recently"]:
wzdx = combine_icone_with_wzdx(icone_msg, wzdx_msg, event_status)
if wzdx:
combined_events.append(wzdx)
return combined_events
def combine_icone_with_wzdx(
icone_standard: dict,
wzdx_wzdx: dict,
event_status: Literal["active", "pending", "planned", "completed"],
) -> dict:
"""Combine iCone message with WZDx message
Args:
icone_standard (dict): iCone RTDH standard message
wzdx_wzdx (dict): WZDx message
event_status (Literal["active", "pending", "planned", "completed"]): WZDx event status
Returns:
dict: Combined WZDx message
"""
combined_event = wzdx_wzdx
updated = False
if event_status == "pending":
combined_event["features"][0]["properties"]["start_date"] = (
date_tools.get_iso_string_from_unix(
icone_standard["event"]["header"]["start_timestamp"]
)
)
updated = True
elif event_status == "completed_recently":
combined_event["features"][0]["properties"]["end_date"] = (
date_tools.get_iso_string_from_unix(
icone_standard["event"]["header"]["start_timestamp"] + 60 * 60
)
)
combined_event["features"][0]["properties"]["core_details"]["description"] += (
" " + icone_standard["event"]["header"]["description"]
)
updated = True
logging.debug("Updated: " + str(updated))
if updated:
update_date = date_tools.get_iso_string_from_datetime(datetime.now())
combined_event["features"][0]["properties"]["core_details"][
"update_date"
] = update_date
combined_event["feed_info"]["data_sources"][0]["update_date"] = update_date
combined_event["features"][0]["properties"][
"experimental_source_type"
] = "icone"
combined_event["features"][0]["properties"]["experimental_source_id"] = (
icone_standard["rtdh_message_id"]
)
combined_event["features"][0]["properties"]["icone_id"] = icone_standard[
"event"
]["source"]["id"]
combined_event["features"][0]["properties"]["icone_message"] = icone_standard
for i in ["route_details_start", "route_details_end"]:
if i in combined_event:
del combined_event[i]
return combined_event
else:
return None
def get_route_details_for_icone(coordinates: list[list[float]]) -> tuple[dict, dict]:
"""Get route details for iCone message
Args:
coordinates (list[list[float]]): List of coordinates
Returns:
tuple[dict, dict]: Route details for start and end coordinates
"""
route_details_start = combination.get_route_details(
coordinates[0][1], coordinates[0][0]
)
if len(coordinates) == 1 or (
len(coordinates) == 2 and coordinates[0] == coordinates[1]
):
route_details_end = None
else:
route_details_end = combination.get_route_details(
coordinates[-1][1], coordinates[-1][0]
)
return route_details_start, route_details_end
def validate_directionality_wzdx_icone(icone: dict, wzdx: dict) -> bool:
"""Validate directionality between iCone and WZDx messages
Args:
icone (dict): iCone RTDH standard message
wzdx (dict): WZDx message
Returns:
bool: Directionality match
"""
direction_1 = icone["event"]["detail"]["direction"]
direction_2 = wzdx["features"][0]["properties"]["core_details"]["direction"]
return direction_1 in [None, "unknown", "undefined"] or direction_1 == direction_2
# Filter out iCone and WZDx messages which are not within the time interval
def validate_dates(icone: dict, wzdx: dict) -> bool:
"""Validate date overlap between iCone and WZDx messages
Args:
icone (dict): iCone RTDH standard message
wzdx (dict): WZDx message
Returns:
bool: Date match
"""
wzdx_start_date = date_tools.get_unix_from_iso_string(
wzdx["features"][0]["properties"]["start_date"]
)
wzdx_end_date = date_tools.get_unix_from_iso_string(
wzdx["features"][0]["properties"]["end_date"]
)
icone_start_date = icone["event"]["header"]["start_timestamp"] * 1000
icone_end_date = (
icone["event"]["header"]["end_timestamp"] * 100
if icone["event"]["header"]["end_timestamp"]
else None
)
return wzdx_start_date - icone_start_date < START_TIME_THRESHOLD_MILLISECONDS or (
icone_end_date == None
or icone_end_date - wzdx_end_date < END_TIME_THRESHOLD_MILLISECONDS
)
def identify_overlapping_features_icone(
icone_standard_msgs: list[dict], wzdx_msgs: list[dict]
) -> list[tuple[dict, dict]]:
"""Identify overlapping iCone and WZDx messages
Args:
icone_standard_msgs (list[dict]): iCone RTDH standard messages
wzdx_msgs (list[dict]): WZDx messages
Returns:
list[tuple[dict, dict]]: Overlapping iCone and WZDx messages
"""
icone_routes = {}
wzdx_routes = {}
matching_routes = []
# Step 1: Add route info to iCone messages
for icone in icone_standard_msgs:
icone["route_details_start"] = (
icone["event"].get("additional_info", {}).get("route_details_start")
)
icone["route_details_end"] = (
icone["event"].get("additional_info", {}).get("route_details_end")
)
route_details_start, route_details_end = get_route_details_for_icone(
icone["event"]["geometry"]
)
if not route_details_start:
logging.debug(
f"Invalid route details for iCone: {icone['event']['source']['id']}"
)
continue
icone["route_details_start"] = route_details_start
icone["route_details_end"] = route_details_end
if (
icone["route_details_end"]
and route_details_start["Route"] != route_details_end["Route"]
):
logging.debug(
f"Mismatched routes for iCone feature {icone['event']['source']['id']}"
)
continue
if route_details_start["Route"] in icone_routes:
icone_routes[route_details_start["Route"]].append(icone)
else:
icone_routes[route_details_start["Route"]] = [icone]
# Step 2: Add route info to WZDx messages
for wzdx in wzdx_msgs:
wzdx["route_details_start"] = wzdx["features"][0]["properties"].get(
"route_details_start"
)
wzdx["route_details_end"] = wzdx["features"][0]["properties"].get(
"route_details_end"
)
if (
wzdx.get("route_details_start")
and not wzdx.get("route_details_end")
or not wzdx.get("route_details_start")
and wzdx.get("route_details_end")
):
logging.debug(
f"Missing route_details for WZDx object: {wzdx['features'][0]['id']}"
)
continue
if not wzdx.get("route_details_start") and not wzdx.get("route_details_end"):
route_details_start, route_details_end = (
combination.get_route_details_for_wzdx(wzdx["features"][0])
)
if not route_details_start or not route_details_end:
logging.debug(f"Missing WZDx route details {wzdx['features'][0]['id']}")
continue
wzdx["route_details_start"] = route_details_start
wzdx["route_details_end"] = route_details_end
else:
route_details_start = wzdx["route_details_start"]
route_details_end = wzdx["route_details_end"]
if route_details_start["Route"] != route_details_end["Route"]:
logging.debug(f"Mismatched routes for feature {wzdx['features'][0]['id']}")
continue
logging.debug(
"Route details: " + str(route_details_start) + str(route_details_end)
)
if route_details_start["Route"] in wzdx_routes:
wzdx_routes[route_details_start["Route"]].append(wzdx)
else:
wzdx_routes[route_details_start["Route"]] = [wzdx]
if not icone_routes:
logging.debug("No routes found for icone")
return []
if not wzdx_routes:
logging.debug("No routes found for wzdx")
return []
logging.error("Match iCone: " + str(icone_standard_msgs) + str(wzdx_msgs))
# Step 3: Identify overlapping events
for wzdx_route_id, wzdx_matched_msgs in wzdx_routes.items():
matching_icone_routes = icone_routes.get(wzdx_route_id, [])
for match_icone in matching_icone_routes:
for match_wzdx in wzdx_matched_msgs:
# require routes to overlap, directionality to match, and dates to match
if (
combination.does_route_overlap(match_icone, match_wzdx)
and validate_directionality_wzdx_icone(match_icone, match_wzdx)
and validate_dates(match_icone, match_wzdx)
):
matching_routes.append((match_icone, match_wzdx))
return matching_routes
if __name__ == "__main__":
main()