def test_trip_state_incorrect_next_stop_id(self): trip_state_incorrect = TripState(self.trip, Point.FromLatLng(42.14446157431765, 24.80178254507307), '3') trip_state_correct = TripState(self.trip, Point.FromLatLng(42.14446157431765, 24.80178254507307), '2') self.assertEqual(trip_state_correct.distance, trip_state_incorrect.distance) trip_state_incorrect = TripState(self.trip, Point.FromLatLng(42.14446157431765, 24.80178254507307), '1') trip_state_correct = TripState(self.trip, Point.FromLatLng(42.14446157431765, 24.80178254507307), '2') self.assertEqual(trip_state_correct.distance, trip_state_incorrect.distance)
def test_trip_state_after_last_stop(self): trip_state = TripState(self.trip, Point.FromLatLng(42.13256957053682, 24.79477347922245), '') self.assertEqual(0, trip_state.get_stop_progress()) self.assertEqual(1, trip_state.get_trip_progress()) self.assertEqual(5, trip_state.get_prev_stop_seq()) self.assertEqual(self.stop_times[-1][0], trip_state.get_estimated_scheduled_time()) trip_state = TripState(self.trip, Point.FromLatLng(42.131219133974184,24.79318410158157), '') self.assertEqual(0, trip_state.get_stop_progress()) self.assertEqual(1, trip_state.get_trip_progress()) self.assertEqual(5, trip_state.get_prev_stop_seq()) self.assertEqual(self.stop_times[-1][0], trip_state.get_estimated_scheduled_time())
def test_trip_state_before_first_stop(self): trip_state = TripState(self.trip, Point.FromLatLng(42.14507245405113, 24.79940072944254), '') self.assertEqual(0, trip_state.get_stop_progress()) self.assertEqual(0, trip_state.get_trip_progress()) self.assertEqual(0, trip_state.get_prev_stop_seq()) self.assertEqual(0, trip_state.get_estimated_scheduled_time()) trip_state = TripState(self.trip, Point.FromLatLng(42.14491993843798, 24.79993315462003), '') self.assertEqual(0, trip_state.get_stop_progress()) self.assertEqual(0, trip_state.get_trip_progress()) self.assertEqual(0, trip_state.get_prev_stop_seq()) self.assertEqual(0, trip_state.get_estimated_scheduled_time())
def debug_stop_distances(self): stop_coords = [Point.FromLatLng(self._stop_times[i][2].stop_lat, self._stop_times[i][2].stop_lon) for i in range(len(self._stop_times))] errors = [] for stop in stop_coords: pt, i = self.poly.GetClosestPoint(stop) errors.append(pt.GetDistanceMeters(stop)) return errors
def test_trip_state_in_between_stops(self): trip_state = TripState(self.trip, Point.FromLatLng(42.14446157431765, 24.80178254507307), '') self.assertEqual(1, trip_state.get_prev_stop_seq()) self.assertTrue(trip_state.get_estimated_scheduled_time() < self.stop_times[1][0]) trip_state = TripState(self.trip, Point.FromLatLng(42.14178793873418, 24.79772549935979), '') self.assertEqual(2, trip_state.get_prev_stop_seq()) self.assertTrue(self.stop_times[1][1] < trip_state.get_estimated_scheduled_time() < self.stop_times[2][0]) trip_state = TripState(self.trip, Point.FromLatLng(42.13851564680248, 24.79707830644129), '') self.assertEqual(3, trip_state.get_prev_stop_seq()) self.assertTrue(self.stop_times[2][1] < trip_state.get_estimated_scheduled_time() < self.stop_times[3][0]) trip_state = TripState(self.trip, Point.FromLatLng(42.13432309381219, 24.7953958493531), '') self.assertEqual(4, trip_state.get_prev_stop_seq()) self.assertTrue(self.stop_times[3][1] < trip_state.get_estimated_scheduled_time() < self.stop_times[4][0])
def test_trip_state_error(self): DELTA = 1e-3 trip_state = TripState(self.trip, Point.FromLatLng(42.14446157431765, 24.80178254507307), '') self.assertTrue(abs(trip_state.debug_error()[0] - trip_state.error) < DELTA) trip_state = TripState(self.trip, Point.FromLatLng(42.14178793873418, 24.79772549935979), '') self.assertTrue(abs(trip_state.debug_error()[0] - trip_state.error) < DELTA) trip_state = TripState(self.trip, Point.FromLatLng(42.13851564680248, 24.79707830644129), '') self.assertTrue(abs(trip_state.debug_error()[0] - trip_state.error) < DELTA) trip_state = TripState(self.trip, Point.FromLatLng(42.13432309381219, 24.7953958493531), '') self.assertTrue(abs(trip_state.debug_error()[0] - trip_state.error) < DELTA) trip_state = TripState(self.trip, Point.FromLatLng(42.131219133974184,24.79318410158157), '') self.assertTrue(abs(trip_state.debug_error()[0] - trip_state.error) < DELTA) trip_state = TripState(self.trip, Point.FromLatLng(42.14507245405113, 24.79940072944254), '') self.assertTrue(abs(trip_state.debug_error()[0] - trip_state.error) < DELTA)
def _scan_for_stops(self, STOP_ERROR): """"This is necessary, because shape_dist_traveled column is not reliable.""" current_stop_index = 0 accum_distance = 0 for i in range(len(self.poly.GetPoints()) - 1): pt_a, pt_b = self.poly.GetPoint(i), self.poly.GetPoint(i + 1) cur_segment_len = self.poly.distance[i] while current_stop_index < len(self._stop_times): stop = Point.FromLatLng(self._stop_times[current_stop_index][2].stop_lat, self._stop_times[current_stop_index][2].stop_lon) dist_to_stop = pt_a.GetDistanceMeters(stop) res = reach_to_point(stop, pt_a, pt_b, dist_to_stop, cur_segment_len, STOP_ERROR) if res: pt_on_shape = res[0] self._stop_distances[current_stop_index] = accum_distance + pt_a.GetDistanceMeters(pt_on_shape) current_stop_index += 1 else: break if current_stop_index == len(self._stop_times): break accum_distance += cur_segment_len return current_stop_index == len(self._stop_times)
def __init__(self, trip, vehicle_point, next_stop_id, last_known = 0): self.trip = trip self.vehicle = vehicle_point self.next_stop_id = next_stop_id self.calculated_length = None self.shape = trip._schedule.GetShape(trip.shape_id) self.poly = Poly() for pt in self.shape.points: self.poly.AddPoint(Point.FromLatLng(pt[0], pt[1])) self.poly.distance = [self.poly.GetPoint(i-1).GetDistanceMeters(self.poly.GetPoint(i)) for i in range(1, len(self.poly.GetPoints()))] self._stop_times = trip.GetTimeStops() self._stop_distances = [None for i in range(len(self._stop_times))] if not self._scan_for_stops(self.STOP_ERROR) and not self._scan_for_stops(self.STOP_ERROR*3): raise StopFarFromPolylineException() self.next_stop_idx = self._get_next_stop_idx() if not self._find_vehicle(last_known): raise VehicleOutOfPolylineException() self._find_previous_stop_indx() self._calculate_distances()
def get_distance_to_end_stop(self): end_stop = Point.FromLatLng(self._stop_times[-1][2].stop_lat, self._stop_times[-1][2].stop_lon) return self.vehicle.GetDistanceMeters(end_stop)
def test_trip_state_with_next_stop_id(self): trip_state = TripState(self.trip, Point.FromLatLng(42.14446157431765, 24.80178254507307), '2') self.assertEqual(1, trip_state.get_prev_stop_seq()) self.assertTrue(trip_state.get_estimated_scheduled_time() < self.stop_times[1][0])
def test_trip_state_at_last_stop(self): trip_state = TripState(self.trip, Point.FromLatLng(42.13357424874254,24.79510188102722), '') self.assertEqual(0, trip_state.get_stop_progress()) self.assertEqual(1, trip_state.get_trip_progress()) self.assertEqual(5, trip_state.get_prev_stop_seq()) self.assertEqual(self.stop_times[-1][0], trip_state.get_estimated_scheduled_time())
def test_trip_state_at_first_stop(self): trip_state = TripState(self.trip, Point.FromLatLng(42.14530077994279, 24.800326824188232), '') self.assertEqual(0, trip_state.get_stop_progress()) self.assertEqual(0, trip_state.get_trip_progress()) self.assertEqual(1, trip_state.get_prev_stop_seq()) self.assertEqual(0, trip_state.get_estimated_scheduled_time())
def test_displaced_stop(self): self.trip = self.schedule.GetTrip('247285') try: TripState(self.trip, Point.FromLatLng(42.14446157431765, 24.80178254507307), '') except StopFarFromPolylineException as e: self.fail("Stop finding doesn't work")