Python에서 두 다각형의 교차점 찾기

Sep 15 2020

파이썬에서 point2 의 교차 좌표 를 얻으려고합니다 polygons.

예를 들어 두 다각형의 교차점 ( polys2). 설명을 위해 교차점을 녹색으로 표시했습니다.

from shapely.geometry import Polygon,Point
import geopandas
polys1 = geopandas.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]),
                               Polygon([(1,1), (3,1), (3,3), (1,3)])])
 
polys2 = geopandas.GeoSeries([Point([(1,2)]),Point([(2,1)]) ])
                                  
df1 = geopandas.GeoDataFrame({'geometry': polys1, 'df1':[1,2]})
df2 = geopandas.GeoDataFrame({'geometry': polys2, 'df2':[1,2]})

ax = df1.plot(color='red', alpha = 0.5)
df2.plot(ax=ax, color='green', alpha=0.5);

파이썬에서 이러한 교차점을 찾는 가장 좋은 방법은 무엇입니까?

답변

10 KadirŞahbaz Sep 15 2020 at 01:39

다각형 경계의 교차점을 사용합니다. 귀하의 경우 교차점을 다점으로 반환합니다.

geom1 = df1.geometry[0]
geom2 = df1.geometry[1]

points = geom1.boundary.intersection(geom2.boundary) # multipoint
points = list(points) # point list

print(points)
#[<shapely.geometry.point.Point at ...>,
# <shapely.geometry.point.Point at ...>]