我想知道关于有向(MultiGraph)和无向(MultiDiGraph)的OSMnx网络表示(1)找到最近的边缘和(2)以可复制的方式将网络及其属性写入磁盘。
在整个分析过程中,我使用最近边的(u,v,key)交叉参考观测和街道段几何学。从geodataframes到networkx图的来回转换并不保持边缘索引的(u,v,k)顺序。
在一个osmnx.simplification.consolidate_intersections?图经过OSMnx图之后,是否有唯一且不变的边标识符?
编辑:根据要求,下面是一个简短的例子,说明u,v,key设置从GeoDataFrames到磁盘的图形读写过程中的变化。从这个github问题和networkx文档看,我认为u,v,key不会改变的假设是站不住脚的。为此,我做了更多的预处理,使用网络的networkx.MultiGraph表示,而不是GeoDataFrame表示,并且只在管道的末尾转换为GeoDataFrames并写入磁盘一次。
import osmnx as ox
import geopandas as gpd
TRANSVERSE_MERCATOR_NZ = 'EPSG:2193'
fname_gpkg = '/tmp/test.gpkg'
fname_graphml = '/tmp/test.graphml'
# 1) create an OSMnx graph for Auckland, New Zealand. Reproject,
# consolidate intersections, convert to undirected. Do some analyses:
# assigning the "road_class" variable serves here as a placeholder.
# Save to geopackage.
g = ox.graph_from_place(
['NZ-AUK'], network_type="drive", retain_all=True
)
# reproject, consolidate, undirect
gp = ox.projection.project_graph(g, TRANSVERSE_MERCATOR_NZ)
g_simplified = ox.simplification.consolidate_intersections(gp, tolerance=30)
g_simplified_undirected = ox.utils_graph.get_undirected(g_simplified)
# get geodataframes and add a column to edges
gdf_nodes_0, gdf_edges_0 = ox.utils_graph.graph_to_gdfs(g_simplified_undirected)
gdf_edges_0['road_class'] = 1 # placeholder for more complicated stuff
# convert back to graph and save to geopackage
g_from_frames = ox.utils_graph.graph_from_gdfs(gdf_nodes_0, gdf_edges_0)
g_for_output = ox.utils_graph.get_undirected(g_from_frames)
ox.io.save_graph_geopackage(g_for_output, fname_gpkg)
# 2) load the saved geopackage back to GeoDataFrames, demonstrate that
# the u, v, k values have changed.
gdf_nodes_1 = gpd.read_file(fname_gpkg,
layer='nodes').set_index('osmid')
gdf_edges_1 = gpd.read_file(fname_gpkg,
layer='edges').set_index(['u', 'v', 'key'])
# show that the network saved to the geopackage and the network loaded
# from the geopackage have edges with different u, v, w indices
assert gdf_nodes_1.index.is_unique and gdf_edges_1.index.is_unique
graph_attrs = {'crs': 'epsg:2193', 'simplified': True}
idx_0 = gdf_edges_0.reset_index()[['u', 'v', 'key']]
idx_1 = gdf_edges_1.reset_index()[['u', 'v', 'key']]
only_0 = idx_0.merge(idx_1, how='outer', indicator=True).loc[lambda x: x['_merge'] == 'left_only']
only_1 = idx_0.merge(idx_1, how='outer', indicator=True).loc[lambda x: x['_merge'] == 'right_only']
# only_0 contains [u, v, key] sets that are in gdf_edges_0 but not gdf_edges_1.
# only_1 contains [u, v, key] sets that are in gdf_edges_1 but not gdf_edges_0.发布于 2021-11-17 17:46:23
在一个osmnx.simplification.consolidate_intersections?图经过OSMnx图之后,是否有唯一且不变的边标识符?
边在合并时发生变化,因为节点在合并时会发生变化。边由端点节点加上键(元组(u, v, k) )唯一标识。因此,当节点发生变化时,边的唯一标识符就会发生变化。但是,您可以获取边缘的唯一标识符(u, v, k),在统一图中查找相应的节点,并检查它们的osmid_original属性:
# download, project, consolidate graph
G = ox.graph_from_place('Piedmont, CA, USA', network_type='drive')
Gp = ox.project_graph(G)
Gc = ox.consolidate_intersections(Gp)
# select the first edge
u, v, k = list(Gc.edges)[0]
# inspect this edge's attributes
print(Gc.edges[(u, v, k)])
# what was the original OSM ID of this edge's u and v nodes?
u_original = Gc.nodes[u]['osmid_original']
v_original = Gc.nodes[v]['osmid_original']
# inspect the original edge's attributes
print(G.edges[(u_original, v_original, k)])因此,边是由它们的节点唯一标识的,并且合并图中的节点有一个属性将它们链接回原来的OSM in。但是,这并不总是有效的,因为合并图中的一些单独的节点(根据定义)是由原始图中的多个节点组成的,这使得查找变得不可能。为了更好的理论对应,整合固有地减少了信息。
https://stackoverflow.com/questions/69907902
复制相似问题