我如何解决问题发生在osmnx plot_graph_route()keyError:'长度'

发布于 2025-02-07 17:26:40 字数 1231 浏览 2 评论 0 原文

我目前正在开发一种用于包裹交付的最佳交付算法。我有一个问题,因为在开发程序时,我遇到了OSMNX的plot_graph_route()函数的问题。 当我像下面的代码一样编码时,我将获得KeyError的结果:“长度”。

这是错误代码。

"C:\Python38\lib\site-packages\osmnx\plot.py", line 303, in plot_graph_route
    data = min(G.get_edge_data(u, v).values(), key=lambda d: d["length"])
  File "C:\Python38\lib\site-packages\osmnx\plot.py", line 303, in <lambda>
    data = min(G.get_edge_data(u, v).values(), key=lambda d: d["length"])
KeyError: 'length'

由于我的测试,假定以下列表A中的两个节点之间发生错误。连接两个节点,即使我任意在两个节点之间添加了一个边缘,也会发生错误。节点之间的路径是通过使用NetWokx的Shortest_path_length()函数获得的结果。

这是我的代码

import networkx as nx
import osmnx as ox
import requests
import numpy as np

a= [1916534315,1916534308] #list_of_problem_node
G = ox.graph_from_place('Gumi, Gyeongsangbuk-do, Korea', network_type='drive')#graph of I want to find 

########## for save and load convinience
# ox.save_graphml(G,'gumimap')
# G = ox.load_graphml('gumimap')

G.add_edge(1916534315,1916534308)
G.add_edge(1916534308,1916534315)
G=ox.utils_graph.get_largest_component(G,strongly=True)

path=nx.shortest_path_length(G,a[0],a[1])
fig1_1, ax = ox.plot_graph_route(G, path, route_linewidth=6, node_size=0.5)

谢谢

I am currently developing an optimal delivery algorithm for parcel delivery. I have a question because I encountered a problem with the plot_graph_route() function of OSMNX while developing the program.
When I coded like the code below, I get a result of KeyError: 'length'.

Here is the error code.

"C:\Python38\lib\site-packages\osmnx\plot.py", line 303, in plot_graph_route
    data = min(G.get_edge_data(u, v).values(), key=lambda d: d["length"])
  File "C:\Python38\lib\site-packages\osmnx\plot.py", line 303, in <lambda>
    data = min(G.get_edge_data(u, v).values(), key=lambda d: d["length"])
KeyError: 'length'

As a result of my testing, it is assumed that an error occurs between the two nodes in List A below. Both nodes are connected, and an error occurs even though I arbitrarily added an edge between the two nodes. The path between the nodes is the result obtained by using the netwokX's shortest_path_length() function.

this is my code

import networkx as nx
import osmnx as ox
import requests
import numpy as np

a= [1916534315,1916534308] #list_of_problem_node
G = ox.graph_from_place('Gumi, Gyeongsangbuk-do, Korea', network_type='drive')#graph of I want to find 

########## for save and load convinience
# ox.save_graphml(G,'gumimap')
# G = ox.load_graphml('gumimap')

G.add_edge(1916534315,1916534308)
G.add_edge(1916534308,1916534315)
G=ox.utils_graph.get_largest_component(G,strongly=True)

path=nx.shortest_path_length(G,a[0],a[1])
fig1_1, ax = ox.plot_graph_route(G, path, route_linewidth=6, node_size=0.5)

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你又不是我 2025-02-14 17:26:40

正如您的错误所暗示的那样,您的新边缘缺乏长度属性, plot_graph_route 函数需要。您可以手动添加它或使用 add_edge_lengths 函数。

As your error suggests, your new edges lack a length attribute, which the plot_graph_route function needs. You can add it manually or use the add_edge_lengths function. https://osmnx.readthedocs.io/en/stable/osmnx.html#osmnx.distance.add_edge_lengths

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文