arcpy開發(fā)實例之POI格網(wǎng)剖分聚類
# _*_ encoding: utf-8 _*_
import arcpy
arcpy.env.workspace = "d:/temp/poi_yuelu";
# 1. 生成格網(wǎng)(魚網(wǎng))
arcpy.management.CreateFishnet("d:/temp/poi_yuelu/grid.shp",'12560907 3264305','12560907 3286103',500,500,45,30,labels=False,geometry_type='POLYGON');
# 刪除行政邊界外的格網(wǎng),以減少計算量
# 2. 空間關(guān)聯(lián)計算每個單元格里的poi數(shù)量
arcpy.analysis.SpatialJoin("grid","poi_yuelu","d:/temp/poi_yuelu/grid_pcount.shp","JOIN_ONE_TO_ONE",match_option="COMPLETELY_CONTAINS");
#刪除所有poi點數(shù)量為0的單元格,以減少計算量
with arcpy.da.UpdateCursor("grid_pcount","*","join_count=0") as cursor:
? ? for row in cursor:
? ? ? ? cursor.deleteRow();
# 3. 使用自然分類法,將單元格的poi數(shù)量劃分為10個組
ys = [];
with arcpy.da.SearchCursor("grid_pcount","join_count") as cursor:
? ? for row in cursor:
? ? ? ? ys.append(row[0]);? ? ??
import jenkspy
breaks = jenkspy.jenks_breaks(ys, nb_class=10)
print breaks
#[1.0, 10.0, 25.0, 45.0, 69.0, 99.0, 138.0, 182.0, 240.0, 305.0, 401.0]
# 4. 根據(jù)breaks,為每個單元格設(shè)置分組號
with arcpy.da.UpdateCursor("grid_pcount",["join_count","pcount"]) as cursor:
? ? for row in cursor:
? ? ? ? if row[0]<breaks[1]:
? ? ? ? ? ? row[1]=1;
? ? ? ? elif row[0]>=breaks[1] and row[0]<breaks[2]:
? ? ? ? ? ? row[1] = 2;
? ? ? ? elif row[0]>=breaks[2] and row[0]<breaks[3]:
? ? ? ? ? ? row[1] = 3;
? ? ? ? elif row[0]>=breaks[3] and row[0]<breaks[4]:
? ? ? ? ? ? row[1] = 4;
? ? ? ? elif row[0]>=breaks[4] and row[0]<breaks[5]:
? ? ? ? ? ? row[1] = 5;
? ? ? ? elif row[0]>=breaks[5] and row[0]<breaks[6]:
? ? ? ? ? ? row[1] = 6;
? ? ? ? elif row[0]>=breaks[6] and row[0]<breaks[7]:
? ? ? ? ? ? row[1] = 7;
? ? ? ? elif row[0]>=breaks[7] and row[0]<breaks[8]:
? ? ? ? ? ? row[1] = 8;
? ? ? ? elif row[0]>=breaks[8] and row[0]<breaks[9]:
? ? ? ? ? ? row[1] = 9;
? ? ? ? elif row[0]>=breaks[9]:
? ? ? ? ? ? row[1] = 10;? ??
? ? ? ? cursor.updateRow(row);
# 6. 使用arcgis自帶的hotarean分析工具
arcpy.stats.OptimizedHotSpotAnalysis("poi_yuelu","d:/temp/poi_hotarea.shp",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Incident_Data_Aggregation_Method="COUNT_INCIDENTS_WITHIN_FISHNET_POLYGONS",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Cell_Size="20 Meters");
# 六邊形魚網(wǎng)
arcpy.stats.OptimizedHotSpotAnalysis("poi_yuelu","d:/temp/poi_hotarea.shp",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Incident_Data_Aggregation_Method="COUNT_INCIDENTS_WITHIN_HEXAGON_POLYGONS",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Cell_Size="20 Meters");