osmnx 笔记: plot_graph_route & plot_graph_routes

本身没什么复杂的,主要是处理数据的部分比较繁琐,所以就独立出来写一个了

1 数据集处理

1.1 获取graph

import osmnx as ox
point1=(31.191184,121.516295)
G_=ox.graph_from_point(point1,dist=3000,network_type='drive')
ox.plot_graph(G_)

1.2 转化成GeoDataFrame

nodes_sh,edges_sh=ox.graph_to_gdfs(G_)

python 包介绍:osmnx_UQI-LIUWJ的博客-CSDN博客-6.1

 

1.3 提取edges_sh中的边 

u, v, _ = list(zip(*edges_sh.index))
edges_sh["u"] = u
edges_sh["v"] = v
#这样的话edges_sh 中就会有u和v 两列了

edges_sh['id']=np.arange(edges_sh.shape[0])
edges_sh.set_index('id',inplace=True)
#修改索引列

edges_sh.drop(['oneway','highway','ref','bridge','lanes','maxspeed','geometry','tunnel'],axis=1,inplace=True)
#丢弃一些暂时不用到的边

1.4 随机生成路线

route_=[63328951]
#表示初始的点
for i in range(1000):
    try:
        route_.append(
            np.random.permutation(
                edges_sh[edges_sh['u']==route_[-1]]['v'].values)[0])
    except:
        break

#u是起点,v是终点,每一次将上一个终点作为下一个起点

#这里用try-except的意思是,如果到了边界,那么就不会有下一条u-v边了,结束route的更新

2 plot_graph_route

2.1 基本使用方法

osmnx.plot.plot_graph_route(
    G, 
    route, 
    route_color='r', 
    route_linewidth=4, 
    route_alpha=0.5, 
    orig_dest_size=100, 
    ax=None, 
    **pg_kwargs)

2.2 参数说明

G (networkx.MultiDiGraph)输入图
route (list)点id组成的list
route_color (string路线的颜色
route_linewidth (int路线的宽度
route_alpha (float)路线的透明度
orig_dest_size (int起止点的大小
ax图像的轴

2.3 使用举例

ox.plot.plot_graph_route(
    G_,
    route_,
    route_color='green',
    route_linewidth=5,
    route_alpha=0.5,
    orig_dest_size=500,
    figsize=(50,20))

 终点在图的边缘处,没有下一条边了(事实上这个route也只有五百多条边)。

这也就是之前使用try语句的原因。

3 plot_graph_routes

3.1 基本使用方法

osmnx.plot.plot_graph_routes(
    G, 
    routes, 
    route_colors='r', 
    route_linewidths=4, 
    **pgr_kwargs)

3.2 参数介绍

G (networkx.MultiDiGraph)输入的图
routes (list)route的list
route_colors (string or list)

如果是string的话,就是所有的路径使用相同的颜色

如果是list的话,就是不同的路径不同的颜色

route_linewidths (int or list)

如果是int的话,就是所有的路径使用相同的宽度

如果是list的话,就是不同的路径不同的宽度

3.3 使用举例

route_list=[]
#route的集合
for _ in range(5):
    route_=[np.random.choice(nodes_sh.index.values)]
    #表示初始的点
    for i in range(1000):
        try:
            route_.append(
                np.random.permutation(
                    edges_sh[edges_sh['u']==route_[-1]]['v'].values)[0])
        except:
            break
    route_list.append(route_)
ox.plot.plot_graph_routes(
    G_,
    route_list,
    route_colors=['green','red','purple','blue','orange'],
    route_linewidths=5,
    figsize=(50,20))

 

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UQI-LIUWJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值