def correct_services_from_test_pt2matsim_schedule(): stops = [Stop(id='26997928P', x='528464.1342843144', y='182179.7435136598', epsg='epsg:27700'), Stop(id='26997928P.link:1', x='528464.1342843144', y='182179.7435136598', epsg='epsg:27700')] stops[0].add_additional_attributes({'name': 'Brunswick Place (Stop P)', 'isBlocking': 'false'}) stops[1].add_additional_attributes({'name': 'Brunswick Place (Stop P)', 'isBlocking': 'false'}) services = [Service(id='10314', routes=[ Route( route_short_name='12', mode='bus', stops=stops, route=['1'], trips={ 'trip_id': ['VJ00938baa194cee94700312812d208fe79f3297ee_04:40:00'], 'trip_departure_time': ['04:40:00'], 'vehicle_id': ['veh_bus_1'] }, arrival_offsets=['00:00:00', '00:02:00'], departure_offsets=['00:00:00', '00:02:00'], await_departure=[True, True] ) ]) ] return services
def different_test_service(): return Service( id='different_service', routes=[ Route(route_short_name='route', mode='bus', stops=[ Stop(id='3', x=528504.1342843144, y=182155.7435136598, epsg='epsg:27700'), Stop(id='4', x=528504.1342843144, y=182155.7435136598, epsg='epsg:27700') ], trips={ 'trip_id': ['VJ00938baa194cee94700312812d208fe79f3297ee_04:40:00'], 'trip_departure_time': ['04:40:00'], 'vehicle_id': ['veh_1_bus'] }, arrival_offsets=['00:00:00', '00:02:00'], departure_offsets=['00:00:00', '00:02:00']) ])
def correct_schedule(): return Schedule(epsg='epsg:27700', services=[ Service(id='service', routes=[ Route(id='1', route_short_name='route', mode='bus', stops=[ Stop(id='0', x=529455.7452394223, y=182401.37630677427, epsg='epsg:27700', linkRefId='1'), Stop(id='1', x=529350.7866124967, y=182388.0201078112, epsg='epsg:27700', linkRefId='2')], trips={'trip_id': ['VJ00938baa194cee94700312812d208fe79f3297ee_04:40:00'], 'trip_departure_time': ['04:40:00'], 'vehicle_id': ['veh_1_bus']}, arrival_offsets=['00:00:00', '00:02:00'], departure_offsets=['00:00:00', '00:02:00'], route=['1', '2']), Route(id='2', route_short_name='route1', mode='bus', stops=[ Stop(id='0', x=529455.7452394223, y=182401.37630677427, epsg='epsg:27700', linkRefId='1'), Stop(id='1', x=529350.7866124967, y=182388.0201078112, epsg='epsg:27700', linkRefId='2')], trips={'trip_id': ['Blep_04:40:00'], 'trip_departure_time': ['05:40:00'], 'vehicle_id': ['veh_2_bus']}, arrival_offsets=['00:00:00', '00:03:00'], departure_offsets=['00:00:00', '00:05:00'], route=['1', '2']) ]) ])
def test_build_graph_builds_correct_graph(): route = Route(route_short_name='name', mode='bus', stops=[Stop(id='1', x=4, y=2, epsg='epsg:27700'), Stop(id='2', x=1, y=2, epsg='epsg:27700'), Stop(id='3', x=3, y=3, epsg='epsg:27700'), Stop(id='4', x=7, y=5, epsg='epsg:27700')], trips={'trip_id': ['1', '2'], 'trip_departure_time': ['1', '2'], 'vehicle_id': ['veh_3_bus', 'veh_4_bus']}, arrival_offsets=['1', '2'], departure_offsets=['1', '2']) g = route.graph() assert_semantically_equal(dict(g.nodes(data=True)), {'1': {'routes': {''}, 'id': '1', 'x': 4.0, 'y': 2.0, 'epsg': 'epsg:27700', 'lat': 49.76682779861249, 'lon': -7.557106577683727, 's2_id': 5205973754090531959, 'name': ''}, '2': {'routes': {''}, 'id': '2', 'x': 1.0, 'y': 2.0, 'epsg': 'epsg:27700', 'lat': 49.766825803756994, 'lon': -7.557148039524952, 's2_id': 5205973754090365183, 'name': ''}, '3': {'routes': {''}, 'id': '3', 'x': 3.0, 'y': 3.0, 'epsg': 'epsg:27700', 'lat': 49.76683608549253, 'lon': -7.557121424907424, 's2_id': 5205973754090203369, 'name': ''}, '4': {'routes': {''}, 'id': '4', 'x': 7.0, 'y': 5.0, 'epsg': 'epsg:27700', 'lat': 49.766856648946295, 'lon': -7.5570681956375, 's2_id': 5205973754097123809, 'name': ''}}) assert_semantically_equal(g.edges(data=True)._adjdict, {'1': {'2': {'routes': {''}}}, '2': {'3': {'routes': {''}}}, '3': {'4': {'routes': {''}}}, '4': {}})
def test_read_gtfs_returns_expected_schedule(correct_stops_to_service_mapping_from_test_gtfs, correct_stops_to_route_mapping_from_test_gtfs): schedule = Schedule('epsg:4326') schedule.read_gtfs_schedule(gtfs_test_file, '20190604') assert schedule.services['1001'] == Service( '1001', [Route( route_short_name='BTR', mode='bus', stops=[Stop(id='BSE', x=-0.1413621, y=51.5226864, epsg='epsg:4326'), Stop(id='BSN', x=-0.140053, y=51.5216199, epsg='epsg:4326')], trips={'BT1': '03:21:00'}, arrival_offsets=['0:00:00', '0:02:00'], departure_offsets=['0:00:00', '0:02:00'] )]) assert schedule.services['1002'] == Service( '1002', [Route( route_short_name='RTR', mode='rail', stops=[Stop(id='RSN', x=-0.1410946, y=51.5231335, epsg='epsg:4326'), Stop(id='RSE', x=-0.1421595, y=51.5192615, epsg='epsg:4326')], trips={'RT1': '03:21:00'}, arrival_offsets=['0:00:00', '0:02:00'], departure_offsets=['0:00:00', '0:02:00'] )]) assert_semantically_equal(schedule.stop_to_service_ids_map(), correct_stops_to_service_mapping_from_test_gtfs) assert_semantically_equal(schedule.stop_to_route_ids_map(), correct_stops_to_route_mapping_from_test_gtfs)
def test_adding_schedules_retains_shared_graph(schedule): schedule_2 = Schedule(epsg='epsg:27700', services=[ Service(id='service3', routes=[ Route(id='31', route_short_name='3route1', mode='bus', stops=[ Stop(id='30', x=529455.7452394223, y=182401.37630677427, epsg='epsg:27700', linkRefId='0'), Stop(id='31', x=529350.7866124967, y=182388.0201078112, epsg='epsg:27700', linkRefId='1')], trips={'trip_id': ['route1_04:40:00'], 'trip_departure_time': ['04:40:00'], 'vehicle_id': ['veh_0_bus']}, arrival_offsets=['00:00:00', '00:02:00'], departure_offsets=['00:00:00', '00:02:00'], route=['0', '1']), Route(id='32', route_short_name='3route2', mode='bus', stops=[ Stop(id='31', x=529455.7452394223, y=182401.37630677427, epsg='epsg:27700', linkRefId='1'), Stop(id='32', x=529350.7866124967, y=182388.0201078112, epsg='epsg:27700', linkRefId='2')], trips={'trip_id': ['route2_05:40:00'], 'trip_departure_time': ['05:40:00'], 'vehicle_id': ['veh_1_bus']}, arrival_offsets=['00:00:00', '00:03:00'], departure_offsets=['00:00:00', '00:05:00'], route=['1', '2']) ])]) schedule.add(schedule_2) assert_all_elements_share_graph(schedule)
def test_adding_merges_separable_schedules(route): schedule = Schedule(epsg='epsg:4326', services=[Service(id='1', routes=[route])]) before_graph_nodes = schedule.reference_nodes before_graph_edges = schedule.reference_edges a = Stop(id='10', x=40, y=20, epsg='epsg:27700', linkRefId='1') b = Stop(id='20', x=10, y=20, epsg='epsg:27700', linkRefId='2') c = Stop(id='30', x=30, y=30, epsg='epsg:27700', linkRefId='3') d = Stop(id='40', x=70, y=50, epsg='epsg:27700', linkRefId='4') schedule_to_be_added = Schedule(epsg='epsg:4326', services=[Service(id='2', routes=[ Route( route_short_name='name', mode='bus', stops=[a, b, c, d], trips={'1': '1', '2': '2'}, arrival_offsets=['00:00:00', '00:03:00', '00:07:00', '00:13:00'], departure_offsets=['00:00:00', '00:05:00', '00:09:00', '00:15:00'], route=['1', '2', '3', '4'], id='1') ])]) tba_graph_nodes = schedule_to_be_added.reference_nodes tba_graph_edges = schedule_to_be_added.reference_edges schedule.add(schedule_to_be_added) assert schedule.services == { '1': Service(id='1', routes=[route]), '2': Service(id='2', routes=[route])} assert schedule.epsg == 'epsg:4326' assert schedule.epsg == schedule_to_be_added.epsg assert set(schedule._graph.nodes()) == set(before_graph_nodes) | set(tba_graph_nodes) assert set(schedule._graph.edges()) == set(before_graph_edges) | set(tba_graph_edges)
def correct_services_from_test_gtfs(): services = [] services.append( Service('1001', [ Route( route_short_name='BTR', mode='bus', stops=[ Stop( id='BSE', x=-0.1413621, y=51.5226864, epsg='epsg:4326'), Stop(id='BSN', x=-0.140053, y=51.5216199, epsg='epsg:4326') ], trips={'BT1': '03:21:00'}, arrival_offsets=['0:00:00', '0:02:00'], departure_offsets=['0:00:00', '0:02:00']) ])) services.append( Service('1002', [ Route( route_short_name='RTR', mode='rail', stops=[ Stop( id='RSN', x=-0.1410946, y=51.5231335, epsg='epsg:4326'), Stop( id='RSE', x=-0.1421595, y=51.5192615, epsg='epsg:4326') ], trips={'RT1': '03:21:00'}, arrival_offsets=['0:00:00', '0:02:00'], departure_offsets=['0:00:00', '0:02:00']) ])) return services
def basic_service(): return Service(id='service1', routes=[ Route(id='1', route_short_name='route1', mode='bus', stops=[ Stop('0', x=1, y=1, epsg='epsg:4326'), Stop('1', x=2, y=2, epsg='epsg:4326'), Stop('2', x=3, y=3, epsg='epsg:4326') ], trips={ 'trip_id': ['route1_04:40:00'], 'trip_departure_time': ['04:40:00'], 'vehicle_id': ['veh_bus_1'] }, arrival_offsets=['00:00:00', '00:02:00'], departure_offsets=['00:00:00', '00:02:00'], route=[]), Route(id='2', route_short_name='route2', mode='bus', stops=[ Stop('1', x=2, y=2, epsg='epsg:4326'), Stop('2', x=3, y=3, epsg='epsg:4326') ], trips={ 'trip_id': ['route2_05:40:00'], 'trip_departure_time': ['05:40:00'], 'vehicle_id': ['veh_bus_2'] }, arrival_offsets=['00:00:00', '00:03:00'], departure_offsets=['00:00:00', '00:05:00'], route=[]), Route(id='3', route_short_name='route3', mode='bus', stops=[ Stop('0', x=1, y=1, epsg='epsg:4326'), Stop('1', x=2, y=2, epsg='epsg:4326'), ], trips={ 'trip_id': ['route1_04:40:00'], 'trip_departure_time': ['04:40:00'], 'vehicle_id': ['veh_bus_3'] }, arrival_offsets=['00:00:00', '00:02:00'], departure_offsets=['00:00:00', '00:02:00'], route=[]), Route(id='4', route_short_name='route4', mode='bus', stops=[ Stop('2', x=3, y=3, epsg='epsg:4326'), Stop('1', x=2, y=2, epsg='epsg:4326'), Stop('0', x=1, y=1, epsg='epsg:4326') ], trips={ 'trip_id': ['route2_05:40:00'], 'trip_departure_time': ['05:40:00'], 'vehicle_id': ['veh_bus_4'] }, arrival_offsets=['00:00:00', '00:03:00'], departure_offsets=['00:00:00', '00:05:00'], route=[]) ])
def similar_non_exact_test_route(): return Route(route_short_name='route', mode='bus', stops=[Stop(id='0', x=528504.1342843144, y=182155.7435136598, epsg='epsg:27700'), Stop(id='0', x=528504.1342843144, y=182155.7435136598, epsg='epsg:27700')], trips={'trip_id': ['Blep_04:40:00'], 'trip_departure_time': ['05:40:00'], 'vehicle_id': ['veh_1_bus']}, arrival_offsets=['00:00:00', '00:03:00'], departure_offsets=['00:00:00', '00:05:00'])
def test_accessing_route(schedule): assert schedule.route('1') == Route(route_short_name='name', mode='bus', id='1', stops=[Stop(id='1', x=4, y=2, epsg='epsg:27700'), Stop(id='2', x=1, y=2, epsg='epsg:27700'), Stop(id='3', x=3, y=3, epsg='epsg:27700'), Stop(id='4', x=7, y=5, epsg='epsg:27700')], trips={'1': '1', '2': '2'}, arrival_offsets=['00:00:00', '00:03:00', '00:07:00', '00:13:00'], departure_offsets=['00:00:00', '00:05:00', '00:09:00', '00:15:00'])
def test_stops_equal(): a = Stop(id='0', x=528504.1342843144, y=182155.7435136598, epsg='epsg:27700') b = Stop(id='0', x=528504.1342843144, y=182155.7435136598, epsg='epsg:27700') assert a == b
def self_looping_route(): return Route( route_short_name='name', mode='bus', stops=[Stop(id='1', x=4, y=2, epsg='epsg:27700'), Stop(id='1', x=4, y=2, epsg='epsg:27700'), Stop(id='3', x=3, y=3, epsg='epsg:27700'), Stop(id='4', x=7, y=5, epsg='epsg:27700')], trips = {'trip_id': ['1', '2'], 'trip_departure_time': ['10:00:00', '20:00:00'], 'vehicle_id': ['veh_3_bus', 'veh_4_bus']}, arrival_offsets=['00:00:00', '00:03:00', '00:07:00', '00:13:00'], departure_offsets=['00:00:00', '00:05:00', '00:09:00', '00:15:00'])
def test_stops_exact(): a = Stop(id='0', x=-0.14910908709500162, y=51.52370573323939, epsg='epsg:4326') b = Stop(id='0', x=-0.14910908709500162, y=51.52370573323939, epsg='epsg:4326') assert a.is_exact(b)
def route(): a = Stop(id='1', x=4, y=2, epsg='epsg:27700', linkRefId='1') b = Stop(id='2', x=1, y=2, epsg='epsg:27700', linkRefId='2') c = Stop(id='3', x=3, y=3, epsg='epsg:27700', linkRefId='3') d = Stop(id='4', x=7, y=5, epsg='epsg:27700', linkRefId='4') return Route( route_short_name='name', mode='bus', stops=[a, b, c, d], trips = {'trip_id': ['1', '2'], 'trip_departure_time': ['10:00:00', '20:00:00'], 'vehicle_id': ['veh_1_bus', 'veh_2_bus']}, arrival_offsets=['00:00:00', '00:03:00', '00:07:00', '00:13:00'], departure_offsets=['00:00:00', '00:05:00', '00:09:00', '00:15:00'], route=['1', '2', '3', '4'], id='1')
def test_has_linkRefId_with_stop_which_has_linkRefId(): a = Stop(id='0', x=-0.14910908709500162, y=51.52370573323939, epsg='epsg:4326') a.add_additional_attributes({'linkRefId': '1234'}) assert a.has_linkRefId()
def test__repr__shows_projection(): a = Stop(id='0', x=528504.1342843144, y=182155.7435136598, epsg='epsg:27700') r = a.__repr__() assert 'epsg:27700' in r
def test_info_shows_id_projection_and_lat_lon(): a = Stop(id='0', x=528504.1342843144, y=182155.7435136598, epsg='epsg:27700') info = a.info() assert a.id in info assert 'epsg:27700' in info assert str(a._round_lat()) in info assert str(a._round_lon()) in info
def test_has_correctly_ordered_route_with_stop_missing_linkrefid(): a = Stop(id='1', x=4, y=2, epsg='epsg:27700') a.add_additional_attributes({'linkRefId': '10'}) b = Stop(id='2', x=4, y=2, epsg='epsg:27700') b.add_additional_attributes({'linkRefId': '20'}) c = Stop(id='3', x=4, y=2, epsg='epsg:27700') r = Route(route_short_name='name', mode='bus', stops=[a, b, c], trips={'trip_id': ['1', '2'], 'trip_departure_time': ['10:00:00', '20:00:00'], 'vehicle_id': ['veh_1_bus', 'veh_2_bus']}, arrival_offsets=['1', '2'], departure_offsets=['1', '2'], route=['10', '15', '30', '25', '20'], id='1') assert not r.has_correctly_ordered_route()
def test_reproject_stops_with_transformer(): a = Stop(id='0', x=528504.1342843144, y=182155.7435136598, epsg='epsg:27700') transformer = Transformer.from_proj(Proj('epsg:27700'), Proj('epsg:4326'), always_xy=True) a.reproject('epsg:4326', transformer) correct_lat = 51.52370573323939 correct_lon = -0.14910908709500162 assert_semantically_equal({ 'x': a.x, 'y': a.y }, { 'x': correct_lon, 'y': correct_lat }) assert round(a.lat, SPATIAL_TOLERANCE) == round(correct_lat, SPATIAL_TOLERANCE) assert round(a.lon, SPATIAL_TOLERANCE) == round(correct_lon, SPATIAL_TOLERANCE) assert a.epsg == 'epsg:4326'
def self_looping_route(): return Route( route_short_name='name', mode='bus', stops=[ Stop(id='1', x=4, y=2, epsg='epsg:27700'), Stop(id='1', x=4, y=2, epsg='epsg:27700'), Stop(id='3', x=3, y=3, epsg='epsg:27700'), Stop(id='4', x=7, y=5, epsg='epsg:27700') ], trips={ '1': '1', '2': '2' }, arrival_offsets=['00:00:00', '00:03:00', '00:07:00', '00:13:00'], departure_offsets=['00:00:00', '00:05:00', '00:09:00', '00:15:00'])
def write_transitLinesTransitRoute(transitLine, transitRoutes, transportMode): mode = transportMode['transportMode'] service_id = transitLine['transitLine']['id'] service_routes = [] for transitRoute, transitRoute_val in transitRoutes.items(): stops = [ Stop(s['stop']['refId'], x=transit_stop_id_mapping[s['stop']['refId']]['x'], y=transit_stop_id_mapping[s['stop']['refId']]['y'], epsg=epsg, transformer=transformer) for s in transitRoute_val['stops'] ] for s in stops: s.add_additional_attributes(transit_stop_id_mapping[s.id]) arrival_offsets = [] departure_offsets = [] await_departure = [] for stop in transitRoute_val['stops']: if 'departureOffset' not in stop[ 'stop'] and 'arrivalOffset' not in stop['stop']: pass elif 'departureOffset' not in stop['stop']: arrival_offsets.append(stop['stop']['arrivalOffset']) departure_offsets.append(stop['stop']['arrivalOffset']) elif 'arrivalOffset' not in stop['stop']: arrival_offsets.append(stop['stop']['departureOffset']) departure_offsets.append(stop['stop']['departureOffset']) else: arrival_offsets.append(stop['stop']['arrivalOffset']) departure_offsets.append(stop['stop']['departureOffset']) if 'awaitDeparture' in stop['stop']: await_departure.append( str(stop['stop']['awaitDeparture']).lower() in ['true', '1']) route = [ r_val['link']['refId'] for r_val in transitRoute_val['links'] ] trips = {} for dep in transitRoute_val['departure_list']: trips[dep['departure'] ['id']] = dep['departure']['departureTime'] r = Route(route_short_name=transitLine['transitLine']['name'], mode=mode, stops=stops, route=route, trips=trips, arrival_offsets=arrival_offsets, departure_offsets=departure_offsets, id=transitRoute, await_departure=await_departure) service_routes.append(r) services.append(Service(id=service_id, routes=service_routes))
def test_print_shows_info(mocker): mocker.patch.object(Stop, 'info') a = Stop(id='0', x=528504.1342843144, y=182155.7435136598, epsg='epsg:27700') a.print() Stop.info.assert_called_once()
def test_initiate_stop_with_kwargs(): a = Stop(id='0', x=528504.1342843144, y=182155.7435136598, epsg='epsg:27700', linkRefId='1') assert a.has_linkRefId() assert a.additional_attributes == ['linkRefId']
def test__str__shows_info(): a = Stop(id='0', x=528504.1342843144, y=182155.7435136598, epsg='epsg:27700') assert 'epsg:27700' in a.__str__() assert str(a._round_lat()) in a.__str__() assert str(a._round_lon()) in a.__str__()
def test_stop_already_in_epsg_4326_gives_lat_lon(): a = Stop(id='0', x=-0.14910908709500162, y=51.52370573323939, epsg='epsg:4326') assert a.lat == a.x assert a.lon == a.y
def test_is_valid_with_single_stop_network(): route = Route(route_short_name='name', mode='bus', stops=[Stop(id='1', x=4, y=2, epsg='epsg:27700')], trips={}, arrival_offsets=[], departure_offsets=[]) assert not route.is_valid_route()
def test_has_more_than_one_stop_with_route_with_single_stop(): route = Route(route_short_name='name', mode='bus', stops=[Stop(id='1', x=4, y=2, epsg='epsg:27700')], trips={}, arrival_offsets=[], departure_offsets=[]) assert not route.has_more_than_one_stop()
def test_add_additional_attributes_to_stops(): a = Stop(id='0', x=528504.1342843144, y=182155.7435136598, epsg='epsg:27700', name='Brunswick Place (Stop P)') a.add_additional_attributes({'isBlocking': True}) assert a.isBlocking == True
def test_iterating_through_additional_attribs(): s = Stop(id='0', x=528504.1342843144, y=182155.7435136598, epsg='epsg:27700', additional_attrib_1='hey', additional_attrib_2='what?') assert set(s.iter_through_additional_attributes()) == { ('additional_attrib_1', 'hey'), ('additional_attrib_2', 'what?') }