【问题标题】:Using shapely to return co ordinates of multilinestring that intersect使用 shapely 返回相交的多线串的坐标
【发布时间】:2018-11-22 11:02:30
【问题描述】:

我使用以下代码使用 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,我正在努力使用“相交”功能。

【问题讨论】:

    标签: python-3.x split shapely line-intersection


    【解决方案1】:

    检查两个 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>]
    

    【讨论】:

      猜你喜欢
      • 2019-12-01
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 2013-12-26
      • 2022-01-22
      相关资源
      最近更新 更多