def test_project_xyz_vs_geo(): """ ensure that project_onto works the same with xyz vs lat_lon points """ net_coords = {'grid-1': [0.0, 0.0], 'grid-2': [10.0, 10.0]} net_edges = [('grid-1', 'grid-2')] node_coords = {0: [5.0, 5.0]} g_net = GeoGraph(gm.PROJ4_LATLONG, net_coords, data=net_edges) g_nodes = GeoGraph(gm.PROJ4_LATLONG, node_coords) g_project = g_net.project_onto(g_nodes, spherical_accuracy=True) g_net_xyz = GeoGraph(gm.PROJ4_LATLONG, g_net.lon_lat_to_cartesian_coords(), data=net_edges) g_nodes_xyz = GeoGraph(gm.PROJ4_LATLONG, g_nodes.lon_lat_to_cartesian_coords()) g_project_xyz = g_net_xyz.project_onto(g_nodes_xyz, spherical_accuracy=True) g_project_coords_ll = g_project_xyz.cartesian_to_lon_lat() def round_coords(coords, round_precision=8): return {i: tuple(map(lambda c: round(c, round_precision), coord)) for i, coord in coords.items()} assert(round_coords(g_project_coords_ll) == round_coords(g_project.coords))
def nodes_plus_grid(): """ Return: nodes as graph and grid as UnionFind/Rtree combo This example input demonstrates the "more" optimal nature of mod_boruvka vs mod_kruskal. 2(10) | 1(4) | sqrt(5){ /| | / | | / | }3 | } 5 (2)0 | | 1{| | | +-+-3-+-4-+-+-+-+-+-+-+5-+-+-+ <-- existing grid In this case, all nodes will be connected via either algorithm, but the graph produced by mod_kruskal will have edge (4,1) whereas mod_boruvka will produce a graph with edge (0,1). Therefore, the mod_boruvka graph is more optimal. """ mv_max_values = [2, 4, 10] coords = np.array([[0.0, 1.0], [1.0, 3.0], [10.0, 5.0]]) coords_dict = dict(enumerate(coords)) nodes = GeoGraph(gm.PROJ4_FLAT_EARTH, coords=coords_dict) nx.set_node_attributes(nodes, 'budget', dict(enumerate(mv_max_values))) grid_coords = np.array([[-5.0, 0.0], [15.0, 0.0]]) grid = GeoGraph(gm.PROJ4_FLAT_EARTH, {'grid-' + str(n): c for n, c in enumerate(grid_coords)}) nx.set_node_attributes(grid, 'budget', {n: 0 for n in grid.nodes()}) grid.add_edges_from([('grid-0', 'grid-1')]) # now find projections onto grid rtree = grid.get_rtree_index() projected = grid.project_onto(nodes, rtree_index=rtree) projected.remove_nodes_from(grid) projected.remove_nodes_from(nodes) # populate disjoint set of subgraphs subgraphs = UnionFind() # only one connected component, so just associate all nodes # with first node of grid parent = grid.nodes()[0] subgraphs.add_component(parent, budget=grid.node[parent]['budget']) for node in grid.nodes()[1:]: subgraphs.add_component(node, budget=grid.node[node]['budget']) subgraphs.union(parent, node, 0) # and the projected "fake" nodes for node in projected.nodes(): subgraphs.add_component(node, budget=np.inf) subgraphs.union(parent, node, 0) # add projected nodes to node set nodes.add_nodes_from(projected, budget=np.inf) # merge coords nodes.coords = dict(nodes.coords, **projected.coords) return nodes, subgraphs, rtree