6

众所周知的功能

from shapely.geometry import *
from shapely.wkt import loads

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 = loads("LINESTRING (12.0133696 47.8217147, 12.0132944 47.8216655, 12.0132056 47.8215749, 12.0131542 47.8215034, 12.0130522 47.8212931, 12.0129941 47.8211294, 12.0130381 47.8209553, 12.0131116 47.8208718, 12.013184 47.8208107, 12.0133547 47.8207312, 12.0135537 47.8206727, 12.013915 47.8206019, 12.0141624 47.8205671, 12.0144317 47.8204965)")

我尝试了一种方法来获得通过在剪切函数之上应用得到的一些线串之间的差异,但由于形状限制,结果并不好。

有任何想法吗?

4

2 回答 2

7

我会回答自己,并对改进感到高兴:

def cut_piece(line,distance, lgth):
    """ From a linestring, this cuts a piece of length lgth at distance.
    Needs cut(line,distance) func from above ;-) 
    """
    precut = cut(line,distance)[1]
    result = cut(precut,lgth)[0]
    return result
于 2015-06-26T12:51:47.387 回答
0

您还可以使用交集并规范化插值以获得特定范围内的曲线:

def cut(line, distance):
    if distance <= 0.0 :#line.length:
        return [None, LineString(line)]
    elif distance >= 1.0:
        return [LineString(line), None]
    coords = list(line.coords)
    for i, p in enumerate(coords):
        pd = line.project(Point(p), normalized=True)
        if pd == distance:
            return [
                LineString(coords[:i+1]),
                LineString(coords[i:])]
        if pd > distance:
            cp = line.interpolate(distance, normalized=True)
            return [
                LineString(coords[:i] + [(cp.x, cp.y)]),
                LineString([(cp.x, cp.y)] + coords[i:])]
def cut_piece(line,distance1, distance2):
    """ From a linestring, this cuts a piece of length lgth at distance.
    Needs cut(line,distance) func from above ;-) 
    """
    l1 = cut(line, distance1)[1]
    l2 = cut(line, distance2)[0]
    result = l1.intersection(l2)
    return result

例子:

cir = Point(0.0, 0.0).buffer(200).exterior
cir.coords = list(cir.coords)[::-1] # to flip the curve direction
print(cir.is_ccw)
d = cut_piece(cir,0.25, 0.75)

例子

于 2020-07-23T19:11:01.860 回答