5

Is there a clever way to traverse a line string up until a point and return the distance to that point? As an example:

a = LineString([(1, 1), (9, 1), (9, 6), (4, 6), (4, 3)])
p = Point((4,4,))

The distance from the start of a, to point p would be:

  8  # 9-1
  5  # 6-1
  5  # 9-4
+ 2  # 2, p intesects line segment after 2 units
= 20 

I suppose I can just traverse the line and summing up the distance until p intersects the line segment, and calculate the last bit, but I get a feeling that there is something more elegant in the API that I haven't yet discovered.

Never heard of shapely until today, and I'm quite impressed by what I have seen so far.

1 Answer 1

11

You can use project() method.

project returns the distance along this geometric object to a point nearest the other object.

a = LineString([(1, 1), (9, 1), (9, 6), (4, 6), (4, 3)])
p = Point((4,4,))
a.project(p)
#20.0

Note that if you define a as LineString([(4, 3), (4, 6), (9, 6), (9, 1), (1, 1)]), you get 1.0.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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