我已经取得了类似的成就groupby
功能。假设你的点实际上是 ShapelyPoint
对象,并按正确的顺序排序,你可以尝试这样的事情。
import pandas as pd
import geopandas as gp
from shapely.geometry import Point, Polygon
# Initialize a test GeoDataFrame where geometry is a list of points
df = gp.GeoDataFrame( [['box', Point(1, 0)],
['box', Point(1, 1)],
['box', Point(2,2)],
['box', Point(1,2)],
['triangle', Point(1, 1)],
['triangle', Point(2,2)],
['triangle', Point(3,1)]],
columns = ['shape_id', 'geometry'],
geometry='geometry')
# Extract the coordinates from the Point object
df['geometry'] = df['geometry'].apply(lambda x: x.coords[0])
# Group by shape ID
# 1. Get all of the coordinates for that ID as a list
# 2. Convert that list to a Polygon
df = df.groupby('shape_id')['geometry'].apply(lambda x: Polygon(x.tolist())).reset_index()
# Declare the result as a new a GeoDataFrame
df = gp.GeoDataFrame(df, geometry = 'geometry')
df.plot()