首页 > 解决方案 > 如何在同一地理数据框中获取多边形与其他多边形的交集?

问题描述

我有一个包含数千个多边形的 shapefile。他们中的许多人接触但不交叉。我需要得到触摸多边形的公共线。

我尝试使用以下函数来实现我的目的,但输出显示一些MultiLineString只有两个点的线条,应该是一个整体LineString

def calcu_intersect_lines(cgidf):

    intersection = gpd.GeoDataFrame(columns=['geometry'], crs=cgidf.crs)

    while len(cgidf) > 1:
        choose = cgidf.iloc[0]
        cgidf.drop(cgidf.index[0], inplace=True)
        for i in range(len(cgidf.index)):
            cgids = cgidf.iloc[i]
            if choose.geometry.exterior.intersects(cgids.geometry.exterior):
                intersects = choose.geometry.exterior.intersection(cgids.geometry.exterior)
                index = len(intersection)
                intersection.loc[index] = [intersects]

            else:
                continue

        return intersection

对于MultiLineString,我尝试使用该shapely.geometry.LineString.union()功能将两条短线连接MultiLineString在一起,如果它们相互接触。但结果也显示了MultiLineString

geopandas 本身的交集函数似乎也导致了 a MultiLineString

有没有返回正常结果的方法(LineString不是MultiLineString连续的公共线路)?


这是输入和输出数据的一个小例子:

a = Polygon(((0, 0), (0, 0.5), (0.5, 1), (1, 0.5), (1, 0), (0.5, -0.5), (0, 0)))
b = Polygon(((0, 0.5), (0.5, 1), (1, 0.5), (1, 2), (0, 0.5)))
c = Polygon(((1, 0.5), (1, 0), (0.5, -0.5), (1.5, -1), (1, 0.5)))
gdf = gpd.GeoDataFrame(columns=['geometry'], data = [a, b, c])
h = calcu_intersect_lines(gdf)

以下是 的值h

index  geometry
0      MULTILINESTRING ((0 0.5, 0.5 1), (0.5 1, 1 0.5))
1      MULTILINESTRING ((1 0.5, 1 0), (1 0, 0.5 -0.5))

两者LineString中的 分别MultiLineString具有公共点(0.5, 1)(1, 0)

我想要的结果如下:

index  geometry
0      LINESTRING (0 0.5, 0.5 1, 1 0.5))
1      LINESTRING (1 0.5, 1 0, 0.5 -0.5))

可能的解决方案:

在评论中,我被提议替换以下行

intersection.loc[index] = [intersects]

经过

intersection.loc[index] = [LineString([*intersects[0].coords, *map(lambda x: x.coords[1], intersects[1:])])]

它在我的简单示例中运行良好。然而,对于真正的 shapefile,它会比这复杂得多。可能有以下几种情况:

  1. 具有多条公共线的两个多边形。

    from shapely.geometry import Polygon
    
    a = Polygon(((0., 0.), (0., 0.5), (0.5, 1.), (1., 0.5), (1., 0.), (0.5, -0.5), (0., 0.)))
    b = Polygon(((0., 0.5), (0.5, 1.), (1.2, 0.7), (1., 0.), (0.5, -0.5), (2., 0.5), (0., 2.)))
    

    对于ab,他们有两条公共线路LineString(((0., 0.5), (0.5, 1.)))LineString(((1., 0.), (0.5, -0.5)))。在这种情况下,我可以简单地使用intersects函数来测试线条是否接触。但是还有另一个问题:

  2. a 中的行MultiLineString不按顺序排列。

    from shapely.geometry import MultiLineString
    
    ml = MultiLineString((((2, 3), (3, 4)), ((0, 2), (2, 3))))
    

    对于ml,此建议将返回错误结果。你对上面的第二个例子有什么想法吗?

标签: pythongeopandasshapely

解决方案


感谢 Georgy 和其他贡献者的帮助,我已经解决了我的问题。这里shapely.ops.linemerge()介绍的功能是我解决的重点。

我在这里发布我的解决方案:

from shapely import ops

def union_multils(ml):

    '''Union touched LineStrings in MultiLineString or GeometryCollection.

    Parameter
    ---------
    ml: GeometryCollection, MultiLineString or LineString

    return
    ------
    ul: MultiLineString or LineString: a MultiLineString suggest the LineStrings 
        in input ml is not connect entitly.
    '''

    # Drop Point and other geom_type(if exist) out
    ml = list(ml)
    ml = [l for l in ml if l.geom_type == 'LineString']

    # Union
    if len(ml) ==  1 and ml[0].geom_type == 'LineString':
        ul = ml[0]
    else:
        ul = ops.linemerge(ml)

    return ul

推荐阅读