【发布时间】:2016-09-10 10:24:44
【问题描述】:
下面的这段代码是根据我找到的here 修改的,它在沿线定义的点处将一个匀称的线串分成两段。我还检查了其他问题,但它们没有直接解决我的问题。但是,我想扩展它以将线分成多个段(在多个点),到目前为止,我所做的所有尝试都失败了。如何修改它以将字符串拆分为任何给定数量的段或在多个点上说((4,5),(9,18)和(6,5))。
input:
line = LineString([(1,2),(8,7),(4,5),(2,4),(4,7),(8,5),(9,18),(1,2),(12,7),(4,5),(6,5),(4,9)])
breakPoint = Point(2,4)
from shapely.geometry import Point,LineString
def make_line_segment(line_string, breakPoint):
geoLoc = line_string.coords
j = None
for i in range(len(geoLoc) - 1):
if LineString(geoLoc[i:i + 2]).intersects(breakPoint):
j = i
break
assert j is not None
# Make sure to always include the point in the first group
if Point(geoLoc[j + 1:j + 2]).equals(breakPoint):
return geoLoc[:j + 2], geoLoc[j + 1:]
else:
return geoLoc[:j + 1], geoLoc[j:]
line1,line2 = make_line_segment(line,breakPoint)
line1 = LineString(line1)
line2 = LineString(line2)
print line1, line2
output: `LINESTRING (1 2, 8 7, 4 5, 2 4) LINESTRING (2 4, 4 7, 8 5, 9 18, 1 2, 12 7, 4 5, 6 5, 4 9)`
【问题讨论】:
-
你没有为我们提供太多的东西。例如,断点总是在线吗?也不清楚你遇到了什么麻烦。为什么不在第二点拆分
line2等等?提供更多细节说明为什么这很困难。