【问题标题】:OSMnx: plot a network on an interactive web map with different colours per infrastructureOSMnx:在交互式网络地图上绘制网络,每个基础设施使用不同的颜色
【发布时间】:2019-05-21 08:20:47
【问题描述】:

我正在尝试根据其 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 函数。此外,此修改不起作用。有人可以为我提供一个如何制作具有不同颜色边缘的交互式地图的示例吗?

【问题讨论】:

    标签: python networkx openstreetmap folium osmnx


    【解决方案1】:

    是的,您可以使用 OSMnx 创建一个交互式 Leaflet web 地图,其边缘按类型着色。

    OSMnx plot_graph_folium 函数可以接受边缘 color 参数作为关键字参数(请参阅the docs),它只是传递给folium.PolyLine(请参阅here)。根据 folium docs,folium 只是将关键字参数传递给 Leaflet。请参阅传单文档here。因此,OSMnx(截至当前版本,v1.0.1)没有内置方法来创建一个 Leaftlet web 地图,每个边缘都有自己的颜色。但是,您可以使用 OSMnx 创建一个交互式 Leaflet web 地图,其边缘按类型(即它们的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 文档)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      • 2018-02-02
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      • 2014-05-18
      相关资源
      最近更新 更多