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

如何获取OSMnx路网中任意边的中点经纬度坐标

获取OSMnx路网边中心点经纬度实现方案

你构建的圣迭戈县驾车路网默认采用EPSG:4326坐标系,节点属性中y对应纬度值、x对应经度值,按以下方法操作即可得到任意边的中心点经纬度:

  • 首先将路网转换为GeoDataFrame格式,该步骤会自动补全所有边的几何字段,避免直连边缺失路径几何信息
  • 针对边的线几何计算中心点:如果需要几何质心直接调用线要素的centroid属性即可;如果需要沿边路径长度一半位置的线性中点(更贴合路网实际路径的中心位置),调用线要素的interpolate方法按归一化长度取点即可
  • 最终从中心点几何中提取y属性为纬度、x属性为经度

完整可运行代码如下:

import osmnx as ox

# 原有路网构建代码
G = ox.graph_from_place('San Diego County, CA', network_type='drive')

# 转换为节点、边GeoDataFrame,自动补全边几何信息
nodes, edges = ox.graph_to_gdfs(G, nodes=True, edges=True)

# 批量计算所有边的沿路径线性中点,要几何质心替换为edges['geometry'].centroid即可
edges['center_point'] = edges['geometry'].apply(lambda line: line.interpolate(0.5, normalized=True))
# 拆分经纬度字段
edges['center_lat'] = edges['center_point'].y
edges['center_lon'] = edges['center_point'].x

# 取单条边的中心点示例
sample_edge = edges.iloc[0]
sample_lat = sample_edge['center_point'].y
sample_lon = sample_edge['center_point'].x
print(f"样例边中心点坐标:纬度 {sample_lat},经度 {sample_lon}")

注意:如果你之前为了计算距离调用过ox.project_graph将路网投影到米制坐标系,需要先将边GeoDataFrame通过ox.project_gdf(edges, to_crs='EPSG:4326')转回WGS84经纬度坐标系再计算,否则得到的坐标不是通用经纬度值。

内容的提问来源于stack exchange,提问作者faarabi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 23:48:39