【问题标题】:OSMNX KeyError: 'x' when trying to get_nearest_nodes()OSMNX KeyError: 'x' 尝试 get_nearest_nodes()
【发布时间】:2021-04-23 01:47:44
【问题描述】:

我目前有一个流程

  1. 使用 ox.geocode_to_gdf() 下载 Open Street 数据
  2. 使用 gpd.overlay() 获取 Geopackage 边缘和节点,并根据另一张地图编辑边缘和节点
  3. 使用 ox.graph_from_gdfs() 将已编辑的边转换回 OSMNX 作为图形

在这个阶段,我有一个图表(示例here) 我想用来估计一些点之间的最短路径。我有这些点的东向和北向,我试图让他使用最接近这些坐标的节点

nodes_flood = ox.distance.get_nearest_nodes(g_post_200_Cur_centre, Easting, Northing)

以便我可以使用图形和目标节点运行 nx.shortest_path()。但我收到一条错误消息

File "<ipython-input-74-ed9774662a83>", line 1, in <module>
nodes_flood = ox.distance.get_nearest_nodes(g_post_200_Cur_centre, Easting, Northing)

File "/Users/opt/anaconda3/envs/Kinshasa/lib/python3.7/site-packages/osmnx/distance.py", line 256, in get_nearest_nodes
nn = [get_nearest_node(G, (y, x), method="haversine") for x, y in zip(X, Y)]

File "/Users/opt/anaconda3/envs/Kinshasa/lib/python3.7/site-packages/osmnx/distance.py", line 256, in <listcomp>
nn = [get_nearest_node(G, (y, x), method="haversine") for x, y in zip(X, Y)]

File "/Users/opt/anaconda3/envs/Kinshasa/lib/python3.7/site-packages/osmnx/distance.py", line 138, in get_nearest_node
df = pd.DataFrame(coords, columns=["node", "x", "y"]).set_index("node")

File "/Users/opt/anaconda3/envs/Kinshasa/lib/python3.7/site-packages/pandas/core/frame.py", line 563, in __init__
data = list(data)
File "/Users/opt/anaconda3/envs/Kinshasa/lib/python3.7/site-packages/osmnx/distance.py", line 137, in <genexpr>
coords = ((n, d["x"], d["y"]) for n, d in G.nodes(data=True))

KeyError: 'x'

不确定是什么原因造成的。我有 OSMNX v1.0。

【问题讨论】:

标签: python geopandas osmnx


【解决方案1】:

我通过将方法参数传递给 get_nearest_nodes() 来修复错误。如果你选择'kdtree'作为方法,你不会有错误。

nodes_flood = ox.distance.get_nearest_nodes(g_post_200_Cur_centre, Easting, Northing, method='kdtree')

但是,如果我没有传递方法名称,我仍然不知道错误的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2012-09-02
    相关资源
    最近更新 更多