基於arcpy實現空間資料聚類,kmeans
阿新 • • 發佈:2019-01-07
並不能直接進行空間資料的聚類,原理是讀取要素的x,y座標來進行聚類,然後將聚類中心儲存為空間資料以達到效果
# encoding: utf-8 from sklearn.cluster import KMeans import numpy as np import arcpy import pandas as pd from arcpy import env env.workspace=r"D:\84.gdb" target="bujiandian" cursor=arcpy.SearchCursor(target) allfea=[] dic={} name="ysdm"#程式碼要素列別的欄位 for row in cursor : ls=[] if(dic.has_key(row.getValue(name))): dic[row.getValue(name)]+=1 else: dic[row.getValue(name)] = 1 ls.append(row.getValue(name)) ls.append(row.getValue("ptx")) ls.append(row.getValue("pty")) allfea.append(ls) data2=pd.DataFrame(allfea,columns=[name,"ptx","pty"]) dataFilter=data2.query(name+"=='9000402'") #取座標進行聚類 df1 = dataFilter.ix[:,1 :] kmeans = KMeans(n_clusters=3, random_state=10).fit(df1) #dfl是聚類的結果 df1['jllable'] = kmeans.labels_ df_count_type = df1.groupby('jllable').apply(np.size) ##聚類中心 cent=kmeans.cluster_centers_ #將聚類中心儲存為空間資料 sr = arcpy.SpatialReference(4326) fc=arcpy.CreateFeatureclass_management( r"D:\cs","test.shp", "POINT", "", "","", sr) arcpy.AddField_management(r"D:\cs\test.shp", "leibie", "TEXT") cursor=arcpy.InsertCursor(fc) for line in cent: feature = cursor.newRow() # Add the point geometry to the feature vertex = arcpy.CreateObject("Point") vertex.X = line[0] vertex.Y =line[1] feature.shape = vertex # Add attributes feature.leibie = "shumu" # write to shapefile cursor.insertRow(feature) del cursor del fc