You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OSMnx经consolidate_intersections处理后的边唯一不变标识符咨询

OSMnx合并路口后是否存在恒定唯一的边标识符?

我希望了解OSMnx路网的有向(MultiGraph)和无向(MultiDiGraph)表示在两类场景下的特性:

  1. 查找最近边
  2. 以可复现的方式将路网及其属性写入磁盘

我的分析流程如下:

  1. 调用osmnx.simplification.consolidate_intersections合并路口,部分边的OSM way ID会被合并
  2. 随后我将图转换为GeoDataFrames并开展进一步预处理
  3. 之后我使用osmnx.distance.nearest_edges将观测点匹配到边(即街道),该步骤需要将GeoDataFrames转回图结构

我在全分析流程中使用最近边的*(u, v, key)值关联观测数据与街道段几何信息,但GeoDataFrames与networkx图互相转换的过程不会保留边索引的(u, v, k)*顺序。

请问OSMnx图经过osmnx.simplification.consolidate_intersections处理后,是否存在唯一且恒定不变的边标识符?


编辑补充

应要求,下方给出简短示例,演示通过GeoDataFrames将图读写到磁盘时,*[u, v, key]集合会发生变化。从相关GitHub issue以及networkx官方文档可知,我此前认为[u, v, key]*不会变化的假设不成立。我目前已通过调整流程绕过该问题:更多预处理工作直接使用networkx.MultiGraph格式的路网完成,仅在流程末尾转换为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.

内容的提问来源于stack exchange,提问作者Timothy W. Hilton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 14:36:04