def test_generating_network_graph_geodataframe(network): gdfs = gngeojson.generate_geodataframes(network.graph) nodes, links = gdfs['nodes'], gdfs['links'] correct_nodes = { 'x': {'0': 528704.1425925883, '1': 528804.1425925883}, 'y': {'0': 182068.78193707118, '1': 182168.78193707118}} correct_links = {'u': {'link_0': '0', 'link_1': '0', 'link_2': '1'}, 'v': {'link_0': '1', 'link_1': '1', 'link_2': '0'}, 'length': {'link_0': 123, 'link_1': 123, 'link_2': 123}, 'attributes': {'link_0': float('nan'), 'link_1': { 'osm:way:highway': {'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 'unclassified'}}, 'link_2': float('nan')}, 'to': {'link_0': '1', 'link_1': '1', 'link_2': '0'}, 'from': {'link_0': '0', 'link_1': '0', 'link_2': '1'}, 'freespeed': {'link_0': 10.0, 'link_1': float('nan'), 'link_2': float('nan')}, 'id': {'link_0': 'link_0', 'link_1': 'link_1', 'link_2': 'link_2'}, 'capacity': {'link_0': 5.0, 'link_1': float('nan'), 'link_2': float('nan')}, 'modes': {'link_0': ['car', 'walk'], 'link_1': ['bike'], 'link_2': ['rail']}} assert_semantically_equal(nodes[set(nodes.columns) - {'geometry'}].to_dict(), correct_nodes) assert_semantically_equal(links[set(links.columns) - {'geometry'}].to_dict(), correct_links) assert round(nodes.loc['0', 'geometry'].coords[:][0][0], 7) == round(528704.1425925883, 7) assert round(nodes.loc['0', 'geometry'].coords[:][0][1], 7) == round(182068.78193707118, 7) assert round(nodes.loc['1', 'geometry'].coords[:][0][0], 7) == round(528804.1425925883, 7) assert round(nodes.loc['1', 'geometry'].coords[:][0][1], 7) == round(182168.78193707118, 7) points = links.loc['link_0', 'geometry'].coords[:] assert round(points[0][0], 7) == round(528704.1425925883, 7) assert round(points[0][1], 7) == round(182068.78193707118, 7) assert round(points[1][0], 7) == round(528804.1425925883, 7) assert round(points[1][1], 7) == round(182168.78193707118, 7) assert nodes.crs == "EPSG:27700" assert links.crs == "EPSG:27700"
def test_generating_schedule_graph_geodataframe(network): gdfs = gngeojson.generate_geodataframes(network.schedule.graph()) nodes, links = gdfs['nodes'], gdfs['links'] correct_nodes = {'services': {'0': {'service'}, '1': {'service'}}, 'routes': {'0': {'1', '2'}, '1': {'1', '2'}}, 'id': {'0': '0', '1': '1'}, 'x': {'0': 529455.7452394223, '1': 529350.7866124967}, 'y': {'0': 182401.37630677427, '1': 182388.0201078112}, 'epsg': {'0': 'epsg:27700', '1': 'epsg:27700'}, 'lat': {'0': 51.525696033239186, '1': 51.52560003323918}, 'lon': {'0': -0.13530998708775874, '1': -0.13682698708848137}, 's2_id': {'0': 5221390668020036699, '1': 5221390668558830581}, 'linkRefId': {'0': '1', '1': '2'}, 'name': {'0': '', '1': ''} } correct_links = {'services': {0: {'service'}}, 'routes': {0: {'1', '2'}}, 'u': {0: '0'}, 'v': {0: '1'}} assert_semantically_equal(nodes[set(nodes.columns) - {'geometry'}].to_dict(), correct_nodes) assert_semantically_equal(links[set(links.columns) - {'geometry'}].to_dict(), correct_links) assert round(nodes.loc['0', 'geometry'].coords[:][0][0], 7) == round(529455.7452394223, 7) assert round(nodes.loc['0', 'geometry'].coords[:][0][1], 7) == round(182401.37630677427, 7) assert round(nodes.loc['1', 'geometry'].coords[:][0][0], 7) == round(529350.7866124967, 7) assert round(nodes.loc['1', 'geometry'].coords[:][0][1], 7) == round(182388.0201078112, 7) points = links.loc[0, 'geometry'].coords[:] assert round(points[0][0], 7) == round(529455.7452394223, 7) assert round(points[0][1], 7) == round(182401.37630677427, 7) assert round(points[1][0], 7) == round(529350.7866124967, 7) assert round(points[1][1], 7) == round(182388.0201078112, 7) assert nodes.crs == "EPSG:27700" assert links.crs == "EPSG:27700"
def test_generating_edge_vph_geodataframe_for_route(schedule): nodes, links = gngeojson.generate_geodataframes(schedule.route('2').graph()) df = schedule.route('2').generate_trips_dataframe() df = use_schedule.generate_edge_vph_geodataframe(df, links) correct_df = GeoDataFrame({'hour': {0: Timestamp('1970-01-01 17:00:00'), 1: Timestamp('1970-01-01 19:00:00'), 2: Timestamp('1970-01-01 17:00:00'), 3: Timestamp('1970-01-01 19:00:00'), 4: Timestamp('1970-01-01 17:00:00'), 5: Timestamp('1970-01-01 18:00:00')}, 'from_stop': {0: '2', 1: '2', 2: '3', 3: '3', 4: '4', 5: '4'}, 'to_stop': {0: '1', 1: '1', 2: '2', 3: '2', 4: '3', 5: '3'}, 'route_name': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}, 'from_stop_name': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}, 'to_stop_name': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}, 'vph': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}, 'geometry': {0: LineString([(-7.557148039524952, 49.766825803756994), (-7.557106577683727, 49.76682779861249)]), 1: LineString([(-7.557148039524952, 49.766825803756994), (-7.557106577683727, 49.76682779861249)]), 2: LineString([(-7.557121424907424, 49.76683608549253), (-7.557148039524952, 49.766825803756994)]), 3: LineString([(-7.557121424907424, 49.76683608549253), (-7.557148039524952, 49.766825803756994)]), 4: LineString([(-7.5570681956375, 49.766856648946295), (-7.557121424907424, 49.76683608549253)]), 5: LineString([(-7.5570681956375, 49.766856648946295), (-7.557121424907424, 49.76683608549253)])}}) assert_geodataframe_equal(df, correct_df, check_less_precise=True)
def test_modal_subset(network): gdfs = gngeojson.generate_geodataframes(network.graph) nodes, links = gdfs['nodes'], gdfs['links'] car = links[links.apply(lambda x: gngeojson.modal_subset(x, {'car'}), axis=1)] assert len(car) == 1 assert car.loc['link_0', 'modes'] == ['car', 'walk']
def _generate_requests_for_simplified_network(n): """ Generates a dictionary describing pairs of nodes for which we need to request directions from Google directions API. For a simplified network n :param n: genet.Network :return: """ gdf_links = geojson.generate_geodataframes(n.modal_subgraph(modes='car'))['links'].to_crs("epsg:4326") gdf_links['path_polyline'] = gdf_links['geometry'].apply(lambda x: spatial.swap_x_y_in_linestring(x)) gdf_links['path_polyline'] = gdf_links['path_polyline'].apply( lambda x: spatial.encode_shapely_linestring_to_polyline(x)) gdf_links['path_nodes'] = gdf_links.apply(lambda x: (x['from'], x['to']), axis=1) gdf_links['origin'] = gdf_links['from'].apply(lambda x: n.node(x)) gdf_links['destination'] = gdf_links['to'].apply(lambda x: n.node(x)) return gdf_links.set_index(['from', 'to'])[['path_polyline', 'path_nodes', 'origin', 'destination']].T.to_dict()
def __init__(self, pt_graph, network_spatial_tree, modes, distance_threshold=30, step_size=10): self.service_modes = modes self.distance_threshold = distance_threshold self.step_size = step_size self.network_spatial_tree = network_spatial_tree self.pt_graph = pt_graph _gdf = gngeojson.generate_geodataframes(pt_graph) self.stops, self.pt_edges = _gdf['nodes'].to_crs( 'epsg:4326'), _gdf['links'].to_crs('epsg:4326') self.edges = self.pt_edges.loc[:, ['u', 'v', 'geometry']].copy() self.nodes = self.find_closest_links() if self.nodes.empty or len(set(self.nodes['id'])) == 1: logging.info( 'The problem did not find closest links for enough stops. If partial solution is allowed,' 'the stops will not be snapped and the routes will be entirely artificial.' ) self.is_partial = True self.problem_graph = nx.DiGraph() else: self.is_partial = False self.problem_graph = self.generate_problem_graph() if not self.is_viable(): self.is_partial = self.is_partially_viable() if self.is_partial: logging.warning( 'This Maximum Stable Set Problem is partially viable.') else: raise InvalidMaxStableSetProblem( 'This Maximum Stable Set Problem has at least one completely connected' 'catchment and cannot proceed to the solver.') self.solution = {} self.artificial_stops = {} self.artificial_links = {} self.unsolved_stops = set()
def test_generating_edge_vph_geodataframe(schedule): gdfs = gngeojson.generate_geodataframes(schedule.graph()) nodes, links = gdfs['nodes'], gdfs['links'] df = schedule.trips_with_stops_to_dataframe() df = use_schedule.generate_edge_vph_geodataframe(df, links) correct_df = GeoDataFrame({'hour': {0: Timestamp('1970-01-01 17:00:00'), 1: Timestamp('1970-01-01 18:00:00'), 2: Timestamp('1970-01-01 17:00:00'), 3: Timestamp('1970-01-01 19:00:00'), 4: Timestamp('1970-01-01 17:00:00'), 5: Timestamp('1970-01-01 19:00:00'), 6: Timestamp('1970-01-01 17:00:00'), 7: Timestamp('1970-01-01 19:00:00'), 8: Timestamp('1970-01-01 17:00:00'), 9: Timestamp('1970-01-01 19:00:00'), 10: Timestamp('1970-01-01 17:00:00'), 11: Timestamp('1970-01-01 18:00:00')}, 'from_stop': {0: '1', 1: '1', 2: '2', 3: '2', 4: '2', 5: '2', 6: '3', 7: '3', 8: '3', 9: '3', 10: '4', 11: '4'}, 'from_stop_name': {0: 'Stop_1', 1: 'Stop_1', 2: 'Stop_2', 3: 'Stop_2', 4: 'Stop_2', 5: 'Stop_2', 6: 'Stop_3', 7: 'Stop_3', 8: 'Stop_3', 9: 'Stop_3', 10: '', 11: ''}, 'to_stop': {0: '2', 1: '2', 2: '1', 3: '1', 4: '3', 5: '3', 6: '2', 7: '2', 8: '4', 9: '4', 10: '3', 11: '3'}, 'to_stop_name': {0: 'Stop_2', 1: 'Stop_2', 2: 'Stop_1', 3: 'Stop_1', 4: 'Stop_3', 5: 'Stop_3', 6: 'Stop_2', 7: 'Stop_2', 8: '', 9: '', 10: 'Stop_3', 11: 'Stop_3'}, 'vph': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1}, 'geometry': { 0: LineString([(4,2), (1,2)]), 1: LineString([(4,2), (1,2)]), 2: LineString([(1,2), (4,2)]), 3: LineString([(1,2), (4,2)]), 4: LineString([(1,2), (3,3)]), 5: LineString([(1,2), (3,3)]), 6: LineString([(3,3), (1,2)]), 7: LineString([(3,3), (1,2)]), 8: LineString([(3,3), (7,5)]), 9: LineString([(3,3), (7,5)]), 10: LineString([(7,5), (3,3)]), 11: LineString([(7,5), (3,3)])}}, crs='epsg:27700') assert_geodataframe_equal(df.sort_index(axis=1), correct_df.sort_index(axis=1), check_less_precise=True)
def test_sanitising_geodataframes_with_ids_list(tmpdir): n = Network('epsg:27700') n.add_node('0', attribs={'x': 528704.1425925883, 'y': 182068.78193707118}) n.add_node('1', attribs={'x': 528804.1425925883, 'y': 182168.78193707118}) n.add_link('link_0', '0', '1', attribs={ 'length': 123, 'modes': ['car', 'walk'], 'ids': ['1', '2'] }) correct_nodes = { 'x': { '0': 528704.1425925883, '1': 528804.1425925883 }, 'y': { '0': 182068.78193707118, '1': 182168.78193707118 } } correct_links = { 'length': { 0: 123 }, 'modes': { 0: 'car,walk' }, 'from': { 0: '0' }, 'to': { 0: '1' }, 'id': { 0: 'link_0' }, 'ids': { 0: '1,2' }, 'u': { 0: '0' }, 'v': { 0: '1' }, 'key': { 0: 0 } } nodes, links = gngeojson.generate_geodataframes(n.graph) nodes = sanitiser.sanitise_geodataframe(nodes) links = sanitiser.sanitise_geodataframe(links) assert_semantically_equal(nodes[['x', 'y']].to_dict(), correct_nodes) assert_semantically_equal( links[['length', 'from', 'to', 'id', 'ids', 'u', 'v', 'key', 'modes']].to_dict(), correct_links)
def test_generating_network_graph_geodataframe(network): nodes, links = gngeojson.generate_geodataframes(network.graph) correct_nodes = { 'x': { '0': 528704.1425925883, '1': 528804.1425925883 }, 'y': { '0': 182068.78193707118, '1': 182168.78193707118 } } correct_links = { 'length': { 0: 123, 1: 123, 2: 123 }, 'modes': { 0: ['car', 'walk'], 1: ['bike'], 2: ['rail'] }, 'freespeed': { 0: 10.0, 1: float('nan'), 2: float('nan') }, 'capacity': { 0: 5.0, 1: float('nan'), 2: float('nan') }, 'from': { 0: '0', 1: '0', 2: '1' }, 'to': { 0: '1', 1: '1', 2: '0' }, 'id': { 0: 'link_0', 1: 'link_1', 2: 'link_2' }, 'u': { 0: '0', 1: '0', 2: '1' }, 'v': { 0: '1', 1: '1', 2: '0' }, 'key': { 0: 0, 1: 1, 2: 0 }, 'attributes': { 0: float('nan'), 1: { 'osm:way:highway': { 'name': 'osm:way:highway', 'class': 'java.lang.String', 'text': 'unclassified' } }, 2: float('nan') } } assert_semantically_equal( nodes[set(nodes.columns) - {'geometry'}].to_dict(), correct_nodes) assert_semantically_equal( links[set(links.columns) - {'geometry'}].to_dict(), correct_links) assert round(nodes.loc['0', 'geometry'].coords[:][0][0], 7) == round(-0.14625948709424305, 7) assert round(nodes.loc['0', 'geometry'].coords[:][0][1], 7) == round(51.52287873323954, 7) assert round(nodes.loc['1', 'geometry'].coords[:][0][0], 7) == round(-0.14478238148334213, 7) assert round(nodes.loc['1', 'geometry'].coords[:][0][1], 7) == round(51.523754629002234, 7) points = links.loc[0, 'geometry'].coords[:] assert round(points[0][0], 7) == round(-0.14625948709424305, 7) assert round(points[0][1], 7) == round(51.52287873323954, 7) assert round(points[1][0], 7) == round(-0.14478238148334213, 7) assert round(points[1][1], 7) == round(51.523754629002234, 7) assert nodes.crs == "EPSG:4326" assert links.crs == "EPSG:4326"