我使用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(poin1,point2,point3)构造多边形将不会生成多边形对象。有没有一种方法可以使用凸包的顶点构造geopandas多边形对象,以便用于空间连接。谢谢您的建议。
答案 0 :(得分:2)
我将直接从您的坐标中创建一个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>]