【问题标题】:Trying to plot two or more infrastructures in the same figure using OSMnx尝试使用 OSMnx 在同一图中绘制两个或多个基础设施
【发布时间】:2018-05-26 19:33:05
【问题描述】:

我正在尝试使用 OSMnx 在同一个图中绘制多个基础设施网络(例如街道、铁路和建筑物),但并没有真正成功。

这是我的尝试之一:

import osmnx as ox
dist = 2000
point = (41.877092, -87.628)
north, south, east, west = ox.bbox_from_point(point, distance=dist)
bbox_proj = ox.bbox_from_point(point, dist, project_utm=True)
streets = ox.core.osm_net_download(
    north=north,
    south=south,
    east=east,
    west=west,
    infrastructure='way["highway"]'
    )
railways = ox.core.osm_net_download(
    north=north,
    south=south,
    east=east,
    west=west,
    infrastructure='way["railway"]'
    )
buildings = ox.core.osm_net_download(
    north=north,
    south=south,
    east=east,
    west=west,
    infrastructure='way["building"]'
    )
streets[0]['elements'] = streets[0]['elements'] + railways[0]['elements'] + buildings[0]['elements']
net = streets
G = ox.core.create_graph(net)
G = ox.truncate_graph_bbox(G, north, south, east, west, truncate_by_edge=True)
G = ox.project_graph(G)
_, _ = ox.plot.plot_graph(G, bbox=bbox_proj, fig_height=10, node_size=0, edge_color='black', edge_linewidth=0.5, save=True)

此代码的结果仅绘制了前 2 个基础设施、街道和铁路,而不是建筑物:

ox.plot_figure_ground 的结果(仅绘制街道基础设施):

尽管没有绘制建筑物数据,但仍在下载建筑物数据(我知道 plot_buildings,但我不想要彩色建筑物,只需要线条)。

在此之前,我试图找到一种在infrastructure 参数中一次添加多个过滤器的方法。比如:

nets = ox.core.osm_net_download(
    north=north,
    south=south,
    east=east,
    west=west,
    infrastructure='way["highway"],way["railway"],way["buildings"]'
    )

但不确定这是否可能。

有什么方法可以在一个图中绘制两个以上的图形,如果可能的话,在 OSMnx 中简洁地绘制?

【问题讨论】:

    标签: python plot openstreetmap networkx


    【解决方案1】:

    尝试将buildings 替换为building。正确的 OSM 密钥是building,末尾没有“s”。事实上,大多数 OSM 密钥都是单数的。

    【讨论】:

    • 是的,我试过了。当我试图使代码更有条理地解决问题时,只是一个错字。不过谢谢!
    【解决方案2】:

    是的,这是可能的,这将简明扼要:

    import matplotlib.pyplot as plt
    import networkx as nx
    import osmnx as ox
    ox.config(use_cache=True, log_console=True)
    
    # get graphs of different infrastructure types, then combine
    place = 'Berkeley, California, USA'
    G1 = ox.graph_from_place(place, custom_filter='["highway"~"residential"]')
    G2 = ox.graph_from_place(place, custom_filter='["railway"~"rail"]')
    G = nx.compose(G1, G2)
    
    # get building footprints
    fp = ox.footprints_from_place(place)
    
    # plot highway edges in yellow, railway edges in red
    ec = ['y' if 'highway' in d else 'r' for _, _, _, d in G.edges(keys=True, data=True)]
    fig, ax = ox.plot_graph(G, bgcolor='k', edge_color=ec,
                            node_size=0, edge_linewidth=0.5,
                            show=False, close=False)
    
    # add building footprints in 50% opacity white
    fp.plot(ax=ax, color='w', alpha=0.5)
    plt.show()
    

    另见https://stackoverflow.com/a/62883614/7321942和https://stackoverflow.com/a/62720802/7321942

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      相关资源
      最近更新 更多