Coordinates of the closest points of two geometries in Shapely

The GIS term you are describing is linear referencing, and Shapely has these methods.

# Length along line that is closest to the point
print(line.project(p))

# Now combine with interpolated point on line
p2 = line.interpolate(line.project(p))
print(p2)  # POINT (5 7)

An alternative method is to use nearest_points:

from shapely.ops import nearest_points
p2 = nearest_points(line, p)[0]
print(p2)  # POINT (5 7)

which provides the same answer as the linear referencing technique does, but can determine the nearest pair of points from more complicated geometry inputs, like two polygons.

Leave a Comment