shapely相关操作

shapely相关操作

1. 将为折线的linestring分为多段直线

line = LineString([(114.18453629400005, 30.507459477000054),
 (114.18823357300005, 30.50530951700017),
 (114.19034520000002, 30.50882250000012)])
line

shapely相关操作_第1张图片

multi_list = []
for i in range(len(line.coords[:-1])):
    start, end = line.coords[i], line.coords[i+1]
    ls = LineString([start,end])
    multi_list.append(ls)
multi_list
[,
 ]

2. 延伸linestring

延伸前:

shapely相关操作_第2张图片

延伸后:

shapely相关操作_第3张图片

函数:(注意投影)

def extend_line_in_end(line, distance=10.2):
    """
    延长线段的两端
    ------
    line(linestring):投影过的线段
    distance(float):在两端延长的的长度
    """
    if not isinstance(line, LineString):
        raise ValueError("Input should be a LineString object")

    # 获取起点和终点
    start, end = line.coords[0], line.coords[-1]
    
    # 计算方向
    dx = end[0] - start[0]
    dy = end[1] - start[1]
    angle = math.atan2(dy, dx)

    # 计算新的起点和终点
    new_start = (start[0] - distance*math.cos(angle), start[1] - distance*math.sin(angle))
    new_end = (end[0] + distance*math.cos(angle), end[1] + distance*math.sin(angle))

    # 创建新的LineString
    new_line = LineString([new_start] + list(line.coords)[1:-1] + [new_end])
    
    return new_line

你可能感兴趣的:(交通,shapely,python)