Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 加速匀速缓冲器_Python_Shapely - Fatal编程技术网

Python 加速匀速缓冲器

Python 加速匀速缓冲器,python,shapely,Python,Shapely,我有不同的想法,比如: 然后我创建了一个多边形,如下所示: 我研究了一下,发现缓冲每个线段的速度比缓冲所有的线串然后缓冲整个字符串的速度要快一些。但是,我确实需要缓冲线的总面积作为一个形状良好的多边形,因为我稍后将其用于交叉点检测。因此,我最终不得不使用缓冲多边形来获得整个多边形,这需要一些时间(不是针对这个特定示例,而是针对其他具有更多绿线的示例) 那么,有没有一种更快的方法来获取我不知道的缓冲多边形呢 以下是一个可复制的示例: import numpy as np from shapel

我有不同的想法,比如:

然后我创建了一个多边形,如下所示:

我研究了一下,发现缓冲每个线段的速度比缓冲所有的线串然后缓冲整个字符串的速度要快一些。但是,我确实需要缓冲线的总面积作为一个形状良好的多边形,因为我稍后将其用于交叉点检测。因此,我最终不得不使用缓冲多边形来获得整个多边形,这需要一些时间(不是针对这个特定示例,而是针对其他具有更多绿线的示例)

那么,有没有一种更快的方法来获取我不知道的缓冲多边形呢

以下是一个可复制的示例:

import numpy as np
from shapely.geometry import MultiLineString, LineString, Polygon
from shapely import ops, affinity
import matplotlib.pyplot as plt
from math import atan2, degrees
from descartes.patch import PolygonPatch

