为了使用osmnx获取两个坐标之间的路由,我使用了以下代码:
import osmnx as ox
ox.config(use_cache=True, log_console=True)
G = ox.graph_from_place('Sydney,New South Wales,Australia', network_type='drive')
import networkx as nx
# find the nearest node to the start location
orig_node = ox.get_nearest_node(G,(intersections['lat'][0],intersections['lon'][0]))
# find the nearest node to the end location
dest_node = ox.get_nearest_node(G,(intersections['lat'][1],intersections['lon'][1]))
shortest_route=nx.shortest_path(G,orig_node,dest_node,weight='time')
其中,交叉口是一个数据,其中包含了各种交叉口的纬度和经度在悉尼。
交叉口‘’lat‘,交叉口’‘lon’代表第0项的纬度和经度等等。
当我绘制这幅图时,我确实得到了适当的结果:显示路线的图解
我获得这些路由中的点的OSM ids,如下所示:
771347,1612748582
但这些似乎是起点和终点本身。是否可以使用osmnx本身获取上面图像中所示路线中的所有坐标。我知道我可以为此使用各种API,但是由于我有75000个点,而且我需要找到所有这些点之间的路径(以及构成路径的坐标),所以我想要一个更有效的解决方案。
发布于 2022-10-05 13:43:24
nx.shortest_path()
返回构成路由的节点的OSM ids列表。
在osmnx中,您可以使用ox.graph_to_gdfs()
获取OSM的节点信息,该信息将返回包含图中所有节点的GeoDataFrame。
一旦有了GeoDataFrame中的所有节点,就可以很容易地提取坐标:
# Get the nodes given the graph, as a GeoDataFrame
nodes = ox.graph_to_gdfs(G, nodes=True, edges=False)
# Extract only the nodes that form your route
nodes = nodes.set_index('id').reindex(shortest_route).reset_index()
# Store all the route information into a DataFrame keeping only useful columns
route_df = nodes[['id', 'lon', 'lat']]
https://stackoverflow.com/questions/73956882
复制相似问题