Hi all,
We received more detour data from William Wong at the MTA. I then plotted the points on a google map to identify the detour. I will now use this data to test against my algorithm. Below is the image of the detour. The blue points are the shape of the route, the yellow points are the route the bus took. Here we can clearly see it took a different route.
We have created our first algorithm to detect detours. This took a few attempts as the distance between the route (shape of the route) and the vehicle point was given back in degrees.
The algorithm detects detours by comparing a vehicle's actual positions with a predefined route, represented as a polyline. Initially, it retrieves the route's list of points from the TripManager and converts these points into coordinates to form a LineString, which represents the route. Then, it fetches the vehicle's positions for the specified trip and vehicle ID from the VehiclePositionManager. To track potential detours, the algorithm initialises a counter (consecutiveOffRouteCount) for consecutive off-route points. For each vehicle position, it converts the latitude and longitude into a Point and calculates the distance from the vehicle's current position to the nearest point on the route using the getDistance method. If the squared distance exceeds a predefined threshold, the counter increments. If the number of consecutive off-route points surpasses another threshold, a detour is detected, and the function returns true. If the vehicle returns to the route (within the distance threshold), the counter resets. If no detour is detected throughout the loop, the function returns false. The getDistance method calculates the orthodromic (great-circle) distance between a point and the nearest point on the line by transforming both the point and the line to a new Coordinate Reference System (CRS), finding the nearest points, and then calculating the distance in the original CRS.
After this we tested it out against 2 sets of different data and the results were correct.
Test case data a route that was detoured:
Output from test:
The distance is 44.83928626961827
The distance is 44.83928626961827
The distance is 44.9238140452626
The distance is 94.46982859464073
The distance is 95.15532478218138
The distance is 174.14860191148065
The distance is 244.12263744454202
The distance is 270.15648688454263
The distance is 1457.4310928626046
The distance is 1540.6228428463392
The distance is 1580.2642361305539
Detour detected for Vehicle 766
Test case data from a route that wasn’t detoured:
Output from test:
The distance is 59.78319550063694
The distance is 113.14590662187429
No detour detected for Vehicle 766
Next week, we plan on:
Develop more test cases.
Allowing the algorithm to check multiple trips at a time.
Create another algorithm to detect detours.
Thanks,
Andrew