我试图使用 osmnx 路由两个坐标。我发现 osmnx 将最近的节点取给我们给定的坐标。所以,我在想是否可以从给定的坐标创建自定义节点,然后路由它。
import geopandas as gpd
import networkx as nx
import osmnx as ox
import numpy as np
import matplotlib.pyplot as plt
G = ox.graph.graph_from_xml('map_3.osm', bidirectional=False, simplify=True, retain_all=False)
nodes_proj, edges_proj = ox.graph_to_gdfs(G, nodes=True, edges=True)
origin_node = ox.distance.nearest_nodes(G,8.390278350417713,48.98250179896953)
destination_node = ox.distance.nearest_nodes(G, 8.390791842821093, 48.98715746773447)
route = ox.distance.shortest_path(G, origin_node,destination_node, weight='length')
bbox = ox.utils_geo.bbox_from_point(point=(48.985338, 8.393094), dist=700)
fig, ax = ox.plot_graph_route(G, route, route_color='r',bbox = bbox, route_linewidth=6,route_alpha=0.5,orig_dest_size=100, ax=None, node_size=0, bgcolor='k')
我还发现了this,它有助于将纬度、经度转换为x、y。
我尝试像这样添加节点,
test = 10000001
G.add_node(test,x=l1,y=l2)
nearest_nodes1 = ox.distance.nearest_nodes(G, 8.3906217,48.9829172)
我能够成功创建节点(也在图中检查),但是当我现在尝试路由时,
route = ox.distance.shortest_path(G, origin_node1,destination_node, weight='length')
它返回 None。我哪里错了?
我认为您忘记向节点添加边缘。 因此,您创建了一个额外的节点,但没有将其连接到您的图表。 因此它无法计算距离? 您还应该添加这样的边缘:
G.add_edge(origin_node,nearest_nodes1)
希望这仍然有帮助?