python 将一个LINESTRING除以一列LINESTRING

pnwntuvh  于 2022-11-21  发布在  Python
关注(0)|答案(1)|浏览(183)

我正在寻找一个解决方案,将Main Line除以多条重叠的线。在这个例子中,我有四条线(为了便于阅读,我在这个图表中的Line 1Line 2Line 3上应用了偏移):

行下方:

from shapely import wkt

main_line = wkt.loads('LINESTRING (461179.6655721677 4507148.788223281, 461217.56786209624 4507181.537033379, 461236.3280996226 4507194.537878151, 461241.7247760045 4507197.640095252, 461258.8379542616 4507210.660701941, 461261.9432857035 4507219.791508417, 461270.90091201715 4507254.590010401, 461271.56385885156 4507303.918307676, 461273.67536588735 4507318.460376316, 461286.2322009634 4507358.346460313, 461302.55653224624 4507403.197152592, 461365.2492823085 4507485.060388609, 461480.4983426857 4507548.512415529, 461580.7367309019 4507618.493483591)')

line_1 = wkt.loads('LINESTRING (461179.6655721677 4507148.788223281, 461217.56786209624 4507181.537033379, 461236.3280996226 4507194.537878151, 461241.7247760045 4507197.640095252, 461258.8379542616 4507210.660701941, 461261.9432857035 4507219.791508417, 461270.90091201715 4507254.590010401, 461271.56385885156 4507303.918307676, 461273.67536588735 4507318.460376316, 461286.2322009634 4507358.346460313, 461302.55653224624 4507403.197152592)')
line_2 = wkt.loads('LINESTRING (461179.6655721677 4507148.788223281, 461217.56786209624 4507181.537033379, 461236.3280996226 4507194.537878151, 461241.7247760045 4507197.640095252, 461258.8379542616 4507210.660701941, 461261.9432857035 4507219.791508417, 461270.90091201715 4507254.590010401, 461271.56385885156 4507303.918307676, 461273.67536588735 4507318.460376316, 461286.2322009634 4507358.346460313, 461302.55653224624 4507403.197152592, 461365.2492823085 4507485.060388609)')
line_3 = wkt.loads('LINESTRING (461179.6655721677 4507148.788223281, 461217.56786209624 4507181.537033379, 461236.3280996226 4507194.537878151, 461241.7247760045 4507197.640095252, 461258.8379542616 4507210.660701941, 461261.9432857035 4507219.791508417, 461270.90091201715 4507254.590010401, 461271.56385885156 4507303.918307676, 461273.67536588735 4507318.460376316, 461286.2322009634 4507358.346460313, 461302.55653224624 4507403.197152592, 461365.2492823085 4507485.060388609, 461480.4983426857 4507548.512415529)')

我不知道我有多少行,在分期我会有一个列表。看上面的图像Main Line将被分为4个部分,但我有点困惑,我怎么能做到这一点。我用下面的代码希望成为一个辉煌的想法,但没有运气。

line_list = [line_1, line_2, line_3]
diff_list = []
first_line_length = line_list[0].length

for line in line_list:
    line_length = line.length

    if line_length != first_line_length:
        diff = main_line.symmetric_difference(line)
        diff_list.append(diff)

fig, ax = plt.subplots(figsize=(10, 10))
ax.set_xlabel('X coordinate', fontsize=15)
ax.set_ylabel('Y coordinate', fontsize=15)

plt.plot(*main_line.xy, label='Main Line', color='blue')

plt.plot(*line_1.parallel_offset(distance=5).xy, label='Line 1', color='green')
plt.plot(*line_2.parallel_offset(distance=10).xy, label='Line 2', color='red')
plt.plot(*line_3.parallel_offset(distance=15).xy, label='Line 3', color='violet')

plt.plot(*diff_list[0].parallel_offset(distance=-5).xy, label='Diff Line 1', color='green')
plt.plot(*diff_list[1].parallel_offset(distance=-10).xy, label='Diff Line 2', color='red')

plt.legend()

plt.show()

简单地说,如果我有 n 条比主线短的线,我想把主线分成 n+1 部分。

yshpjwxd

yshpjwxd1#

