def networker_run_compare(cfg, known_results_file, output_dir): nwk = networker_runner.NetworkerRunner(cfg, output_dir) nwk.validate() nwk.run() # compare this run against existing results test_geo = nio.read_shp_geograph(os.path.join(output_dir, "edges.shp"), simplify=False) known_geo = nio.read_shp_geograph(known_results_file, simplify=False) # compare sets of edges test_edges = get_rounded_edge_sets(test_geo, round_precision=8) known_edges = get_rounded_edge_sets(known_geo, round_precision=8) assert test_edges == known_edges, \ "edges in test do not match known results"
def networkplanner_run_compare(cfg, known_results_file, output_dir): nwk_p = networkplanner_runner.NetworkPlannerRunner(cfg, output_dir) nwk_p.validate() nwk_p.run() # compare this run against existing results test_geo = nio.read_shp_geograph(os.path.join(output_dir, "networks-proposed.shp"), simplify=False) known_geo = nio.read_shp_geograph(known_results_file, simplify=False) # compare sets of edges test_edges = get_rounded_edge_sets(test_geo, round_precision=8) known_edges = get_rounded_edge_sets(known_geo, round_precision=8) assert test_edges == known_edges, \ "edges in test do not match known results"
def test_dataset_store_to_geograph(): """ make sure we can go from shp to geograph to dataset_store """ data_dir = os.path.join("data", "pop_100") test_geo = nio.read_shp_geograph(os.path.join(data_dir, "networks-proposed.shp"), simplify=False) dataset = dataset_store.load(os.path.join(data_dir, "dataset.db")) dataset_geo = networkplanner_runner.dataset_store_to_geograph(dataset) test_edges = test_geo.get_coord_edge_set() dataset_edges = dataset_geo.get_coord_edge_set() assert test_edges == dataset_edges, \ "edges in test do not match dataset results"
def test_line_subgraph_intersection(): """ Test case where precision and odd geometry issues occur """ # initialize network, nodes existing_net_file = os.path.join("data", "katsina", "existing.shp") demand_nodes_file = os.path.join("data", "katsina", "metrics.csv") network = nio.read_shp_geograph(existing_net_file, simplify=False) network.coords = { "g-" + str(n): network.coords[n] for n in network.nodes() } new_labels = ["g-" + str(n) for n in network.nodes()] nx.relabel_nodes(network, dict(zip(network.nodes(), new_labels)), copy=False) nodes = nio.read_csv_geograph(demand_nodes_file, "x", "y") # populate disjoint set of subgraphs subgraphs = UnionFind() # only one connected component, so just add all nodes associated # with first node net_nodes = network.nodes() parent = net_nodes[0] subgraphs.add_component(parent, budget=0) for node in net_nodes[1:]: subgraphs.add_component(node, budget=0) subgraphs.union(parent, node, 0) # now find projections onto grid rtree = network.get_rtree_index() projected = network.project_onto(nodes, rtree_index=rtree) projected.remove_nodes_from(network) assert len(projected.edges()) == 1, "should only be 1 projected edge" edge = projected.edges()[0] p1, p2 = projected.coords[edge[0]], projected.coords[edge[1]] invalid, subgraphs = gm.line_subgraph_intersection(subgraphs, rtree, p1, p2) assert not invalid, "edge should intersect network only once"
def test_line_subgraph_intersection(): """ Test case where precision and odd geometry issues occur """ # initialize network, nodes existing_net_file = os.path.join("data", "katsina", "existing.shp") demand_nodes_file = os.path.join("data", "katsina", "metrics.csv") network = nio.read_shp_geograph(existing_net_file, simplify=False) network.coords = {"g-" + str(n): network.coords[n] for n in network.nodes()} new_labels = ["g-" + str(n) for n in network.nodes()] nx.relabel_nodes(network, dict(zip(network.nodes(), new_labels)), copy=False) nodes = nio.read_csv_geograph(demand_nodes_file, "x", "y") # populate disjoint set of subgraphs subgraphs = UnionFind() # only one connected component, so just add all nodes associated # with first node net_nodes = network.nodes() parent = net_nodes[0] subgraphs.add_component(parent, budget=0) for node in net_nodes[1:]: subgraphs.add_component(node, budget=0) subgraphs.union(parent, node, 0) # now find projections onto grid rtree = network.get_rtree_index() projected = network.project_onto(nodes, rtree_index=rtree) projected.remove_nodes_from(network) assert len(projected.edges()) == 1, "should only be 1 projected edge" edge = projected.edges()[0] p1, p2 = projected.coords[edge[0]], projected.coords[edge[1]] invalid, subgraphs = gm.line_subgraph_intersection(subgraphs, rtree, p1, p2) assert not invalid, "edge should intersect network only once"
# setup log logger = logging.getLogger('networker') logger.info("networker %s (Python %s)" % (networker.__version__, '.'.join(map(str, sys.version_info[:3])))) parser = argparse.ArgumentParser(description="Clean network and report it") parser.add_argument("network_filename", help="network to be cleaned") parser.add_argument("--output_directory", "-o", \ default=".", \ help="directory where network output files will be written") args = parser.parse_args() net = nio.read_shp_geograph(args.network_filename, simplify=False) num_found = 0 for zero_len_edge in net.find_zero_len_edges(): node0 = zero_len_edge[0] node1 = zero_len_edge[1] num_found += 1 logger.warn("found zero length edge ({},{}), with coords ({},{})".\ format(node0, node1, net.coords[node0], net.coords[node1])) net.remove_edge(node0, node1) logger.info("found {} total zero length edges".format(num_found)) #TODO: Additional checks?
dest="spherical_accuracy", action="store_true", default=False, help="connect nodes to edges as though on a sphere (ignored unprojected inputs)") parser.add_argument("--output_directory", "-o", \ default=".", \ help="directory where all output files will be written") args = parser.parse_args() nodes = nio.read_csv_geograph(args.node_filename, args.x_column, args.y_column) net = None if len(args.network_filename) > 5 and args.network_filename[-5:] == '.json': net = nio.read_json_geograph(args.network_filename) else: net = nio.read_shp_geograph(args.network_filename, simplify=False) # relabel nodes/coords so that merge can work prefix = "net-" nx.relabel_nodes(net, {n: prefix + str(n) for n in net.nodes()}, copy=False) net.coords = {prefix + str(n): c for n, c in net.coords.items()} def project_helper(use_rtree, spherical_accuracy): if use_rtree: logger.info("building rtree...") rtree = net.get_rtree_index() logger.info("projecting nodes...") return net.project_onto(nodes, rtree_index=rtree, spherical_accuracy=spherical_accuracy)
"-n", type=int, default=100, help="size of nodes in pixels") parser.add_argument("--output_file", "-o", \ default="network.png", \ help="file to write image to") args = parser.parse_args() nets = [] for net_file in args.network_files: if len(net_file) > 5 and net_file[-5:] == '.json': nets.append(nio.read_json_geograph(net_file)) else: nets.append(nio.read_shp_geograph(net_file, simplify=False)) # do the rendering and save to file import matplotlib matplotlib.use('Agg') from matplotlib import pyplot as plt plt.figure(figsize=(8, 8)) for net in nets: utils.draw_geograph(net, node_size=args.node_size) # don't render axes cur_axes = plt.gca() cur_axes.set_frame_on(False) cur_axes.axes.get_xaxis().set_visible(False) cur_axes.axes.get_yaxis().set_visible(False)
parser.add_argument("network_files", metavar="NETWORK_FILE", type=str, nargs="+", help="network to be rendered (.shp OR .json)") parser.add_argument("--node_size", "-n", type=int, default=100, help="size of nodes in pixels") parser.add_argument("--output_file", "-o", \ default="network.png", \ help="file to write image to") args = parser.parse_args() nets = [] for net_file in args.network_files: if len(net_file) > 5 and net_file[-5:] == '.json': nets.append(nio.read_json_geograph(net_file)) else: nets.append(nio.read_shp_geograph(net_file, simplify=False)) # do the rendering and save to file import matplotlib matplotlib.use('Agg') from matplotlib import pyplot as plt plt.figure(figsize=(8,8)) for net in nets: utils.draw_geograph(net, node_size=args.node_size) # don't render axes cur_axes = plt.gca() cur_axes.set_frame_on(False) cur_axes.axes.get_xaxis().set_visible(False) cur_axes.axes.get_yaxis().set_visible(False)