2

我使用以下代码使用 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])

我得到这样的图像:简单

我一直在看以下看起来相似的问题。我现在想遍历我的 street_coords 列表,如果街道与另一条街道交叉,则将街道分成 2 条,但我发现很难找到交叉点的坐标。但是,由于我不熟悉使用 Shapely,我正在努力使用“相交”功能。

4

1 回答 1

0

检查两个 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>]
于 2018-11-29T11:49:24.923 回答