在python中为osm图边缘着色

发布于 2025-01-14 05:23:49 字数 644 浏览 6 评论 0原文

我正在尝试制作一张地图,其中某条路线(例如路线中包含的边缘)是彩色的。现在,我有以下代码:

node_route=[1223724841, 1688813022, 2464466873, 1688813022, 1688813022, 1688813022, 1688813022]
ec = ['w' for u, v, k in G_map.edges(keys=True)] 
for i in range(len(node_route)-1):
    ec = ['r' for u, v, k in G_map.edges(keys=True) if (u==node_route[i] and v==node_route[i+1])]
    
fig, ax = ox.plot_graph(G_map, node_color='r', node_edgecolor='k', node_size=40, node_zorder=3, edge_color=ec, edge_linewidth=1)  

列表node_rote是我想要着色的有序路线。我首先将“w”分配给图表(G_map)中的每个边。然后我检查 node_route 列表中的每一对(索引 i 和 i+1),如果该对等于图形边缘的“u”和“v”,则它将用“r”着色。问题是这段代码返回一个空列表。

感谢您的帮助!

I'm trying to make a map where a certain route, e.g. the edges included in the route, is colored. For now, I have the following code:

node_route=[1223724841, 1688813022, 2464466873, 1688813022, 1688813022, 1688813022, 1688813022]
ec = ['w' for u, v, k in G_map.edges(keys=True)] 
for i in range(len(node_route)-1):
    ec = ['r' for u, v, k in G_map.edges(keys=True) if (u==node_route[i] and v==node_route[i+1])]
    
fig, ax = ox.plot_graph(G_map, node_color='r', node_edgecolor='k', node_size=40, node_zorder=3, edge_color=ec, edge_linewidth=1)  

The list node_rote is the ordered route i would like to color. I start by assigning 'w' to every edge in my graph (G_map). Then i check every pair in my node_route list (index i and i+1) and if the pair is equal to 'u' and 'v' of the graph edges it will be colored with 'r'. The problem is this code returns an empty list.

Thanks for the help!

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

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

发布评论

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

评论(1

千纸鹤带着心事 2025-01-21 05:23:49

对于初学者来说,每次循环时都会重新分配 ec ,并丢弃之前的值。

如果我正确理解您要做什么,您最好构建一个字典,将节点 id:s 映射到它们在 node_route 中的位置。然后循环遍历G_map.edges(...),在node_routeuv节点的位置code> 使用字典,并根据 pos[u]+1 == pos[v] 是否将“r”或“w”附加到 ec 中。

For starters, you are re-assigning ec each time through the loop, throwing away the earlier value.

If I understand correctly what you are trying to do, you might be better off building a dictionary that maps node id:s to their position in node_route. Then loop over G_map.edges(...), looking up the positions of the u and v nodes in node_route using your dictionary, and appending 'r' or 'w' to ec depending on whether pos[u]+1 == pos[v] or not.

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