一次与一组多边形/多边形相交并获得相交的多边形

问题描述 投票:0回答:1

我有一个

Geopandas
数据框,其中包含五个多边形几何体。现在,我想一次与所有几何体相交,并从中得到相交的多边形。我尝试使用一元联合和
polygonize
但这给了我一个多边形列表,但我只想要一个具有多多边形多边形集的交集的多边形。我们如何将一组多边形或多边形相交并得到相交的多边形?

df=
location    geometry    
1          MULTIPOLYGON (((-0.304766 51.425882, -0.304904...    
2          MULTIPOLYGON (((-0.305968 51.427425, -0.30608 ...    
3          MULTIPOLYGON (((-0.358358 51.423471, -0.3581 5...    
4          MULTIPOLYGON (((-0.357654 51.413925, -0.357604...    
    rows=[]
    listpoly = [a.intersection(b) for a, b in combinations(df['geometry'], 2)]
    rings = []
    for poly in listpoly:
      if type(poly) == MultiPolygon:
         exterior_lines = LineString()
         for p in poly:
            exterior_lines = exterior_lines.union(p.exterior)
         rings.append(exterior_lines)
      elif type(poly) == Polygon:
         rings.append(LineString(list(poly.exterior.coords)))
    union = unary_union(rings)
    result = [geom for geom in polygonize(union)]
    print(result)
MULTILINESTRING((-0.0345 54.900...))
MULTILINESTRING((-0.045 54.200...))
MULTILINESTRING((-0.05 54.650...))
MULTILINESTRING((-0.04 54.750...))
python geopandas intersection multipolygons
1个回答
0
投票

Shapely 的 intersect_all 函数可以帮助你。

代码示例:

import geopandas as gpd
from matplotlib import pyplot as plt
import shapely
import shapely.plotting

data = [
    {"geometry": shapely.box(0, 0, 100, 100), "color": "blue"},
    {"geometry": shapely.box(50, 50, 150, 150), "color": "green"},
    {"geometry": shapely.box(25, 75, 75, 125), "color": "yellow"},
]

data_gdf = gpd.GeoDataFrame(data)
result = shapely.intersection_all(data_gdf.geometry)
print(result)

data_gdf.plot(color=data_gdf["color"], alpha=0.50)
shapely.plotting.plot_polygon(result, color="red", linewidth=2)
plt.show()

结果是红色方块:

© www.soinside.com 2019 - 2024. All rights reserved.