简体   繁体   中英

How can I get a node object from networkx/OSMnx MultiDiGraph

I want to get a node object from OSMnx's MultiDiGraph by node's attribution. For example, by specifying latitude and longitude coordinates or osmid. But now I can only get it through the list index.

import osmnx as ox

# G: MultiDiGraph
G = ox.graph_from_bbox(n, s, e, w, network_type='all')

# get node by node's index
orig = list(G)[5]
dest = list(G)[24]


# Is there a way similar to the following?
# orig = G.nodes(osmid="123456")


route = ox.shortest_path(G, orig, dest, weight="length")
fig, ax = ox.plot_graph_route(G, route, route_color="r", route_linewidth=6, node_size=2)

Have you read the OSMnx documentation and usage examples , and the NetworkX documentation? They demonstrate how to access a node by its ID, and the OSMnx docs explain how to find the node nearest to some lat/long coordinates.

import osmnx as ox
G = ox.graph_from_place("Piedmont, CA, USA", network_type="drive")

# identify OSM ID of node(s) nearest to some point(s)
lat = 37.8262501
lng = -122.2476037
node_id = ox.nearest_nodes(G, lng, lat)

# access some node by its OSM ID
my_node = G.nodes[node_id]

# solve a shortest path between two nodes
path = ox.shortest_path(G, node_id, some_other_node_id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM