微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

osmnx:g.node 已弃用,但 g.nodes 导致错误

如何解决osmnx:g.node 已弃用,但 g.nodes 导致错误

我正在尝试运行此示例代码。它说“AttributeError: 'MultiDiGraph' object has no attribute 'node'”,我发现 g.node 已被弃用并替换为 g.nodes。但是如果我只是将其更改为 g.nodes,则会出现另一个错误

    Traceback (most recent call last):
  File "/home/anderledani/PycharmProjects/pythonProject1/snaptoroadtest.py",line 12,in <module>
    nn = min((u,v),key=lambda n: ox.distance.great_circle_vec(lat,lng,G.nodes[n]['y'],G.nodes[n]['x']))
  File "/home/anderledani/PycharmProjects/pythonProject1/snaptoroadtest.py",in <lambda>
    nn = min((u,G.nodes[n]['x']))
  File "/home/anderledani/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/networkx/classes/reportviews.py",line 187,in __getitem__
    return self._nodes[n]
KeyError: 0

我试图运行的代码

import matplotlib.pyplot as plt
import osmnx as ox
ox.config(log_console=True,use_cache=True)

# get graph and define a reference point
G = ox.graph_from_point((13.743942,100.570006),network_type='drive',dist=350,simplify=True)
lat = 13.744001
lng = 100.570457

# get nearest node incident to nearest edge to reference point
geom,u,v = ox.distance.get_nearest_edge(G,(lat,lng))
nn = min((u,G.nodes[n]['x']))

# plot the reference point and this nearest node
fig,ax = ox.plot_graph(G,node_color='#999999',show=False,close=False)
ax.scatter(lng,lat,c='r',marker='x')
ax.scatter(G.nodes[nn]['x'],G.nodes[nn]['y'],s=50,zorder=2)
plt.show()

解决方法

osmnx 的文档中,get_nearest_edge 方法被标记为已弃用。

但是,我认为您可以直接获得所需的变量 nn - 我猜它是最近的节点 - 使用:nearest_nodes:

nn = ox.distance.nearest_nodes(G,lat,lng)

如果你想要离最近边最近的节点,更新到新方法nearest_edges

(u,v,key) = nearest_edges(G,lng)

包含提问者代码的第二个答案:How to find nearest node along nearest edge?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。