如何用shapely删除短隔离线

2024-05-13 19:10:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从MultiLineString中过滤掉所有短的隔离线(例如,长度<;2)。输出也是一个MultiLineString。对于孤立线,我指的是那些不touchintersect其他线

from shapely.geometry import MultiLineString
from pprint import pprint
from matplotlib import pyplot
from figures import SIZE, set_limits, plot_line, plot_bounds, color_issimple
from figures import plot_coords as _plot_coords

def plot_coords(ax, ob):
    for line in ob:
        _plot_coords(ax, line, zorder=1)

def plot_lines(ax, ob):
    color = color_issimple(ob)
    for line in ob:
        plot_line(ax, line, color=color, alpha=0.7, zorder=2)

fig = pyplot.figure(1, figsize=SIZE, dpi=90)

#1: line1 original
ax = fig.add_subplot(121)
mline1 = MultiLineString([((0, 0), (2, 0)), ((1, 0), (1, 1)), ((1, 2), (3, 2)),
                          ((3, 0), (3, 1))])


plot_coords(ax, mline1)
plot_bounds(ax, mline1)
plot_lines(ax, mline1)

ax.set_title('a) original')
set_limits(ax, -1, 4, -1, 3)

#2: line2 goal
ax = fig.add_subplot(122)

mline2 = MultiLineString([((0, 0), (2, 0)), ((1, 0), (1, 1)), ((1, 2), (3, 2))])

plot_coords(ax, mline2)
plot_bounds(ax, mline2)
plot_lines(ax, mline2)

ax.set_title('b) goal')
set_limits(ax, -1, 4, -1, 3)

pyplot.show()

enter image description here

这可以分为两个子问题:

  1. 如何选择所有隔离线
  2. 如何按长度过滤这些隔离线

第二个问题可能可以这样解决:

filtered = [line for line in list(mline1) if line.length<2]

如何解决第一个问题


Tags: fromimportplotlinecoordsaxcolorset
1条回答
网友
1楼 · 发布于 2024-05-13 19:10:51

解决方案:

from shapely.geometry import MultiLineString
from pprint import pprint
from matplotlib import pyplot
from figures import SIZE, set_limits, plot_line, plot_bounds, color_issimple
from figures import plot_coords as _plot_coords

def plot_coords(ax, ob):
    for line in ob:
        _plot_coords(ax, line, zorder=1)


def plot_lines(ax, ob):
    color = color_issimple(ob)
    for line in ob:
        plot_line(ax, line, color=color, alpha=0.7, zorder=2)


fig = pyplot.figure(1, figsize=SIZE, dpi=90)

# 3: line3 lines that intersecting each other
mline1 = MultiLineString([((0, 0), (2, 0)), ((1, 0), (1, 1)), ((1, 2), (3, 2)),
                          ((3, 0), (3, 1))])
ax = fig.add_subplot(121)

# append all intersecting lines, this will create duplicates for lines
# intersecting more than one line
intersection = []
for i in range(len(mline1)):
    for j in range(i + 1, len(mline1)):
        if mline1[i].intersects(mline1[j]):
            intersection.append(mline1[i])
            intersection.append(mline1[j])

# remove duplicates
uniq_intersection = []
for line in intersection:
    if not any(p.equals(line) for p in uniq_intersection):
        uniq_intersection.append(line)

uniq_intersection = MultiLineString(uniq_intersection)
plot_coords(ax, uniq_intersection)
plot_bounds(ax, uniq_intersection)
plot_lines(ax, uniq_intersection)

ax.set_title('c) intersection')
set_limits(ax, -1, 4, -1, 3)

# 4: line4 filtered lines
ax = fig.add_subplot(122)
isolated = [line for line in mline1 if line not in uniq_intersection]

# filter by length
isolated_short = [line for line in isolated if line.length < 2]
filtered = [line for line in mline1 if line not in isolated_short]

filtered = MultiLineString(filtered)
plot_coords(ax, filtered)
plot_bounds(ax, filtered)
plot_lines(ax, filtered)

ax.set_title('d) filtered')
set_limits(ax, -1, 4, -1, 3)

pyplot.show()

enter image description here

相关问题 更多 >