0

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.

0

1 Answer 1

5

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
3
  • Perfect answer, but the problem is distance here is in degrees. I want to convert degrees to meters/km/mile. I am not able to find a function that does this.
    – Suchithra
    Oct 22, 2020 at 20:25
  • Any suggestions for converting degrees to meters/km/mile?
    – Suchithra
    Oct 22, 2020 at 21:30
  • Use , shapely.ops.transform() as in Transforming Shapely Polygon and MultiPolygon objects for example, but there are many other examples in GIS SE
    – gene
    Oct 22, 2020 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.