-
Notifications
You must be signed in to change notification settings - Fork 2
/
attenuator.py
389 lines (326 loc) · 13.7 KB
/
attenuator.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
import argparse
import json
from ..tools import (
cdot_geospatial_api,
geospatial_tools,
combination,
date_tools,
wzdx_translator,
)
import logging
from datetime import datetime, timedelta
PROGRAM_NAME = "ExperimentalCombinationGeotabAttenuator"
PROGRAM_VERSION = "1.0"
ATTENUATOR_TIME_AHEAD_SECONDS = 30 * 60
ISO_8601_FORMAT_STRING = "%Y-%m-%dT%H:%M:%SZ"
def main():
wzdxFile, geotabFile, output_dir, updateDates = parse_rtdh_arguments()
wzdx = json.loads(open(wzdxFile, "r").read())
geotab_avl = [json.loads(open(geotabFile, "r").read())]
outputPath = output_dir + "/wzdx_attenuator_combined.geojson"
if updateDates == "true":
geotab_avl[0]["avl_location"]["source"]["collection_timestamp"] = (
date_tools.get_current_ts_millis() / 1000
)
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(days=2))
)
combined_events = get_combined_events(geotab_avl, wzdx)
if len(combined_events) == 0:
print(
"No overlapping events found between WZDx and Geotab ATMA 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 attenuator WZDx combination script
Returns:
str: WZDx file path
str: Geotab file path
str: Output directory
str: Update dates boolean
"""
parser = argparse.ArgumentParser(
description="Combine WZDx and Geotab AVL (ATMA) 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("geotabFile", 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.geotabFile, args.outputDir, args.updateDates
def validate_directionality(geotab: dict, wzdx: dict) -> bool:
"""Validate that the directionality of the Geotab and WZDx objects match
Args:
geotab (dict): Geotab AVL message
wzdx (dict): WZDx message
Returns:
bool: Whether the directionality of the Geotab and WZDx objects match
"""
geotab_bearing = geotab["avl_location"]["position"]["bearing"]
wzdx_direction = wzdx["features"][0]["properties"]["core_details"]["direction"]
geotab_direction = geospatial_tools.get_closest_direction_from_bearing(
geotab_bearing, wzdx_direction
)
return geotab_direction == wzdx_direction
def validate_dates(geotab: dict, wzdx: dict) -> bool:
"""Validate that the Geotab date falls within the WZDx date range
Args:
geotab (dict): Geotab AVL message
wzdx (dict): WZDx message
Returns:
bool: Whether the Geotab date falls within the WZDx date range
"""
geotab_date = date_tools.get_unix_from_iso_string(
geotab["avl_location"]["source"]["collection_timestamp"]
)
logging.debug(f"Geotab: {json.dumps(geotab)}")
if not geotab_date:
geotab_date = (
geotab["avl_location"]["source"]["collection_timestamp"] * 1000
if geotab["avl_location"]["source"]["collection_timestamp"]
else None
)
if not geotab_date:
logging.debug(f"No geotab date found")
return False
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"]
)
logging.debug(f"Dates: {wzdx_start_date}, {geotab_date}, {wzdx_end_date}")
return wzdx_start_date <= geotab_date <= wzdx_end_date
def get_combined_events(geotab_msgs: list[dict], wzdx_msgs: list[dict]) -> list[dict]:
"""Combine/integrate overlapping Geotab AVL ATMA 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
"""
active_wzdx_msgs = wzdx_translator.filter_active_wzdx(wzdx_msgs)
combined_events = []
for i in identify_overlapping_features(geotab_msgs, active_wzdx_msgs):
geotab_msg, wzdx_msg = i
event_status = wzdx_translator.get_event_status(wzdx_msg["features"][0])
if event_status in ["active"]:
wzdx = combine_geotab_with_wzdx(geotab_msg, wzdx_msg)
combined_events.append(wzdx)
return combined_events
def identify_overlapping_features(
geotab_msgs: list[dict], wzdx_msgs: list[dict]
) -> list[tuple[dict, dict]]:
"""Identify overlapping Geotab AVL ATMA and WZDx messages
Args:
geotab_msgs (list[dict]): Geotab avl messages
wzdx_msgs (list[dict]): WZDx messages
Returns:
list[tuple[dict, dict]]: List of tuples of Geotab and WZDx messages that overlap
"""
geotab_routes = {}
matching_routes = []
for geotab_msg in geotab_msgs:
geometry = geotab_msg["avl_location"]["position"]
route_details = cdot_geospatial_api.GeospatialApi().get_route_and_measure(
(geometry["latitude"], geometry["longitude"])
)
if not route_details:
logging.debug(
f"No route details for Geotab object: {geotab_msg['rtdh_message_id']}"
)
continue
geotab_msg["route_details_start"] = route_details
geotab_msg["route_details_end"] = None
if route_details["Route"] in geotab_routes:
geotab_routes[route_details["Route"]].append(geotab_msg)
else:
geotab_routes[route_details["Route"]] = [geotab_msg]
if not geotab_routes:
return []
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") or 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 route_details for WZDx object: {wzdx['features'][0]['id']}"
)
continue
wzdx["route_details_start"] = route_details_start
wzdx["route_details_end"] = route_details_end
logging.debug(
json.dumps(route_details) + ", " + json.dumps(route_details_end)
)
if not wzdx.get("route_details_start"):
logging.debug(
f"Unable to retrieve start point route details for WZDx event {wzdx['features'][0].get('id')}"
)
continue
matching_geotab_routes = geotab_routes.get(
wzdx["route_details_start"]["Route"], []
)
if matching_geotab_routes:
logging.debug(
f"FOUND MATCHING GEOTAB ROUTE FOR {wzdx['features'][0]['id']}"
)
if not wzdx.get("route_details_end"):
logging.debug(
f"Unable to retrieve start point route details for WZDx event {wzdx['features'][0].get('id')}"
)
continue
if (
wzdx["route_details_start"]["Route"]
!= wzdx["route_details_end"]["Route"]
):
logging.debug(
f"Start/End don't match: {wzdx['route_details_start']['Route']}, {wzdx['route_details_end']['Route']}"
)
continue
for geotab in matching_geotab_routes:
logging.debug(
f"VALIDATING MATCH: {combination.does_route_overlap(geotab, wzdx)}, {validate_directionality(geotab, wzdx)}, {validate_dates(geotab, wzdx)}"
)
if (
combination.does_route_overlap(geotab, wzdx)
and validate_directionality(geotab, wzdx)
and validate_dates(geotab, wzdx)
):
matching_routes.append((geotab, wzdx))
return matching_routes
return matching_routes
def combine_geotab_with_wzdx(geotab_avl: dict, wzdx_wzdx: dict) -> dict:
"""Combine Geotab AVL ATMA message with WZDx message. Converts WZDx message to planned-moving-area, with geometry and mile markers from ATMA position and speed.
Args:
geotab_avl (dict): Geotab AVL ATMA message
wzdx_wzdx (dict): WZDx message
Returns:
dict: Combined WZDx message
"""
combined_feature = wzdx_wzdx["features"][0]
# determine distance ahead to use in moving area
speed = geotab_avl["avl_location"]["position"]["speed"]
bearing = geotab_avl["avl_location"]["position"]["bearing"]
route_details = geotab_avl["route_details_start"]
distance_ahead = get_distance_ahead_miles(speed, ATTENUATOR_TIME_AHEAD_SECONDS)
# determine new geometry and mile markers
event_start_marker = wzdx_wzdx["route_details_start"]["Measure"]
event_end_marker = wzdx_wzdx["route_details_end"]["Measure"]
mMin = min(event_start_marker, event_end_marker)
mMax = max(event_start_marker, event_end_marker)
geometry, startMarker, endMarker = get_geometry_for_distance_ahead(
distance_ahead, route_details, bearing, mMin, mMax
)
combined_feature["properties"]["beginning_milepost"] = startMarker
combined_feature["properties"]["ending_milepost"] = endMarker
combined_feature["geometry"]["coordinates"] = geometry
# update route details for whichever side was overwritten by geotab
if (
combined_feature["properties"]["beginning_milepost"]
== geotab_avl["route_details_start"]["Measure"]
):
combined_feature["properties"]["route_details_start"] = geotab_avl[
"route_details_start"
]
elif (
combined_feature["properties"]["ending_milepost"]
== geotab_avl["route_details_start"]["Measure"]
):
combined_feature["properties"]["route_details_end"] = geotab_avl[
"route_details_start"
]
# update description and data sources
combined_feature["properties"]["core_details"][
"description"
] += f" Moving area updated by Geotab ATMA {geotab_avl['avl_location']['vehicle']['id']}"
# add fields for traceability
combined_feature["properties"]["experimental_source_type"] = "geotab"
combined_feature["properties"]["experimental_source_id"] = geotab_avl[
"rtdh_message_id"
]
combined_feature["properties"]["geotab_id"] = geotab_avl["avl_location"]["vehicle"][
"id"
]
combined_feature["properties"]["geotab_message"] = geotab_avl
# update update_dates
update_date = date_tools.get_iso_string_from_datetime(datetime.now())
combined_feature["properties"]["core_details"]["update_date"] = update_date
wzdx_wzdx["feed_info"]["data_sources"][0]["update_date"] = update_date
wzdx_wzdx["features"][0] = combined_feature
return wzdx_wzdx
def get_geometry_for_distance_ahead(
distance_ahead: float, route_details: dict, bearing: float, mMin: float, mMax: float
) -> tuple[list[list[float]], float, float]:
"""Get the geometry for a distance ahead on a route, within the bounds of mile markers
Args:
distance_ahead (float): Distance ahead of vehicle
route_details (dict): GIS route details
bearing (float): Vehicle bearing
mMin (float): Minimum mile marker of event, to generate geometry within
mMax (float): Maximum mile marker of event, to generate geometry within
Returns:
tuple[list[list[float]], float, float]: Geometry, start mile marker, end mile marker
"""
route_ahead = cdot_geospatial_api.GeospatialApi().get_route_geometry_ahead(
route_details["Route"],
route_details["Measure"],
bearing,
distance_ahead,
routeDetails=route_details,
mMin=mMin,
mMax=mMax,
)
if not route_details:
return [], mMin, mMax
return (
route_ahead["coordinates"],
route_ahead["start_measure"],
route_ahead["end_measure"],
)
# Speed in mph, time in seconds
def get_distance_ahead_miles(speed: float, time: float) -> float:
"""Get the distance ahead in miles given a speed and time. Vehicle speed is "floored" at 5 mph.
Args:
speed (float): Speed of vehicle, in mph
time (float): Time ahead of vehicle, in seconds
Returns:
float: Distance ahead of vehicle, in miles
"""
speed = max(speed, 5)
return speed * time / 3600
if __name__ == "__main__":
main()