如何将凸体顶点转换为geopandas多边形。

问题描述 投票:0回答:1

我使用DBSCAN将坐标聚在一起,然后使用凸壳在每个聚类周围绘制 "多边形"。然后我想从我的凸壳形状中构造出geopandas多边形,用于空间连接。

import pandas as pd, numpy as np, matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
from scipy.spatial import ConvexHull



Lat=[10,10,20,23,27,28,29,34,11,34,66,22]
Lon=[39,40,23,21,11,29,66,33,55,22,11,55]

D=list(zip(Lat, Lon))
df = pd.DataFrame(D,columns=['LAT','LON'])

X=np.array(df[['LAT', 'LON']])


kms_per_radian = 6371.0088
epsilon = 1500 / kms_per_radian
db = DBSCAN(eps=epsilon, min_samples=3) 


model=db.fit(np.radians(X))
cluster_labels = db.labels_




num_clusters = len(set(cluster_labels))



cluster_labels = cluster_labels.astype(float)
cluster_labels[cluster_labels == -1] = np.nan



labels = pd.DataFrame(db.labels_,columns=['CLUSTER_LABEL'])

dfnew=pd.concat([df,labels],axis=1,sort=False)





z=[] #HULL simplices coordinates will be appended here

for i in range (0,num_clusters-1):
    dfq=dfnew[dfnew['CLUSTER_LABEL']==i]
    Y = np.array(dfq[['LAT', 'LON']])
    hull = ConvexHull(Y)
    plt.plot(Y[:, 1],Y[:, 0],  'o')
    z.append(Y[hull.vertices,:].tolist())
    for simplex in hull.simplices:
        ploted=plt.plot( Y[simplex, 1], Y[simplex, 0],'k-',c='m')


plt.show()

print(z)

在list[z]中添加的顶点代表凸壳的坐标,但是它们不是以序列和闭环对象构建的,因此使用polygon = Polygon(poin1,point2,point3)构建多边形将不会产生多边形对象.是否有办法使用凸壳顶点构建geopandas多边形对象,以便用于空间连接。THanks for your advice.

geopandas convex-hull
1个回答
1
投票

与其直接生成多边形,我不如用你的坐标做一个MultiPoint,然后在MultiPoint周围生成凸壳。这应该会导致相同的几何体,但以正确的方式排序。

有了 z 如同你所做的列表。

from shapely.geometry import MultiPoint

chulls = []
for hull in z:
    chulls.append(MultiPoint(hull).convex_hull)

chulls
[<shapely.geometry.polygon.Polygon at 0x117d50dc0>,
 <shapely.geometry.polygon.Polygon at 0x11869aa30>]
© www.soinside.com 2019 - 2024. All rights reserved.