def get_data(G, district_name): # print(nx.info(G)) print(f'File Path = {PATH}') # save as graphml path = f'{PATH}/{district_name}.xml' ox.save_graphml(G, path) # save as .osm path = f'{PATH}/{district_name}.osm' ox.config(all_oneway=True) if not os.path.exists(path): ox.save_graph_xml(G, filepath=path) # save as folium html path = f'{PATH}/{district_name}_folium.html' if not os.path.exists(path): map_folium = ox.folium.plot_graph_folium(G) map_folium.save(path) # save as SVG path = f'{PATH}/{district_name}_image.svg' fig, ax = ox.plot_graph(G, show=False, save=True, close=True, filepath=path) # save graph as a shapefile and .csv path = f'{PATH}/{district_name}_shape' ox.save_graph_shapefile(G, filepath=path) make_adjacency_matrix(district_name) clean_csv(district_name) make_adjacency_required_matrix(district_name)
def get_data_drone(G, district_name): # Graph for Drones district_name += '_drone' R = 6371e3 nodes = list(G.nodes.data()) for i in range(len(nodes)): for j in range(len(nodes)): if i == j or (G.has_edge(nodes[i][1]['osmid'], nodes[j][1]['osmid'])): continue else: lon1 = float(nodes[i][1]['x'] * np.pi / 180) lat1 = float(nodes[i][1]['y'] * np.pi / 180) lon2 = float(nodes[j][1]['x'] * np.pi / 180) lat2 = float(nodes[j][1]['y'] * np.pi / 180) d_lat = lat2 - lat1 d_lon = lon2 - lon1 a = np.sin(d_lat/2) ** 2 + np.cos(lat1) * \ np.cos(lat2) * np.sin(d_lon/2) ** 2 c = 2 * np.arctan2(a**0.5, (1 - a)**0.5) d = R * c d = round(d, 3) G.add_edge(nodes[i][1]['osmid'], nodes[j][1]['osmid'], length=d) # save as graphml path = f'{PATH}/{district_name}.xml' ox.save_graphml(G, path) # save as .osm path = f'{PATH}/{district_name}.osm' ox.config(all_oneway=True) if not os.path.exists(path): ox.save_graph_xml(G, filepath=path) # save as folium html path = f'{PATH}/{district_name}_folium.html' if not os.path.exists(path): map_folium = ox.folium.plot_graph_folium(G) map_folium.save(path) # save as SVG path = f'{PATH}/{district_name}_image.svg' fig, ax = ox.plot_graph(G, show=False, save=True, close=True, filepath=path)
def test_network_saving_loading(): # save graph as shapefile and geopackage G = ox.graph_from_place(place1, network_type="drive") ox.save_graph_shapefile(G) ox.save_graph_geopackage(G) # save/load graph as graphml file ox.save_graphml(G, gephi=True) ox.save_graphml(G, gephi=False) filepath = os.path.join(ox.settings.data_folder, "graph.graphml") G = ox.load_graphml(filepath, node_type=str) # test osm xml output default_all_oneway = ox.settings.all_oneway ox.settings.all_oneway = True G = ox.graph_from_point(location_point, dist=500, network_type="drive") ox.save_graph_xml(G, merge_edges=False) # test osm xml output merge edges ox.save_graph_xml(G, merge_edges=True, edge_tag_aggs=[("length", "sum")]) # test osm xml output from gdfs nodes, edges = ox.graph_to_gdfs(G) ox.save_graph_xml([nodes, edges]) # test ordered nodes from way df = pd.DataFrame({ "u": [54, 2, 5, 3, 10, 19, 20], "v": [76, 3, 8, 10, 5, 20, 15] }) ordered_nodes = ox.io._get_unique_nodes_ordered_from_way(df) assert ordered_nodes == [2, 3, 10, 5, 8] ox.settings.all_oneway = default_all_oneway
def test_network_saving_loading(): # save graph as shapefile and geopackage G = ox.graph_from_place(place1, network_type="drive") ox.save_graph_shapefile(G) ox.save_graph_geopackage(G) # save/load graph as graphml file ox.save_graphml(G, gephi=True) ox.save_graphml(G, gephi=False) filepath = os.path.join(ox.settings.data_folder, "graph.graphml") G2 = ox.load_graphml(filepath) # verify everything in G is equivalent in G2 for (n1, d1), (n2, d2) in zip(G.nodes(data=True), G2.nodes(data=True)): assert n1 == n2 assert d1 == d2 for (u1, v1, k1, d1), (u2, v2, k2, d2) in zip(G.edges(keys=True, data=True), G2.edges(keys=True, data=True)): assert u1 == u2 assert v1 == v2 assert k1 == k2 assert tuple(d1.keys()) == tuple(d2.keys()) assert tuple(d1.values()) == tuple(d2.values()) for (k1, v1), (k2, v2) in zip(G.graph.items(), G2.graph.items()): assert k1 == k2 assert v1 == v2 assert tuple(G.graph["streets_per_node"].keys()) == tuple( G2.graph["streets_per_node"].keys()) assert tuple(G.graph["streets_per_node"].values()) == tuple( G2.graph["streets_per_node"].values()) # test custom data types nd = {"osmid": str} ed = {"length": str, "osmid": float} G2 = ox.load_graphml(filepath, node_dtypes=nd, edge_dtypes=ed) # test osm xml output default_all_oneway = ox.settings.all_oneway ox.settings.all_oneway = True G = ox.graph_from_point(location_point, dist=500, network_type="drive") ox.save_graph_xml(G, merge_edges=False) # test osm xml output merge edges ox.save_graph_xml(G, merge_edges=True, edge_tag_aggs=[("length", "sum")]) # test osm xml output from gdfs nodes, edges = ox.graph_to_gdfs(G) ox.save_graph_xml([nodes, edges]) # test ordered nodes from way df = pd.DataFrame({ "u": [54, 2, 5, 3, 10, 19, 20], "v": [76, 3, 8, 10, 5, 20, 15] }) ordered_nodes = ox.io._get_unique_nodes_ordered_from_way(df) assert ordered_nodes == [2, 3, 10, 5, 8] ox.settings.all_oneway = default_all_oneway
def test_osm_xml(): # test loading a graph from a local .osm xml file node_id = 53098262 neighbor_ids = 53092170, 53060438, 53027353, 667744075 with bz2.BZ2File("tests/input_data/West-Oakland.osm.bz2") as f: handle, temp_filename = tempfile.mkstemp(suffix=".osm") os.write(handle, f.read()) os.close(handle) for filename in ("tests/input_data/West-Oakland.osm.bz2", temp_filename): G = ox.graph_from_xml(filename) assert node_id in G.nodes for neighbor_id in neighbor_ids: edge_key = (node_id, neighbor_id, 0) assert neighbor_id in G.nodes assert edge_key in G.edges assert G.edges[edge_key]["name"] in ("8th Street", "Willow Street") os.remove(temp_filename) # test .osm xml saving default_all_oneway = ox.settings.all_oneway ox.settings.all_oneway = True G = ox.graph_from_point(location_point, dist=500, network_type="drive") ox.save_graph_xml(G, merge_edges=False) # test osm xml output merge edges ox.save_graph_xml(G, merge_edges=True, edge_tag_aggs=[("length", "sum")]) # test osm xml output from gdfs nodes, edges = ox.graph_to_gdfs(G) ox.save_graph_xml([nodes, edges]) # test ordered nodes from way df = pd.DataFrame({"u": [54, 2, 5, 3, 10, 19, 20], "v": [76, 3, 8, 10, 5, 20, 15]}) ordered_nodes = ox.osm_xml._get_unique_nodes_ordered_from_way(df) assert ordered_nodes == [2, 3, 10, 5, 8] ox.settings.all_oneway = default_all_oneway