由于精度问题,Shapely无法在点上拆分线条

2024-04-23 15:12:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在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然而,插值点具有很高的精度。我不知道如何控制这一切。
三。理论上,我可以四舍五入这个新点的坐标,但这似乎也不能保证新点在直线上。在

相关问题herehere,和{a3}。在


Tags: thefromimportnewlinenot精度distance
2条回答

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

# 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

我试着自己用上面的代码,但有时它确实失败,因为大量的分割结果…显然,多边形创建打破了几点线,不知道为什么。在

我使用了snap函数,它似乎起作用了:

    snap(line_to_modify,closest_point,0.01)

返回的值是修改后的几何体,其中有最近的点。首先,你需要使用project和interpolate来找到不在直线上的最近点。你可以用intersect找到它

相关问题 更多 >