是否可以在Folium中绘制路径?

时间:2020-03-07 14:14:49

标签: python python-3.x maps data-science folium

我阅读了许多与此相关的文档,但是找不到我想要的东西。

我想绘制两点之间的步行路径。可能吗?如果没有,那么python中是否还有其他用于此目的的库?

2 个答案:

答案 0 :(得分:5)

当然可以。使用PolyLine

import folium

m = folium.Map(location=[40.720, -73.993],
              zoom_start=15)

loc = [(40.720, -73.993),
       (40.721, -73.996)]

folium.PolyLine(loc,
                color='red',
                weight=15,
                opacity=0.8).add_to(m)
m

您会得到:

enter image description here


编辑1

要在两点之间绘制步行路径,可以结合使用OSMnxnetworkx

import osmnx as ox
import networkx as nx

ox.config(log_console=True,
          use_cache=True)

G_walk = ox.graph_from_place('Manhattan Island, New York City, New York, USA',
                             network_type='walk')

orig_node = ox.get_nearest_node(G_walk,
                                (40.748441, -73.985664))

dest_node = ox.get_nearest_node(G_walk,
                                (40.748441, -73.4))

route = nx.shortest_path(G_walk, orig_node, dest_node, weight='length')

fig, ax = ox.plot_graph_route(G_walk,
                              route,
                              node_size=0,
                              save=True,
                              file_format='svg',
                              filename='test')

您会得到:

enter image description here


编辑2

对于叶子类型的地图,您可以使用plot_route_folium

import osmnx as ox
import networkx as nx

ox.config(log_console=True, use_cache=True)

G_walk = ox.graph_from_place('Manhattan Island, New York City, New York, USA',
                             network_type='walk')

orig_node = ox.get_nearest_node(G_walk,
                                (40.748441, -73.985664))

dest_node = ox.get_nearest_node(G_walk,
                                (40.748441, -73.4))

route = nx.shortest_path(G_walk,
                         orig_node,
                         dest_node,
                         weight='length')

route_map = ox.plot_route_folium(G_walk, route)

route_map.save('route.html')

您会得到一个有用的html文件:

enter image description here

答案 1 :(得分:0)

如果您使用的是 Openstreetmap 和 folium,那么您可以按照此操作。虽然这是为了驾驶汽车路线与方向和时间。 你可以点击这个链接https://pastebin.ubuntu.com/p/7TtnBxWPNr/