예제 #1
0
def test_feed_to_graph_plot():
    # Create a simple graph
    G = nx.Graph(crs={'init': 'epsg:4326', 'no_defs': True})

    # And add two nodes to it
    G.add_node('a', x=-122.2729918, y=37.7688136)
    G.add_node('b', x=-122.2711039, y=37.7660709)

    # Now, reproject the graph to the default 2163 epsg
    G2 = reproject(G)

    # Extract the x and y values of the reproject nodes
    xs = []
    ys = []
    for i, node in G2.nodes(data=True):
        xs.append(node['x'])
        ys.append(node['y'])

    # TODO: Can we improve how this is assessed? Seems like G.nodes()
    #       does not return nodes in same order in Py 3.6 as in 3.5
    expected_xs = [-1932968.345, -1932884.818]
    for x in xs:
        a = (x == pytest.approx(expected_xs[0], abs=0.01))
        b = (x == pytest.approx(expected_xs[1], abs=0.01))
        assert (a or b)

    expected_ys = [-543020.339, -543361.855]
    for y in ys:
        a = (y == pytest.approx(expected_ys[0], abs=0.01))
        b = (y == pytest.approx(expected_ys[1], abs=0.01))
        assert (a or b)
예제 #2
0
def test_coalesce_operation():
    # Create a simple graph
    G = nx.MultiDiGraph(crs={'init': 'epsg:4326', 'no_defs': True}, name='foo')

    # And add two nodes to it
    G.add_node('a', x=-122.2729918, y=37.7688136, boarding_cost=10)
    G.add_node('b', x=-122.2711039, y=37.7660709, boarding_cost=15)
    G.add_node('c', x=-122.2711038, y=37.7660708, boarding_cost=12)
    G.add_edge('a', 'b', length=10, mode='transit')
    G.add_edge('b', 'c', length=1, mode='walk')

    # Also add a node, and edge that is a more expensive variant
    # of effectively the same edge to make sure this more expensive edge
    # gets tossed during the coalesce
    G.add_node('b_alt', x=-122.2711039, y=37.7660709, boarding_cost=13.5)
    G.add_edge('a', 'b_alt', length=100, mode='transit')

    # Also add a second edge between the same nodes, but with an equal weight
    G.add_edge('a', 'b_alt', length=100, mode='transit')

    # Add a node that won't be preserved (no edges connected to it)
    G.add_edge('a', 'b_alt', length=10, mode='transit')

    G2 = reproject(G)
    G2c = coalesce(G2, 200)

    # Same akward situation as before, where edges are returned in
    # different order between Py 3.5 and 3.6
    for i, node in G2c.nodes(data=True):
        a = _dict_equal(node, {
            'x': -1933000,
            'y': -543000,
            'boarding_cost': 10.0
        })
        b = _dict_equal(node, {
            'x': -1932800,
            'y': -543400,
            'boarding_cost': 13.5
        })
        assert (a or b)

    all_edges = list(G2c.edges(data=True))
    assert len(all_edges) == 1

    # Make sure that the one edge came out as expected
    assert _dict_equal(all_edges[0][2], {'length': 10, 'mode': 'transit'})
예제 #3
0
def test_coalesce_operation():
    def method_a(x):
        return x.mean()

    def method_b(x):
        return x.max()

    for method, val in [
        (method_a, 55),
        (method_b, 100),
    ]:
        # Create a simple graph
        G = nx.MultiDiGraph(crs={
            'init': 'epsg:4326',
            'no_defs': True
        },
                            name='foo')

        # And add two nodes to it
        G.add_node('a',
                   x=-122.2729918,
                   y=37.7688136,
                   modes=['3'],
                   boarding_cost=10)
        G.add_node('b',
                   x=-122.2711039,
                   y=37.7660709,
                   modes=['3'],
                   boarding_cost=15)
        G.add_node('c',
                   x=-122.2711038,
                   y=37.7660708,
                   modes=['1', '3'],
                   boarding_cost=12)

        G.add_edge('a', 'b', length=10, mode='transit')
        G.add_edge('b', 'c', length=1, mode='walk')

        # Also add a node, and edge that is a more expensive variant
        # of effectively the same edge
        G.add_node('b_alt',
                   x=-122.2711039,
                   y=37.7660709,
                   modes=['3'],
                   boarding_cost=13.5)

        G.add_edge('a', 'b_alt', length=100, mode='transit')

        # Also add a second edge between the same nodes, but with
        # smaller weight so, it will get tossed in the cleanup
        G.add_edge('a', 'b_alt', length=50, mode='transit')

        G2 = reproject(G)
        G2c = coalesce(G2, 200, edge_summary_method=method)

        # Same akward situation as before, where edges are returned in
        # different order between Py 3.5 and 3.6
        for _, node in G2c.nodes(data=True):
            ref_node_a = {
                'x': -1933000,
                'y': -543000,
                'modes': ['3'],
                'boarding_cost': 10.0
            }
            a = _dict_equal(node, ref_node_a)

            ref_node_b = {
                'x': -1932800,
                'y': -543400,
                'modes': ['1', '3'],
                'boarding_cost': 13.5
            }
            b = _dict_equal(node, ref_node_b)

            assert (a or b)

        all_edges = list(G2c.edges(data=True))
        assert len(all_edges) == 1

        # Make sure that the one edge came out as expected
        assert _dict_equal(all_edges[0][2], {'length': val, 'mode': 'transit'})