使用 osmnx 绘制路线

发布于 2025-01-16 01:22:10 字数 701 浏览 0 评论 0原文

我正在尝试开发一个代码来使用 osnmx 在地图中绘制路线。这就是我所拥有的:

route=[292257954, 429200554, 1187086228, 1179868692, 430284828]
fig, ax = ox.plot.plot_graph_route(G_map, route, route_color='r', route_linewidth=4, route_alpha=0.5, orig_dest_size=100, ax=None)

在路由列表中,我将节点写为 OSM 的 ID。但我收到此错误:

    fig, ax = ox.plot.plot_graph_route(G_map, route, route_color='r', route_linewidth=4, route_alpha=0.5, orig_dest_size=100, ax=None)

  File ~\anaconda3\envs\ox\lib\site-packages\osmnx\plot.py:303 in plot_graph_route
    data = min(G.get_edge_data(u, v).values(), key=lambda d: d["length"])

AttributeError: 'NoneType' object has no attribute 'values'

有人可以帮助我吗?

I'm trying to develop a code to plot a route in a map with osnmx. This is what I have:

route=[292257954, 429200554, 1187086228, 1179868692, 430284828]
fig, ax = ox.plot.plot_graph_route(G_map, route, route_color='r', route_linewidth=4, route_alpha=0.5, orig_dest_size=100, ax=None)

In the route list, I have the nodes written as IDs of OSM. But i get this error:

    fig, ax = ox.plot.plot_graph_route(G_map, route, route_color='r', route_linewidth=4, route_alpha=0.5, orig_dest_size=100, ax=None)

  File ~\anaconda3\envs\ox\lib\site-packages\osmnx\plot.py:303 in plot_graph_route
    data = min(G.get_edge_data(u, v).values(), key=lambda d: d["length"])

AttributeError: 'NoneType' object has no attribute 'values'

Could anyone help me?

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

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

发布评论

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

评论(1

陪我终i 2025-01-23 01:22:10
  • 通过在立交桥中查找OSMID来重建图,
  • 显然这不是一条有效的路线,并且在重建图
[
  "No path between 292257954 and 429200554.",
  "No path between 429200554 and 1187086228.",
  "Either source 1187086228 or target 1179868692 is not in G",
  "Either source 1179868692 or target 430284828 is not in G"
]

完整代码后其中一个节点不存在

import requests
import pandas as pd
import shapely
import osmnx as ox
import networkx as nx
import json

route = [292257954, 429200554, 1187086228, 1179868692, 430284828]

# get lat/lon of osmids
df = pd.DataFrame(
    requests.get(
        f"http://overpass-api.de/api/interpreter?data=[out:json]; node(id:{','.join([str(n) for n in route])});out;"
    ).json()["elements"]
)

# get graph bound by nodes in list
poly = shapely.geometry.MultiPoint(df.loc[:, ["lon", "lat"]].values).convex_hull
G_map = ox.graph_from_polygon(poly, network_type="drive")

rr = []
err = []
for a,b in zip(route, route[1:]):
    try:
        r = nx.shortest_path(G_map, a, b, weight='length')
        rr.append(r)
    except nx.NetworkXNoPath as e:
        err.append(str(e))
    except nx.NodeNotFound as e:
        err.append(str(e))

print(json.dumps(err, indent=2))
        
if len(rr)>0:
    fig, ax = ox.plot.plot_graph_route(
        G_map,
        rr,
        route_color="r",
        route_linewidth=4,
        route_alpha=0.5,
        orig_dest_size=100,
        ax=None,
    )

  • have reconstructed graph by looking up OSMID in overpass
  • clearly this is not a valid route, also one of the nodes does not exist after reconstructing graph
[
  "No path between 292257954 and 429200554.",
  "No path between 429200554 and 1187086228.",
  "Either source 1187086228 or target 1179868692 is not in G",
  "Either source 1179868692 or target 430284828 is not in G"
]

full code

import requests
import pandas as pd
import shapely
import osmnx as ox
import networkx as nx
import json

route = [292257954, 429200554, 1187086228, 1179868692, 430284828]

# get lat/lon of osmids
df = pd.DataFrame(
    requests.get(
        f"http://overpass-api.de/api/interpreter?data=[out:json]; node(id:{','.join([str(n) for n in route])});out;"
    ).json()["elements"]
)

# get graph bound by nodes in list
poly = shapely.geometry.MultiPoint(df.loc[:, ["lon", "lat"]].values).convex_hull
G_map = ox.graph_from_polygon(poly, network_type="drive")

rr = []
err = []
for a,b in zip(route, route[1:]):
    try:
        r = nx.shortest_path(G_map, a, b, weight='length')
        rr.append(r)
    except nx.NetworkXNoPath as e:
        err.append(str(e))
    except nx.NodeNotFound as e:
        err.append(str(e))

print(json.dumps(err, indent=2))
        
if len(rr)>0:
    fig, ax = ox.plot.plot_graph_route(
        G_map,
        rr,
        route_color="r",
        route_linewidth=4,
        route_alpha=0.5,
        orig_dest_size=100,
        ax=None,
    )

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