使用 shapely to return 相交的多线串坐标

Using shapely to return co ordinates of multilinestring that intersect

我使用 Shapely 的 LineString 函数生成了随机街道,代码如下:

class StreetNetwork():

def __init__(self):
    self.street_coords = []
    self.coords = {}

def gen_street_coords(self, length, coordRange):
    min_, max_ = coordRange
    for i in range(length): 
        street = LineString(((randint(min_, max_), randint(min_, max_)),
                  (randint(min_, max_), randint(min_,max_))))
        self.street_coords.append(street)

如果我使用:

street_network = StreetNetwork() street_network.gen_street_coords(10, [-50, 50])

我得到这样一张图片:Simple

我一直在看下面的 question,它看起来很相似。我现在想遍历我的 street_coords 列表,如果街道与另一条街道交叉,则将街道分成 2 条,但我发现很难找到交叉点的坐标。但是,由于我不熟悉使用 Shapely,所以我很难使用 "intersects" 函数。

检查两个 LineString 对象的交集相当简单。为了避免得到空的几何图形,我建议在计算之前先检查交集。像这样:

from shapely.geometry import LineString, Point

def get_intersections(lines):
    point_intersections = []
    line_intersections = [] #if the lines are equal the intersections is the complete line!
    lines_len = len(lines)
    for i in range(lines_len):
        for j in range(i+1, lines_len): #to avoid computing twice the same intersection we do some index handling
            l1, l2 = lines[i], lines[j]
            if l1.intersects(l2):
                intersection = l1.intersection(l2)
                if isinstance(intersection, LineString):
                    line_intersections.append(intersection)
                elif isinstance(intersection, Point)
                    point_intersections.append(intersection)
                else:
                    raise Exception('What happened?')

    return point_intersections, line_intersections

以例:

l1 = LineString([(0,0), (1,1)])
l2 = LineString([(0,1), (1,0)])
l3 = LineString([(5,5), (6,6)])
l4 = LineString([(5,5), (6,6)])
my_lines = [l1, l2, l3, l4]
print get_intersections(my_lines)

我得到了:

[<shapely.geometry.point.Point object at 0x7f24f00a4710>,      
    <shapely.geometry.linestring.LineString object at 0x7f24f00a4750>]