从带有点的 geopandas 数据框创建多边形

Creating a polygon from a geopandas dataframe with points

我有一个 geopandas 数据框,其中存储了 X、Y 坐标、它们的纬度和经度当量以及它们作为点的几何形状。数据片段:

             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,...

应该将纬度和经度坐标作为嵌套列表提供给 Shapely Polygon 对象。参见

gdf 是 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))

生成的 polygons 列表包含 4 点多边形