Shapely无法在点上拆分线段,可能是由于精度问题。

12

我正尝试在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. 但是,插值点的精度非常高,我不知道如何控制它。
3. 理论上,我可以将此新点的坐标四舍五入,但这似乎也不能确保新点位于该直线上。
相关问题在这里这里这里
2个回答

5
我找到了一个有点不太正式的解决方案。如果有人发布更好的方法,我会接受那个方法。
# 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

5

我尝试使用上述代码自行操作,但由于分割结果太多,有时会失败......显然所创建的多边形将线段分解成了几个点,不知道为什么。

我改用了snap函数,似乎可以正常工作:

    snap(line_to_modify,closest_point,0.01)

返回的值是经过修改后最接近它的几何形状。首先,您需要使用project和interpolate找到不在该线上的最接近点。然后,您可以使用intersect进行验证。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接