def test_freq_backref(): with test_db_session() as session: node1 = OSRMNode(1, 1, 2, True, False) node2 = OSRMNode(2, 3, 4, False, False) session.add(node1) session.add(node2) the_edge = OSRMEdge(1, 2, 5, 5, False) session.add(the_edge) session.commit() the_freq = OSRMEdgeFrequencies( edge=OSRMEdge.hash_edge(1, 2), forward=True, freq=10, geom=None ) session.add(the_freq) the_freq_backward = OSRMEdgeFrequencies( edge=OSRMEdge.hash_edge(1, 2), forward=False, freq=11, geom=None ) session.add(the_freq_backward) session.commit() results = session.query(OSRMEdgeFrequencies).all() eq_(len(results), 2) eq_(results[0].freq, 10) eq_(results[0].forward, True) # test join eq_(results[0].edgeobj.source, 1) eq_(results[0].edgeobj.sink, 2)
def test_freq_backref(): with test_db_session() as session: node1 = OSRMNode(1, 1, 2, True, False) node2 = OSRMNode(2, 3, 4, False, False) session.add(node1) session.add(node2) the_edge = OSRMEdge(1, 2, 5, 5, False) session.add(the_edge) session.commit() the_freq = OSRMEdgeFrequencies(edge=OSRMEdge.hash_edge(1, 2), forward=True, freq=10, geom=None) session.add(the_freq) the_freq_backward = OSRMEdgeFrequencies(edge=OSRMEdge.hash_edge(1, 2), forward=False, freq=11, geom=None) session.add(the_freq_backward) session.commit() results = session.query(OSRMEdgeFrequencies).all() eq_(len(results), 2) eq_(results[0].freq, 10) eq_(results[0].forward, True) # test join eq_(results[0].edgeobj.source, 1) eq_(results[0].edgeobj.sink, 2)
def test_insert_osrm_routestep(): with test_db_session() as session: route_hash = OSRMRoute.hash_route(1) route = OSRMRoute(route_hash=route_hash, start_lat=2, start_lon=3, end_lat=4, end_lon=5, duration=6, nsteps=7) session.add(route) node1 = OSRMNode(1, 1, 2, True, False) node2 = OSRMNode(2, 3, 4, False, False) node3 = OSRMNode(3, 5, 6, False, False) session.add(node1) session.add(node2) session.add(node3) edge1 = OSRMEdge(1, 2, 5, 0.3, True) edge2 = OSRMEdge(2, 3, 6, 0.3, True) session.add(edge1) session.add(edge2) step1 = OSRMRouteStep(route_hash=route_hash, step_idx=0, edge_id=OSRMEdge.hash_edge(1, 2), forward=OSRMEdge.is_forward(1, 2)) step2 = OSRMRouteStep(route_hash=route_hash, step_idx=1, edge_id=OSRMEdge.hash_edge(3, 2), forward=OSRMEdge.is_forward(3, 2)) session.add(step1) session.add(step2) session.commit() result = session.query(OSRMRoute).filter( OSRMRoute.route_hash == route_hash).all() eq_(len(result), 1) eq_(result[0].start_lat, 2) eq_(result[0].nsteps, 7) steps = session.query(OSRMRouteStep).all() eq_(steps[0].edge_id, OSRMEdge.hash_edge(1, 2)) eq_(steps[0].edge_id, OSRMEdge.hash_edge(2, 1)) # commutes eq_(steps[0].step_idx, 0) eq_(steps[0].forward, True) eq_(steps[1].step_idx, 1) eq_(steps[1].forward, False) eq_(steps[0].edge.source_node.lon, 2) eq_(steps[0].edge.source_node.lat, 1)
def upload_osrm_binary(binaryfd, dbsession, merge=False, commit_every=1000): ''' Upload OSRM data into the db :param: binaryfd - a file descript pointing to the .osrm data :param: dbsession - an active :class:`sqlalchemy.Session` ''' insert_method = dbsession.add if not merge else dbsession.merge log.info("Loading OSRM node data") nodes_added = 0 for node in unpack_osrm_nodes(binaryfd): ormified = OSRMNode(node.id, node.lat, node.lon, node.bollard, node.traffic_light) insert_method(ormified) nodes_added += 1 if nodes_added % commit_every == 0: log.info("Added %i nodes", nodes_added) dbsession.commit() dbsession.commit() log.info("Added %i nodes", nodes_added) log.info("Loading OSRM edge data") existing_edges = set() edges_added = 0 for edge in unpack_osrm_edges(binaryfd): ormified = OSRMEdge( edge.node_a, edge.node_b, edge.distance, edge.weight, edge.bidirectional, ) # make sure we don't j key = OSRMEdge.hash_edge(edge.node_a, edge.node_b) if key in existing_edges: log.warning("Skipping existing edge: %s", repr(edge)) continue existing_edges.add(key) insert_method(ormified) edges_added += 1 if edges_added % commit_every == 0: log.info("Added %i edges", edges_added) dbsession.commit() dbsession.commit() log.info("Added %i edges", edges_added)
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])
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])