Python 如何使用Shapely删除彼此太近的点?
我有以下代码:Python 如何使用Shapely删除彼此太近的点?,python,shapely,Python,Shapely,我有以下代码: 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) 现在我想编辑点,如果两个相邻点的距离小于最小距离,那么我想删除后一个点: i、 例如,从line=[(1,1),(1.1,1.1),(3,3)],最小距离=2我会得到line=[(1,1
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)
现在我想编辑点,如果两个相邻点的距离小于最小距离
,那么我想删除后一个点:
i、 例如,从line=[(1,1),(1.1,1.1),(3,3)],最小距离=2
我会得到line=[(1,1),(3,3)]
是否可以编写一个强力解决方案(即,
用于行内点
)并覆盖行内点,或者是否有内置函数?您可以使用简化
方法来实现:
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]))
您可以使用
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]))