一种方法是使用interpolate返回沿线指定距离处的点的方法。您只需首先以某种方式生成距离列表。以输入行为例罗伊2012年的回答:
import numpy as np
from shapely.geometry import LineString
from shapely.ops import unary_union
line = LineString(([0, 0], [2, 1], [3, 2], [3.5, 1], [5, 2]))
![enter image description here](https://i.stack.imgur.com/8srIW.png)
以指定距离分裂:
distance_delta = 0.9
distances = np.arange(0, line.length, distance_delta)
# or alternatively without NumPy:
# points_count = int(line.length // distance_delta) + 1
# distances = (distance_delta * i for i in range(points_count))
points = [line.interpolate(distance) for distance in distances] + [line.boundary[1]]
multipoint = unary_union(points) # or new_line = LineString(points)
![enter image description here](https://i.stack.imgur.com/BKeW5.png)
Note that since the distance is fixed you can have problems at the end of the line as shown in the image. Depending on what you want you can include/exclude the [line.boundary[1]]
part which adds the line's endpoint or use distances = np.arange(0, line.length, distance_delta)[:-1]
to exclude the penultimate point.
另请注意,unary_union我正在使用应该比调用更有效object.union(other)在循环内,如另一个答案所示。
分割为固定数量的点:
n = 7
# or to get the distances closest to the desired one:
# n = round(line.length / desired_distance_delta)
distances = np.linspace(0, line.length, n)
# or alternatively without NumPy:
# distances = (line.length * i / (n - 1) for i in range(n))
points = [line.interpolate(distance) for distance in distances]
multipoint = unary_union(points) # or new_line = LineString(points)
![enter image description here](https://i.stack.imgur.com/SPbTr.png)