Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

python - Trying to plot two or more infrastructures in the same figure using OSMnx

I'm trying to plot multiple infrastructure networks(for example streets, rails and buildings) in the same figure using OSMnx but not really having success.

This is one of my attempts:

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)

Result of this code is only ploting the first 2 infrastructures, streets and rails, but not buildings:

enter image description here Result of ox.plot_figure_ground (only plots streets infrastructure):

enter image description here Buildings data is being downloaded despite not being plotted(I know about plot_buildings but I don't want colored buildings, just lines).

Before this I was trying to find a way to add multiple filters all at once in the infrastructure parameter. Something like:

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

But not sure if this is possible.

Any way to plot more than two of them in one figure and, if posible, concisely in OSMnx?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes it is possible, and this will do it concisely:

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

See also https://stackoverflow.com/a/62883614/7321942 and https://stackoverflow.com/a/62720802/7321942


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...