[GIS] Calculating distance between two Linestring using Python

distancegeojsonlinestringpythonshapely

I have two linestring, each linestring represents a rail road. I am trying to calculate minimum distance between these two linestrings. Which function is efficient to do this?

I have only come across distance function that takes a point and linestring but not 2 linestring.

For e.g.:

l1 = LineString([(42.073407, -87.806245), (42.0752508,-87.8080299)])  
l2 = LINESTRING(35.442827 -79.470579, 35.444889 -79.469465, 35.445829 -79.468907, 35.446608 -79.468294, 35.447893 -79.46687)

I need the distance between these two shapely objects.

Best Answer

In The Shapely User Manual -> object.distance(other)

Returns the minimum distance (float) to the other geometric object.

from shapely.geometry import LineString
l1 = LineString([(42.073407, -87.806245), (42.0752508,-87.8080299)])
from shapely.wkt import loads
l2 = loads(" LINESTRING(35.442827 -79.470579, 35.444889 -79.469465, 35.445829 -79.468907, 35.446608 -79.468294, 35.447893 -79.46687)")
l1.distance(l2)
10.650628707489625
l2.distance(l1)
10.650628707489625