首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用osmnx计算地图上两点之间的距离

用osmnx计算地图上两点之间的距离
EN

Stack Overflow用户
提问于 2022-06-09 10:56:24
回答 1查看 1.2K关注 0票数 1

我有下面的代码来计算地图上两点之间的最短路径。主要问题是networkx的shortest_path函数对两个点都返回相同的节点。我正在模拟的版本基于以下链接:https://towardsdatascience.com/visualization-in-python-finding-routes-between-points-2d97d4881996

get_nearest_node函数似乎不再存在。

我怎么才能修好它?

代码语言:javascript
运行
复制
    import networkx as nx
    import osmnx as ox
    
    graph_area = 'San Francisco, California, United States'
    mode = 'drive'
    
    # Create the graph of the area from OSM data. It will download the data and create the graph
    G = ox.graph_from_place(graph_area, network_type=mode)
    
    # OSM data are sometime incomplete so we use the speed module of osmnx to add missing edge speeds and travel times
    G = ox.add_edge_speeds(G)
    G = ox.add_edge_travel_times(G)
    
    # Save graph to disk if you want to reuse it
    #ox.save_graphml(G, "SanFrancisco.graphml")
    
    
    #G = ox.load_graphml("SanFrancisco.graphml")
    
    # Plot the graph
    fig, ax = ox.plot_graph(G, figsize=(10, 10), node_size=0, edge_color='y', edge_linewidth=0.2)
    
    start_latlng = (37.7824, -122.4461)
    end_latlng = (37.7956, -122.4096)
    
    print(G)
    print(start_latlng[0], start_latlng[1])
    print(end_latlng[0], end_latlng[1])
    
    # find the nearest node to the start location
    orig_node = ox.nearest_nodes(G, start_latlng[0], start_latlng[1])# find the nearest node to the end location
    dest_node = ox.nearest_nodes(G, end_latlng[0], end_latlng[1])#  find the shortest path
    print(orig_node)
    print(dest_node)
    shortest_route = nx.shortest_path(G, orig_node, dest_node, method='bellman-ford')
    print(shortest_route)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-13 13:58:06

再次检查节点函数的文档。您正在交换所传递的X和Y(即lng和lat坐标)参数的位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72559089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档