【问题标题】:Shapely unable to split line on point due to precision issues由于精度问题,Shapely 无法在点上分割线
【发布时间】:2018-05-05 21:13:08
【问题描述】:

我正在尝试在 Shapely 中将一个点插入到 LineString 上,然后相应地拆分线串。但是由于精度误差,Shapely 认为插值点不在linestring 上,因此split 操作不起作用。

这是一个例子:

from shapely.ops import split
from shapely.geometry import LineString, Point

### Initialize point and line
line = LineString([(0.123,0.456),(5.678,7.890),(12.135,6.789)])    
point = Point(4.785,8.382)   

### Interpolate point onto line
new_point = line.interpolate(line.project(point))
print new_point
>> POINT (5.593949278213755 7.777518800043393)

### BUT: line does not intersect the interpolated point
line.intersects(new_point)
>> False

### EVEN THOUGH: distance between them is essentially (not exactly) zero
line.distance(new_point)
>> 0.0

### THEREFORE: line cannot be split using the new point
len(split(line, new_point))
>> 1

我认为问题如下:
1.我将原始点/线坐标四舍五入,以免它们超出机器的精度限制。
2. 但是,INTERPOLATED 点的精度非常高。我不知道如何控制它。
3.理论上,我可以将这个新点的坐标四舍五入,但这似乎也不能确保新点在线上。

相关问题hereherehere

【问题讨论】:

    标签: python shapely


    【解决方案1】:

    我想出了一个有点老套的解决方案。如果有人发布更好的,我会接受。

    # After the code above: 
    
    ### Create a buffer polygon around the interpolated point
    buff = new_point.buffer(0.0001)
    
    ### Split the line on the buffer
    first_seg, buff_seg, last_seg = split(line,buff)
    
    ### Stitch together the first segment, the interpolated point, and the last segment 
    line = LineString(list(first_seg.coords) + list(new_point.coords) + list(last_seg.coords))
    
    line.intersects(new_point)
    >> True
    

    【讨论】:

      【解决方案2】:

      我自己尝试了上面的代码,但有时它确实会因为大量的分割结果而失败......显然创建的多边形将线分成了几个点,不知道为什么。

      我改用了 snap 功能,它似乎可以工作:

          snap(line_to_modify,closest_point,0.01)
      

      返回值是修改后的几何图形,其中包含最近的点。首先,您需要使用 project 和 interpolate 来找到不在线上的最近点。然后你可以用 intersect 来验证它

      【讨论】:

        猜你喜欢
        • 2016-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多