OSMnx:在交互式网络地图上绘制网络,每个基础设施使用不同的颜色
OSMnx: plot a network on an interactive web map with different colours per infrastructure
我正在尝试绘制一个网络,其中的边缘根据其 Opens 街道地图属性 ('highway') 具有不同的颜色。如果我使用 ox.graph
,它会起作用,但是,这会生成一个静态地图。如果我使用 ox.plot.plot_graph_folium()
如果我将所有边都设置为具有相同的颜色,我只会得到一个交互式地图。如果我分配给 edge_color 一个颜色列表,它就不起作用。
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 (Whosebug)建议在每条边上增加一个新列,然后修改plot_graph_folium
函数。此外,此修改不起作用。有人可以为我提供一个示例,说明如何制作具有不同颜色边缘的交互式地图吗?
是的,您可以使用 OSMnx 创建交互式 Leaflet 网络地图,其边缘按类型着色。
OSMnx plot_graph_folium
函数可以接受边缘 color
参数作为关键字参数(参见 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。因此,没有 built-in OSMnx(截至当前版本,v1.0.1)创建 Leaflet 网络地图的方法,每条边都有自己的颜色。但是,您 可以 创建交互式 Leaflet 网络使用 OSMnx 将边缘按类型着色(即它们的 highway
值)进行映射,使用如下代码:
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
请注意,此解决方案可以处理简化和未简化的 OSMnx 边(简化边可以有多个 highway
值)。每当它在 types:colors 的字典中找到它的一种边缘类型时,它就会绘制边缘。您可以使用未简化的图表(有关详细信息,请参阅 OSMnx 文档)使其更加精确。
我正在尝试绘制一个网络,其中的边缘根据其 Opens 街道地图属性 ('highway') 具有不同的颜色。如果我使用 ox.graph
,它会起作用,但是,这会生成一个静态地图。如果我使用 ox.plot.plot_graph_folium()
如果我将所有边都设置为具有相同的颜色,我只会得到一个交互式地图。如果我分配给 edge_color 一个颜色列表,它就不起作用。
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 (Whosebug)建议在每条边上增加一个新列,然后修改plot_graph_folium
函数。此外,此修改不起作用。有人可以为我提供一个示例,说明如何制作具有不同颜色边缘的交互式地图吗?
是的,您可以使用 OSMnx 创建交互式 Leaflet 网络地图,其边缘按类型着色。
OSMnx plot_graph_folium
函数可以接受边缘 color
参数作为关键字参数(参见 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。因此,没有 built-in OSMnx(截至当前版本,v1.0.1)创建 Leaflet 网络地图的方法,每条边都有自己的颜色。但是,您 可以 创建交互式 Leaflet 网络使用 OSMnx 将边缘按类型着色(即它们的 highway
值)进行映射,使用如下代码:
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
请注意,此解决方案可以处理简化和未简化的 OSMnx 边(简化边可以有多个 highway
值)。每当它在 types:colors 的字典中找到它的一种边缘类型时,它就会绘制边缘。您可以使用未简化的图表(有关详细信息,请参阅 OSMnx 文档)使其更加精确。