假设这个练习与前面的练习一样(所有行都有相同的起点),修改代码以执行以下操作:
1.按长度降序排列的订单行,从最长到最短(主线最长)
1.然后迭代行列表,并仅对后面的行进行对称差分(main -line 1、line 1-line 2和line 2-line 3)。
1.这些段将是第2步中计算的差值加上第3行。
线1比线2长,线2比线3长

from shapely import wkt
from matplotlib import pyplot as plt

main_line = wkt.loads('LINESTRING (461179.6655721677 4507148.788223281, 461217.56786209624 4507181.537033379, 461236.3280996226 4507194.537878151, 461241.7247760045 4507197.640095252, 461258.8379542616 4507210.660701941, 461261.9432857035 4507219.791508417, 461270.90091201715 4507254.590010401, 461271.56385885156 4507303.918307676, 461273.67536588735 4507318.460376316, 461286.2322009634 4507358.346460313, 461302.55653224624 4507403.197152592, 461365.2492823085 4507485.060388609, 461480.4983426857 4507548.512415529, 461580.7367309019 4507618.493483591)')

line_1 = wkt.loads('LINESTRING (461179.6655721677 4507148.788223281, 461217.56786209624 4507181.537033379, 461236.3280996226 4507194.537878151, 461241.7247760045 4507197.640095252, 461258.8379542616 4507210.660701941, 461261.9432857035 4507219.791508417, 461270.90091201715 4507254.590010401, 461271.56385885156 4507303.918307676, 461273.67536588735 4507318.460376316, 461286.2322009634 4507358.346460313, 461302.55653224624 4507403.197152592)')
line_2 = wkt.loads('LINESTRING (461179.6655721677 4507148.788223281, 461217.56786209624 4507181.537033379, 461236.3280996226 4507194.537878151, 461241.7247760045 4507197.640095252, 461258.8379542616 4507210.660701941, 461261.9432857035 4507219.791508417, 461270.90091201715 4507254.590010401, 461271.56385885156 4507303.918307676, 461273.67536588735 4507318.460376316, 461286.2322009634 4507358.346460313, 461302.55653224624 4507403.197152592, 461365.2492823085 4507485.060388609)')
line_3 = wkt.loads('LINESTRING (461179.6655721677 4507148.788223281, 461217.56786209624 4507181.537033379, 461236.3280996226 4507194.537878151, 461241.7247760045 4507197.640095252, 461258.8379542616 4507210.660701941, 461261.9432857035 4507219.791508417, 461270.90091201715 4507254.590010401, 461271.56385885156 4507303.918307676, 461273.67536588735 4507318.460376316, 461286.2322009634 4507358.346460313, 461302.55653224624 4507403.197152592, 461365.2492823085 4507485.060388609, 461480.4983426857 4507548.512415529)')
line_list = [main_line,line_1, line_2, line_3]
line_list.sort(key=lambda x: x.length, reverse=True)
diff_list = []

for ix in range(len(line_list)-1):
#for line in line_list:
    lineA=line_list[ix]
    lineB=line_list[ix+1]
    #line_length = line.length

    #if line_length != first_line_length:
    diff = lineA.symmetric_difference(lineB)
    diff_list.append(diff)

fig, ax = plt.subplots(figsize=(10, 10))
ax.set_xlabel('X coordinate', fontsize=15)
ax.set_ylabel('Y coordinate', fontsize=15)

plt.plot(*main_line.xy, label='Main Line', color='blue')

plt.plot(*line_list[1].parallel_offset(distance=5).xy, label='Line 1', color='green')
plt.plot(*line_list[2].parallel_offset(distance=10).xy, label='Line 2', color='red')
plt.plot(*line_list[3].parallel_offset(distance=15).xy, label='Line 3', color='violet')

plt.plot(*diff_list[0].parallel_offset(distance=-5).xy, label='Diff main - Line 1', color='blue')
plt.plot(*diff_list[1].parallel_offset(distance=-10).xy, label='Diff Line 1 - Line 2', color='green')
plt.plot(*diff_list[2].parallel_offset(distance=-15).xy, label='Diff Line 2 - Line 3', color='red')

plt.legend()

plt.show()

相关问题