gpt4 book ai didi

python - 尝试使用 OSMnx 在同一图中绘制两个或多个基础设施

转载 作者:行者123 更新时间:2023-11-28 19:05:14 35 4
gpt4 key购买 nike

我正在尝试使用 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 个基础设施、街道和铁路,但不绘制建筑物:

enter image description hereox.plot_figure_ground 的结果(仅绘制街道基础设施):

enter image description here尽管没有绘制建筑数据,但仍在下载建筑数据(我知道 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 中简洁地绘制?

最佳答案

是的,这是可能的,这将简明扼要地做到这一点:

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

OSMnx streets, railway, and buildings plotted together

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

关于python - 尝试使用 OSMnx 在同一图中绘制两个或多个基础设施,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47784074/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com