arcpy開(kāi)發(fā)實(shí)例之POI的DBSCAN聚類(lèi)
# _*_ encoding: utf-8 _*_
import arcpy
arcpy.env.workspace = "d:/temp/poi_yuelu";
# 1.讀取poi的x和y坐標(biāo)并存入數(shù)組X
X = []
with arcpy.da.SearchCursor("poi_yuelu",["SHAPE@XY"]) as cursor:
? ? for row in cursor:
? ? ? ? X.append([row[0][0],row[0][1]]);
# 2. 將X轉(zhuǎn)換為numpy數(shù)組
import numpy as np
X = np.array(X);
# 3. 使用sklearn.DBSCAN聚類(lèi)
from sklearn.cluster import DBSCAN
dbscan = DBSCAN(eps = 50, min_samples = 10);
clustring = dbscan.fit(X);
# 4. 將聚類(lèi)id寫(xiě)入poi的cid字段
arcpy.management.AddField("poi_yuelu","cid","SHORT");
with arcpy.da.UpdateCursor("poi_yuelu",["fid","cid"]) as cursor:
? ? i = 0;
? ? for row in cursor:
? ? ? ? row[1] = clustering.labels_[i];
? ? ? ? i +=1;
? ? ? ? cursor.updateRow(row);
#5. 為每個(gè)類(lèi)簇生成最小約束多邊形
arcpy.management.MinimumBoundingGeometry("poi_yuelu","domain.shp","CONVEX_HULL","LIST","cid");