It can be useful to specify position along linear features such s LineStrings and MultiLineStrings with a 1-dimensional referencing system. Shapely supports linear referencing based on length or distance, evaluating the distance along a geometric object to the projection of a given point, or the point at a given distance along the object.
使用一维参照系统指定沿线性要素(如LineStrings和MultiLineStrings)得位置是很有用的。Shapely支持基于长度或距离的线性参考,计算沿几何对象到给定点的投影的距离,或沿对象给定距离的点的距离。
object.interpolate(distance[, normalized=False])
Return a point at the specified distance along a linear geometric object.
返回一个沿线性几何对象的指定距离的点。
If the normalized arg is True
, the distance will be interpreted as a fraction of the geometric object’s length.
如果normalized参数为True,距离将被解释为几何对象长度的一部分。
>>> ip = LineString([(0, 0), (0, 1), (1, 1)]).interpolate(1.5)
>>> ip
<shapely.geometry.point.Point object at 0x740570>
>>> ip.wkt
'POINT (0.5000000000000000 1.0000000000000000)'
>>> LineString([(0, 0), (0, 1), (1, 1)]).interpolate(0.75, normalized=True).wkt
'POINT (0.5000000000000000 1.0000000000000000)'
object.project(other[, normalized=False])
Returns the distance along this geometric object to a point nearest the other object.
返回沿该几何对象到最接近另一个对象的点的距离。
If the normalized arg is True
, return the distance normalized to the length of the object. The project()
method is the inverse of interpolate()
.
如果normalized参数为True,返回对象长度的标准化距离。project()方法是interpolate()的逆运算。
>>> LineString([(0, 0), (0, 1), (1, 1)]).project(ip)
1.5
>>> LineString([(0, 0), (0, 1), (1, 1)]).project(ip, normalized=True)
0.75
For example, the linear referencing methods might be used to cut lines at a specified distance.
例如,线性参考方法可能被用来按指定的距离分割线。
def cut(line, distance):
# Cuts a line in two at a distance from its starting point
if distance <= 0.0 or distance >= line.length:
return [LineString(line)]
coords = list(line.coords)
for i, p in enumerate(coords):
pd = line.project(Point(p))
if pd == distance:
return [
LineString(coords[:i+1]),
LineString(coords[i:])]
if pd > distance:
cp = line.interpolate(distance)
return [
LineString(coords[:i] + [(cp.x, cp.y)]),
LineString([(cp.x, cp.y)] + coords[i:])]
>>> line = LineString([(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0)])
>>> pprint([list(x.coords) for x in cut(line, 1.0)])
[[(0.0, 0.0), (1.0, 0.0)],
[(1.0, 0.0), (2.0, 0.0), (3.0, 0.0), (4.0, 0.0), (5.0, 0.0)]]
>>> pprint([list(x.coords) for x in cut(line, 2.5)])
[[(0.0, 0.0), (1.0, 0.0), (2.0, 0.0), (2.5, 0.0)],
[(2.5, 0.0), (3.0, 0.0), (4.0, 0.0), (5.0, 0.0)]]