def main(args): parser = argparse.ArgumentParser() parser.add_argument('output', metavar='graph.pickle', help='Ouput graph file') parser.add_argument( '--connection', default='postgresql://*****:*****@localhost/osrm', help='Postgres connection string. Default %(default)s') parser.add_argument('--prune', action='store_true', help='Collapse redundant edges, prune tails') parser.add_argument('--verbose', action='store_true', help='Increase logging level') args = parser.parse_args() logging.basicConfig( level=logging.INFO if args.verbose else logging.WARNING) log.info("Creating DB engine") engine = create_engine(args.connection, echo=False) log.info("Creating DB session") Session = sessionmaker(bind=engine) session = Session() g = build_graph(session) if args.prune: log.info("Collapsing unidirectional strings") pruned = graphtools.collapse_degree_2_vtxs(g) log.info("Removed %i thru-nodes", pruned) log.info("Collapsing bidirectional strings") pruned = graphtools.collapse_bidirectional_streets(g) log.info("Removed %i thru-nodes", pruned) log.info("Snipping tails") snipped = graphtools.delete_degree_1_vtxs(g) log.info("Removed %i tails", snipped) loners = graphtools.delete_degree_0_vtxs(g) log.info("Removed %i loner-nodes (should be zero)", loners) redundancies = graphtools.identify_rendudant_nodes(g) log.info("Marked %i nodes as redundant", redundancies) log.info("Saving graph to %s", args.output) g.save(args.output)
def test_build_graph(): with test_db_session() as session: # Make a y-shaped graph node1 = OSRMNode(1, 1, 2, True, False) node2 = OSRMNode(2, 3, 4, False, False) node3 = OSRMNode(3, 5, 6, False, False) node4 = OSRMNode(4, 7, 8, False, False) session.add(node1) session.add(node2) session.add(node3) session.add(node4) # The source-sink nodes aren't necessarily # in "forward" order. edge_12 = OSRMEdge(2, 1, 5, 5, False) edge_23 = OSRMEdge(2, 3, 5, 5, False) edge_24 = OSRMEdge(4, 2, 5, 5, False) session.add_all([edge_12, edge_23, edge_24]) session.commit() # The combination of the hash + forward # nature should define the edge frequency. freq_12 = OSRMEdgeFrequencies( edge=OSRMEdge.hash_edge(1, 2), # 1->2 forward=True, freq=12, geom=None) freq_21 = OSRMEdgeFrequencies( edge=OSRMEdge.hash_edge(1, 2), # 2->1 forward=False, freq=21, geom=None) freq_34 = OSRMEdgeFrequencies( edge=OSRMEdge.hash_edge(2, 3), # 2-> 3 forward=True, freq=32, geom=None) freq_24 = OSRMEdgeFrequencies( edge=OSRMEdge.hash_edge(2, 4), # This means 2->4 forward=True, freq=24, geom=None) session.add_all([freq_12, freq_21, freq_34, freq_24]) session.commit() results = session.query(OSRMEdgeFrequencies).all() eq_(len(results), 4) eq_(results[0].edgeobj.source, 2) eq_(results[0].edgeobj.sink, 1) graph = build_graph(session) eq_(len(graph.vs), 4) eq_(len(graph.es), 4) assert (graph.is_directed) eq_(set(graph.vs["osm_id"]), set([1, 2, 3, 4])) eq_(len(graph.vs.select(osm_id=1)), 1) vtx_1 = graph.vs.select(osm_id=1)[0] eq_(vtx_1.indegree(), 1) eq_(vtx_1.outdegree(), 1) eq_(vtx_1.degree(), 2) eq_(vtx_1.successors()[0]["osm_id"], 2) eq_(vtx_1.predecessors()[0]["osm_id"], 2) vtx_2 = graph.vs.select(osm_id=2)[0] eq_(vtx_2.indegree(), 1) eq_(vtx_2.outdegree(), 3) eq_(vtx_2.degree(), 4) # Check edge weights are correct # vtx 0 is node 1 eq_(gt.output_weights(graph, 0), [12]) eq_(gt.output_weights(graph, 1), [21, 32, 24])