是的,我的 head 函数是 get_d_to_nearest_edge()。这段代码如下:
def get_d_to_nearest_edge(latitude, longitude, file_name):
# get nearest node incident to nearest edge to reference point
G = ox.load_graphml(filename=file_name, folder='BAPenvironment')
distance = get_nearest_edge(G, (latitude, longitude))
print(type(distance))
print(distance)
在这个函数中,我调用了 get_nearest_edge() 方法,但是它被重写为 @Sparky05 之前告诉我的。代码如下:
def get_nearest_edge(G, point):
"""
Return the nearest edge to a pair of coordinates. Pass in a graph and a tuple
with the coordinates. We first get all the edges in the graph. Secondly we compute
the euclidean distance from the coordinates to the segments determined by each edge.
The last step is to sort the edge segments in ascending order based on the distance
from the coordinates to the edge. In the end, the first element in the list of edges
will be the closest edge that we will return as a tuple containing the shapely
geometry and the u, v nodes.
Parameters
----------
G : networkx multidigraph
point : tuple
The (lat, lng) or (y, x) point for which we will find the nearest edge
in the graph
Returns
-------
closest_edge_to_point : tuple (shapely.geometry, u, v)
A geometry object representing the segment and the coordinates of the two
nodes that determine the edge section, u and v, the OSM ids of the nodes.
"""
start_time = time.time()
gdf = ox.graph_to_gdfs(G, nodes=False, fill_edge_geometry=True)
graph_edges = gdf[["geometry", "u", "v"]].values.tolist()
edges_with_distances = [
(
graph_edge,
ox.Point(tuple(reversed(point))).distance(graph_edge[0])
)
for graph_edge in graph_edges
]
edges_with_distances = sorted(edges_with_distances, key=lambda x: x[1])
closest_edge_to_point, distance = edges_with_distances[0]
geometry, u, v = closest_edge_to_point
ox.log('Found nearest edge ({}) to point {} in {:,.2f} seconds'.format((u, v), point, time.time() - start_time))
print(type(distance))
print(distance)
return geometry, u, v, distance
问题是,当我在 get_nearest_edge() 函数中检查类型时,类型是浮点数,就像@Sparky05 所说的那样,找到了距离。但是,当我将此值返回给我的头函数 get_d_to_nearest_edge() 时,该值是一个元组,不能打印为浮点数。
但更重要的是,我发现的距离完全不正确。输出:0.00025593968794827235,实际距离应该是26.13米,有帮助吗?