0

I have a point given by lat and lon and I want to find the nearest edge to the point by minimum Euclidean distance. For example

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)

and I get

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

It outputs the vertices of the edge and its geometry. The distance between the point and nearest edge is 162. But how do I find the the projection of my point onto this nearest edge?

sofaruntitled
  • 275
  • 1
  • 2
  • 6

1 Answers1

1

Here's a complete minimal working example:

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]
gboeing
  • 5,691
  • 2
  • 15
  • 41
  • Thank you very much. I also just figured it out using nearest_points from shapely.ops. I should spend some time going through the documentarians of the libraries. I'm really not familiar with them. I'm a phd student at Cornell (wanted to go to your talk when you visited but I had to teach at that time). I developed a nonparametric regression estimator for network data, The method can be used for density estimation or varying coefficient regression on a network. I completed theoretical development and simulation studies but implementation gives me a headache. My coding skill is weak. – sofaruntitled Sep 28 '20 at 18:28