Graphics Engineer
Graphics Engineer

Reputation: 105

Is it possible to draw paths in Folium?

I read many documentations related to this, but can't find something what I am looking for.

I want to plot walking paths between two points. Is it possible? if not, is there any other library in python for this purpose ?

Upvotes: 5

Views: 20600

Answers (1)

sentence
sentence

Reputation: 8933

Of course, you can. Use 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

and you get:

enter image description here


EDIT 1

In order to draw a walking path between two points, you can use a combination of OSMnx and networkx:

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')

and you get:

enter image description here


EDIT 2

For a folium-type map you can use 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')

and you get a useful html file:

enter image description here

Upvotes: 22

Related Questions