我有以下代码:
from shapely.geometry import LineString, Point
xs = range(10)
ys = range(10)
points = [Point(x, y) for x, y in zip(x, y)]
line = LineString(points)
现在,我要编辑点,以便如果两个相邻点比MIN_DISTANCE
更近,那么我想删除后一个点:
即从line=[(1, 1), (1.1, 1.1), (3, 3)], MIN_DISTANCE=2
我会得到line=[(1,1), (3,3)]
。
是否可以编写蛮力解决方案(即for point in line
)并覆盖行中的点?或者是否有内置函数?
答案 0 :(得分:1)
您可以使用simplify
方法来实现:
new_line = line.simplify(tolerance=MIN_DISTANCE)
以您的示例为例:
from shapely.geometry import LineString, Point
xs = range(10)
ys = range(10)
x, y = zip(*[(1, 1), (1.1, 1.1), (3, 3)])
points = [Point(x, y) for x, y in zip(x, y)]
line = LineString(points)
MIN_DISTANCE = 2
new_line = line.simplify(tolerance=MIN_DISTANCE)
print(f"Original coordinates: {line.xy}")
print(f"Simplified coordinates: {new_line.xy}")
# Original coordinates: (array('d', [1.0, 1.1, 3.0]), array('d', [1.0, 1.1, 3.0]))
# Simplified coordinates: (array('d', [1.0, 3.0]), array('d', [1.0, 3.0]))