문제 설명
점이 있는 geopandas 데이터 프레임에서 다각형 만들기 (Creating a polygon from a geopandas dataframe with points)
저는 X, Y 좌표, 해당 위도 및 경도 및 점으로 기하학을 저장한 geopandas 데이터 프레임을 가지고 있습니다. Data snippet:
long lat X Y geometry
55898 ‑45.520195 ‑18.571566 151 179 POINT (‑45.52019 ‑18.57157)
55902 ‑45.520227 ‑18.582375 151 183 POINT (‑45.52023 ‑18.58238)
55910 ‑45.520293 ‑18.603994 151 191 POINT (‑45.52029 ‑18.60399)
56267 ‑45.517361 ‑18.571574 152 179 POINT (‑45.51736 ‑18.57157)
내가 하고 싶은 것은 4개의 점을 순차적으로 결합하는 것입니다. 즉, 데이터 프레임의 순서에 따라 정사각형의 좌표를 얻을 수 있습니다. 원하는 출력은 실제로 다른 열을 포함할 필요가 없으며 다각형의 기하학만 포함하면 다음과 유사합니다.
1 POLYGON ((977855.4451904297 188082.3223876953,...
2 POLYGON ((1017949.977600098 225426.8845825195,...
3 POLYGON ((988872.8212280273 146772.0317993164,...
4 POLYGON ((1000721.531799316 136681.776184082, ...
5 POLYGON ((915517.6877458114 120121.8812543372,...
참조 솔루션
방법 1:
One should provide the lat and lon coordinates as a nested list to the Shapely Polygon object. See https://stackoverflow.com/a/30461816/12987768
With gdf
being the GeoDataFrame:
from shapely.geometry import Polygon
rows_numbers= [i for i in range(0, len(gdf), 4)]
polygons = []
for idx, _ in enumerate(rows_numbers):
if idx == len(rows_numbers)‑1:
print("end of gdf")
break
polygon_coords = []
for point in gdf.geometry.values[rows_numbers[idx]: rows_numbers[idx+1]]:
polygon_coords.append([point.x, point.y])
polygons.append(Polygon(polygon_coords))
with the resulting polygons
list containing the 4‑point Polygons