if __name__ == '__main__':
    Coords = np.array([
        [0, 0, 0, 0, 'N', 0, 0],
        [0, 1, 0, 'BRANCH', 'N', 0, 0],
        [0, 0, 0, 'BRANCH', 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [-0.85, -0.51, 0, 'BRANCH', 'Y', 45, 0],
        [-0.85, -0.51, 0, 'NODE', 'Y', 45, 0],
        [-1.71, -1.03, 0, 0, 'Y', 45, 0],
        [-1.66, -2.02, 0, 'BRANCH', 'Y', 45, 0],
        [-1.66, -2.02, 0, 'NODE', 'Y', 45, 0],
        [-1.60, -3.02, 0, 'BRANCH', 'Y', 45, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [0.90, -0.42, 0, 'BRANCH', 'Y', 45, 0],
        [0.90, -0.42, 0, 'NODE', 'Y', 45, 0],
        [1.81, -0.84, 0, 'BRANCH', 'Y', 45, 0],
        [0, 0, 0, 'BRANCH', 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [0.10, -0.99, 0, 0, 'Y', 45, 0],
        [-0.69, -1.59, 0, 0, 'Y', 45, 0],
        [-0.53, -2.58, 0, 'BRANCH', 'Y', 45, 0],
        [-0.53, -2.58, 0, 'NODE', 'Y', 45, 0],
    ], dtype=object)

    for ind, coord in enumerate(Coords):
        if coord[3] == 'BRANCH':
            if (coord[0:3] == Coords[ind + 1, 0:3]).all():
                np.delete(Coords, ind, 0)

    lines = []

    j = 0
    for i in range(len(Coords)):
        if (Coords[i, 3] == 'BRANCH') or (i == (len(Coords) - 1)):
            lines.append(Coords[j:i+1].tolist())
            j = i+1

    if not lines:
        Lines = [Coords[:]]
    else:
        Lines = [line for line in lines if len(line) > 1]

    fig, ax = plt.subplots()

    patches = []
    lines = []
    Vs = []
    all_r_lines = []
    texts = []


    for num, line in enumerate(Lines):
        line = np.asarray(line, dtype=object)
        num_coords = line[:, 0:2]
        cumm = 0

        indi_coords = []

        for i, joint in enumerate(line):

            if joint[4] == 'Y' and joint[3] != 'BRANCH':

                """ --------------- BODY -------------------------------- """
                indi_coords.append((joint[0], joint[1]))

                new_coords = ((line[i+1][0]), (line[i+1][1]))
                angle = degrees(atan2(
                    (new_coords[1] - joint[1]),
                    (new_coords[0] - joint[0])
                ))

                if cumm > 0:
                    Lines[num][i][6] = cumm

                cumm += 1

            else:
                indi_coords.append((joint[0], joint[1]))
                cumm = 0

        lines.append(np.asarray(indi_coords))

    linestring = MultiLineString(lines)

    for num, line_coords in reversed(list(enumerate(Lines))):
        for i, joint in reversed(list(enumerate(line_coords))):

            if joint[4] == 'Y' and i < (len(Coords)-1) and joint[3] != 'BRANCH':

                if joint[6] > 0:
                    """ --------------- PATCH -------------------------------- """
                    lineA = LineString([(joint[0], joint[1]),
                                        ((line_coords[i+1][0]), (line_coords[i+1][1]))])
                    left_line = affinity.rotate(
                        lineA, joint[5]/2, (joint[0], joint[1]))
                    rigt_line = affinity.rotate(
                        lineA, -joint[5]/2, (joint[0], joint[1]))

                    try:
                        Vs[-1] = ops.unary_union([MultiLineString(
                            [lineA, left_line, rigt_line])] + all_r_lines[-1])
                    except:
                        Vs.append(MultiLineString([lineA, left_line, rigt_line]))

                    """ --------------- ANGLE LINES -------------------------------- """

                    rotate_angle = line_coords[i-1][5]/2
                    r_lines = [affinity.rotate(
                        Vs[-1],
                        j,
                        (line_coords[i-1][0], line_coords[i-1][1])
                    ) for j in np.linspace(-rotate_angle, rotate_angle, num=3)
                    ]

                    all_r_lines += [r_lines]

                    Vs[-1] = ops.unary_union([Vs[-1]] + r_lines)

                else:
                    """ --------------- PATCH -------------------------------- """
                    lineA = LineString([(joint[0], joint[1]),
                                        ((line_coords[i+1][0]), (line_coords[i+1][1]))])
                    left_line = affinity.rotate(
                        lineA, joint[5]/2, (joint[0], joint[1]))
                    rigt_line = affinity.rotate(
                        lineA, -joint[5]/2, (joint[0], joint[1]))

                    Vs.append(MultiLineString([lineA, left_line, rigt_line]))

                    all_r_lines = []

    all_lines = Vs

    a = ops.unary_union(all_lines)

    creature = (Vs + [a] + [linestring])

    polies = []
    for l in creature:
        polies.append(Polygon(l.buffer(0.5)))

    creature_poly = ops.unary_union(polies)
    creature_patch = PolygonPatch(creature_poly, fc='BLUE', alpha=0.1)

    absorbA = creature_poly
    moves = Vs

    for c_l in linestring:
        x, y = c_l.xy
        ax.plot(x, y)

    for m in all_lines:
        for line in m:
            x, y = line.xy
            ax.plot(x, y, 'g--', alpha=0.25)

    ax.axis('equal')

    ax.add_patch(creature_patch)

    ax.axis('equal')
    plt.show()
将numpy导入为np
从shapely.geometry导入多行线、线串、多边形
从shapely导入操作,关联
将matplotlib.pyplot作为plt导入
从数学导入到2,度
从descartes.patch导入PolygonPatch
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
Coords=np.array([
[0,0,0,0,'N',0,0],
[0,1,0,'分支','N',0,0],
[0,0,0,'分支',0,0,0],
[0, 0, 0, 0, 0, 0, 0],
[-0.85,-0.51,0,'BRANCH','Y',45,0],
[-0.85,-0.51,0,'NODE','Y',45,0],
[1.71,-1.03,0,0,'Y',45,0],
[1.66,-2.02,0,'分支','Y',45,0],
[1.66,-2.02,0,'NODE','Y',45,0],
[1.60,-3.02,0,'分支','Y',45,0],
[0, 0, 0, 0, 0, 0, 0],
[0.90,-0.42,0,'BRANCH','Y',45,0],
[0.90,-0.42,0,'NODE','Y',45,0],
[1.81,-0.84,0,'BRANCH','Y',45,0],
[0,0,0,'分支',0,0,0],
[0, 0, 0, 0, 0, 0, 0],
[0.10,-0.99,0,0,'Y',45,0],
[-0.69,-1.59,0,0,'Y',45,0],
[-0.53,-2.58,0,'BRANCH','Y',45,0],
[-0.53,-2.58,0,'NODE','Y',45,0],
],dtype=object)
对于ind,枚举中的坐标(坐标):
如果坐标[3]=“分支机构”:
如果(坐标[0:3]==坐标[ind+1,0:3])。全部()
np.delete(坐标,ind,0)
行=[]
j=0
对于范围内的i(len(Coords)):
如果(Coords[i,3]=='BRANCH')或(i==(len(Coords)-1)):
line.append(Coords[j:i+1].tolist())
j=i+1
如果不是行:
行=[Coords[:]]
其他:
Lines=[如果len(line)>1,则line for Lines in Lines]
图,ax=plt.子批次()
补丁=[]
行=[]
Vs=[]
所有_r_行=[]
文本=[]
对于num,枚举中的行(行):
line=np.asarray(line,dtype=object)
num_coords=行[:,0:2]
cumm=0
独立协调=[]
对于i,枚举中的联合(第行):
如果关节[4]='Y'和关节[3]!='分支机构“:
“”------------------正文----------------------------“”“
indi_coords.append((联合[0],联合[1]))
新坐标=((第[i+1][0]行),(第[i+1][1]行)
角度=度(atan2(
(新坐标[1]-联合[1]),
(新坐标[0]-关节[0])
))
如果cumm>0:
行[num][i][6]=cumm
cumm+=1
其他:
indi_coords.append((联合[0],联合[1]))
cumm=0
行。追加(名词短语asarray(indi_coords))
linestring=多行线(行)
对于num,反向(列表(枚举(行)))中的行坐标:
对于i,反向连接(列表(枚举(线坐标)):
如果关节[4]=“Y”和i<(len(Coords)-1)和关节[3]!=”分支机构“:
如果接头[6]>0:
“”------------------修补程序----------------------------“”“
lineA=线条字符串([(接缝[0],接缝[1]),
((行坐标[i+1][0]),(行坐标[i+1][1]))
左_线=affinity.rotate(
线性,节理[5]/2,(节理[0],节理[1]))
rigt_line=affinity.rotate(
线性A,-节理[5]/2,(节理[0],节理[1]))
尝试:
Vs[-1]=操作一元联合([MultiLineString(
[直线A、左直线、右直线]+所有直线[-1])
除:
Vs.append(多行线([lineA,left\u line,rigt\u line]))
“”--------------------角线--------------------“”
旋转角度=直线坐标[i-1][5]/2
r_行=[affinity.rotate(
Vs[-1],
J
(行协调[i-1][0],行协调[i-1][1])
)对于np.linspace中的j(-rotate_angle,rotate_angle,num=3)
]
所有r\u线+=[r\u线]
Vs[-1]=操作一元联合([Vs[-1]]+r_线)
其他:
“”------------------修补程序----------------------------“”“
lineA=线条字符串([(接缝[0],接缝[1]),
((行坐标[i+1][0]),(行坐标[i+1][1]))
左_线=affinity.rotate(
线性,节理[5]/2,(节理[0],节理[1]))
rigt_line=affinity.rotate(
线性A,-节理[5]/2,(节理[0],节理[1]))
Vs.append(多行线([lineA,left\u line,rigt\u line]))
所有_r_行=[]
所有线=Vs
a=操作一元联合(所有线路)
生物=(Vs+[a]+[linestring])
polies=[]
对于生物中的l:
polies.append(多边形(l.buffer(0.5)))
生物聚合=行动一元联盟(polies)
生物补丁=多边形补丁(生物补丁,fc='蓝色',alpha=0.1)
吸光度=生物聚
移动=Vs
对于行字符串中的c_l:
x、 y=c_l.xy
轴图(x,y)
对于所有_行中的m:
对于以m为单位的行:
x、 y=line.xy
坐标图(x,y,'g--',α=0.25
    polygons = [Point(i, 0).buffer(0.7) for i in range(5)]

    cascaded_union(polygons)