How to deal with rounding errors in Shapely

前端 未结 1 367
北恋
北恋 2020-12-10 04:28

I have a case which is based on projecting a point on a line and then separate this line on it. My use case is slightly more complicated, but my problem can be reproduced wi

相关标签:
1条回答
  • 2020-12-10 05:13

    Fundamentally, a precision model is needed, and there are various plans to implement this into GEOS at some time (don't hold your breath, as this has been under discussion for several years).

    Otherwise, the options are distance-based tests (recommended) or more expensive buffer-based techniques by a small adjustment (see machine epsilon):

    from shapely.geometry import LineString, Point
    
    line1 = LineString([(1, 1.2), (2, 2), (3, 2.3), (4, 1.2)])
    pt = Point(2.5, 1.2)
    pr = line1.interpolate(line1.project(pt))
    
    # Distance based
    print(line1.distance(pr) == 0.0)  # True
    
    # Buffer based
    EPS = 1.2e-16
    print(line1.buffer(EPS).contains(pr))  # True
    print(line1.buffer(EPS).intersects(LineString([pt, pr])))  # True
    

    You can also chain cheaper and expensive tests using an or operator, for example:

    print(line1.contains(pr) or line1.buffer(EPS).contains(pr))
    

    which only runs the second and more expensive test if the first one returns False.

    0 讨论(0)
提交回复
热议问题