OSMnx:在交互式网络地图上绘制网络,每个基础设施具有不同的颜色 [英] OSMnx: plot a network on an interactive web map with different colours per infrastructure

查看:99
本文介绍了OSMnx:在交互式网络地图上绘制网络,每个基础设施具有不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一个网络,其中边缘根据其 Opens 街道地图属性(高速公路")具有不同的颜色.如果我使用 ox.graph,它会起作用,但是,这会生成一个静态地图.如果我使用 ox.plot.plot_graph_folium(),如果我将所有边设置为相同的颜色,我只会得到一个交互式地图.如果我为 edge_color 分配一个颜色列表,它就不起作用.

<预><代码>将 osmnx 导入为 oxaddress_name='19, Molstraat, Van Leeuwenhoekkwartier, 代尔夫特, 南荷兰省, 荷兰, 2611EM, 荷兰'图=ox.graph_from_address(address_name,距离=300)ec = ['skyblue' if data['highway']=='footway'如果 data['highway']=='residential',则为 'paleturquoise'如果数据['highway']=='cycleway',则为'orange'如果数据['公路']=='服务',则为'sienna'else 'lightgreen' 如果 data['highway']=='living street'如果数据['highway']=='secondary',否则为'灰色'else 'lightskyblue' 如果数据['highway']=='pedestrian'否则为 u, v, key, data in graph.edges(keys=True, data=True)]#this 有效,但是是静态的ox.plot_graph(graph,fig_height=8,fig_width=8,node_size=0,edge_color=ec)#这不起作用进口大叶ox.plot.plot_graph_folium(graph, popup_attribute='highway',edge_color=ec)#this有效,但只有一种颜色进口大叶ox.plot.plot_graph_folium(graph,popup_attribute='highway',edge_color='blue')

这个类似的问题 (

请注意,此解决方案可以处理简化和未简化的 OSMnx 边(简化的边可以有多个 highway 值).每次在 types:colors 字典中找到其边缘类型之一时,它都会绘制边缘.您可以通过使用未简化的图形(有关详细信息,请参阅 OSMnx 文档)使其更加精确.

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).

这篇关于OSMnx:在交互式网络地图上绘制网络,每个基础设施具有不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