1

I use Shapely and I've run into an issue where I can find the intersection of two lines. However once I have the intersection point, I don't seem to be able and cross-reference that it belongs in both lines.

Example

from shapely.geometry import Point, LineString

# Test two lines
l1 = LineString([(0, 300), (999, 300)])
l2 = LineString([(379, 250), (450, 320)])
point = l1.intersection(l2)

assert point.intersects(l1)  # True
assert point.intersects(l2)  # False

I wonder if this is a rounding issue? And in such a case how can I get around it? I.e. if I have a point, how can I guarantee that the point is in the line with an error of say 2 decimals?

  • I believe that it is indeed due to floating point precision. Not very elegant, but you could do something like: point.buffer(0.00001).intersects(l2). I thought shapely had a near() method that allowed you to provide a precision for intersects(), but I couldn't find it. You could use the distance() function and ensure that point is within some specified tolerance of both lines. – Jon Mar 14 at 18:58

Your Answer

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

Browse other questions tagged or ask your own question.