我正在尝试绘制一个网络,其中边缘根据其Opens街道地图属性(“高速公路”)具有不同的颜色。如果我使用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')
这个类似的问题(stackoverflow)建议向每个边添加新列,然后修改plot_graph_folium
函数。此外,此修改不起作用。有人可以为我提供一个如何制作具有不同颜色边缘的交互式地图的示例吗?
答案 0 :(得分:0)
是的,您可以使用 OSMnx 创建带有按类型着色的边缘的交互式 Leaflet 网络地图。
OSMnx plot_graph_folium
函数可以接受一个边 color
参数作为关键字参数(参见 the docs),它只是传递给 folium.PolyLine
(参见 {{3} })。根据folium here,folium 反过来只是将关键字参数传递给Leaflet。请参阅传单文档 docs。因此,OSMnx(截至当前版本,v1.0.1)没有内置方法来创建每个边缘都具有自己的颜色的 Leaftlet web 地图。但是,您可以使用 OSMnx 创建一个带有按类型(即它们的 highway
值)着色的边缘的交互式 Leaflet 网络地图,使用如下代码:
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 文档)使其更加精确。