Reputation: 57
I want to filter out all isolated lines that are short (e.g., length<2) from a MultiLineString
. The output is also a MultiLineString
. For isolated lines I mean those do not touch or intersect others.
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()
So this can be separated into two sub-questions:
The second question can be probably solved like this:
filtered = [line for line in list(mline1) if line.length<2]
How to solve the first question?
Upvotes: 1
Views: 1276
Reputation: 57
A solution:
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()
Upvotes: 1