I'm trying to plot a network where edges have different colors according to their Opens street map attribute ('highway'). It works if I use ox.graph
, however, this generates a static map. If I use ox.plot.plot_graph_folium()
I only get an interactive map if I set all edges to have the same color. If I assign to edge_color a list of colors it doesn't work.
import osmnx as ox
address_name='19, Molstraat, Van Leeuwenhoekkwartier, Delft, South Holland, Netherlands, 2611EM, Netherlands'
graph=ox.graph_from_address(address_name, distance=300)
ec = ['skyblue' if data['highway']=='footway'
else 'paleturquoise' if data['highway']=='residential'
else 'orange' if data['highway']=='cycleway'
else 'sienna' if data['highway']=='service'
else 'lightgreen' if data['highway']=='living street'
else 'grey' if data['highway']=='secondary'
else 'lightskyblue' if data['highway']=='pedestrian'
else 'black' for u, v, key, data in graph.edges(keys=True, data=True)]
#this works, but is static
ox.plot_graph(graph,fig_height=8,fig_width=8,node_size=0, edge_color=ec)
#this does not work
import folium
ox.plot.plot_graph_folium(graph, popup_attribute='highway',edge_color=ec)
#this works, but is one color only
import folium
ox.plot.plot_graph_folium(graph,popup_attribute='highway',edge_color='blue')
This similar question (stackoverflow) suggests adding a new column to each edge and then modify the plot_graph_folium
function. Also, this modification is not working. Could someone provide me with an example of how to make an interactive map with edges of different colors?
Yes, you can use OSMnx to create an interactive Leaflet web map with edges colored by type.
The OSMnx plot_graph_folium
function can accept an edge color
argument as a keyword argument (see the docs) that it just passes along to folium.PolyLine
(see here). According to the folium docs, folium in turn just passes the keyword argument along to Leaflet. See the Leaflet docs here. Thus, there is no built-in way for OSMnx (as of the current release, v1.0.1) to create a Leaftlet web map with each edge given its own color. However, you can create an interactive Leaflet web map with edges colored by type (i.e., their highway
value) with OSMnx, using a bit of code like this:
import osmnx as ox
address = '19, Molstraat, Van Leeuwenhoekkwartier, Delft'
G = ox.graph_from_address(address, dist=300)
# define the colors to use for different edge types
hwy_colors = {'footway': 'skyblue',
'residential': 'paleturquoise',
'cycleway': 'orange',
'service': 'sienna',
'living street': 'lightgreen',
'secondary': 'grey',
'pedestrian': 'lightskyblue'}
# return edge IDs that do not match passed list of hwys
def find_edges(G, hwys):
edges = []
for u, v, k, data in G.edges(keys=True, data='highway'):
check1 = isinstance(data, str) and data not in hwys
check2 = isinstance(data, list) and all([d not in hwys for d in data])
if check1 or check2:
edges.append((u, v, k))
return set(edges)
# first plot all edges that do not appear in hwy_colors's types
G_tmp = G.copy()
G_tmp.remove_edges_from(G.edges - find_edges(G, hwy_colors.keys()))
m = ox.plot_graph_folium(G_tmp, popup_attribute='highway', weight=5, color='black')
# then plot each edge type in hwy_colors one at a time
for hwy, color in hwy_colors.items():
G_tmp = G.copy()
G_tmp.remove_edges_from(find_edges(G_tmp, [hwy]))
if G_tmp.edges:
m = ox.plot_graph_folium(G_tmp,
graph_map=m,
popup_attribute='highway',
weight=5,
color=color)
m
Note that this solution can handle both simplified and unsimplified OSMnx edges (simplified edges can have multiple highway
values). It will draw the edge each time it finds one of its edge types in the dictionary of types:colors. You can make this more precise by using an unsimplified graph (see OSMnx docs for details).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With