Tìm giao điểm của 2 đa giác trong Python
Sep 15 2020
Tôi đang cố gắng lấy pointtọa độ giao nhau của 2 polygonstrong Python.
Ví dụ giao điểm của hai đa giác ( polys2). Đối với mục đích minh họa, tôi đã đánh dấu các điểm giao nhau bằng màu xanh lá cây.
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);
Cách tốt nhất để tìm các điểm giao nhau này trong Python là gì?
Trả lời
10 KadirŞahbaz Sep 15 2020 at 01:39
Sử dụng giao điểm của ranh giới đa giác. Trong trường hợp của bạn, nó trả về các điểm giao nhau dưới dạng đa điểm.
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 ...>]