python - OSMnx:在交互式网络 map 上绘制网络,每个基础设施具有不同的颜色

标签 python networkx openstreetmap folium osmnx

我正在尝试绘制一个网络,其中边缘根据其 Opens 街道 map 属性(“高速公路”)具有不同的颜色。如果我使用 ox.graph,它会起作用,然而,这会生成一个静态 map 。如果我使用 ox.plot.plot_graph_folium()如果我将所有边设置为相同的颜色,我只会得到一个交互式 map 。如果我为 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功能。此外,此修改不起作用。有人可以为我提供一个如何制作具有不同颜色边缘的交互式 map 的示例吗?

最佳答案

是的,您可以使用 OSMnx 创建带有按类型着色的边缘的交互式 Leaflet web map 。
OSMnx plot_graph_folium函数可以接受边缘 color参数作为关键字参数(参见 the docs ),它只是传递给 folium.PolyLine (见 here)。据叶docs , folium 反过来只是将关键字参数传递给 Leaflet。请参阅传单文档 here .因此,OSMnx(截至当前版本,v1.0.1)没有内置方法来创建每个边缘都具有自己的颜色的 Leaftlet web map 。但是,您可以使用 OSMnx 创建一个带有按类型(即它们的 highway 值)着色的边缘的交互式 Leaflet web map ,使用如下代码:

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 folium leaflet interactive web map with street network edges colored by highway type
请注意,此解决方案可以处理简化和未简化的 OSMnx 边(简化边可以有多个 highway 值)。每次在 types:colors 字典中找到其边缘类型之一时,它都会绘制边缘。您可以通过使用未简化的图形(有关详细信息,请参阅 OSMnx 文档)使其更加精确。

关于python - OSMnx:在交互式网络 map 上绘制网络,每个基础设施具有不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56234047/

相关文章:

python - 如何将元组作为参数并返回由参数的前三个和后三个元素组成的元组

java - Osmdroid 禁用双击缩放

python : can reduce be translated into list comprehensions like map, lambda 和过滤器?

python - 使用python的递归grep

python - 我该如何修复 AttributeError : 'dict_values' object has no attribute 'count' ?

python - 比较大量图的同构性

python - 两个节点之间的路径

android - OSM - 使用自定义图标显示当前位置

java - 如何在特定图 block 上绘制图像?

Python POST 请求 - 设置整个原始帖子正文?