osmnx:项目指向街段

osmnx: project point to street segments

我有一个由 lat 和 lon 给出的点,我想通过最小欧氏距离找到离该点最近的边。例如

import osmnx as ox
track = [(40.7052, -74.0069)]
fig, ax = ox.plot_graph(G, show=False, close=False)
for pairs in track:
    ax.scatter(pairs[1], pairs[0], c='red')
plt.show()

ox.distance.get_nearest_edge(G, track, return_geom=True, return_dist=True)

然后我得到

(2350521192,2350521202,0,
<shapely.geometry.linestring.LineString at 0x16569aa30>,
162.22242578930698)

它输出边的顶点及其几何。点和最近边缘之间的距离是 162。但是如何找到我的点到最近边缘的投影?

这是一个完整的最小工作示例:

import osmnx as ox
from shapely.geometry import Point
ox.config(use_cache=True, log_console=True)

# create point tuple as (lat, lng)
point = (40.7052, -74.0069)
G = ox.graph_from_point(point, network_type='drive')
u, v, k, edge_geom, dist = ox.distance.get_nearest_edge(G, point, return_geom=True, return_dist=True)

# create shapely point geometry object as (x, y), that is (lng, lat)
point_geom = Point(reversed(point))

# use shapely to find the point along the edge that is closest to the reference point
nearest_point_on_edge = edge_geom.interpolate(edge_geom.project(point_geom))
nearest_point_on_edge.coords[0]