arcpy开发实例之POI格网剖分聚类
# _*_ encoding: utf-8 _*_
import arcpy
arcpy.env.workspace = "d:/temp/poi_yuelu";
# 1. 生成格网(鱼网)
arcpy.management.CreateFishnet("d:/temp/poi_yuelu/grid.shp",'12560907 3264305','12560907 3286103',500,500,45,30,labels=False,geometry_type='POLYGON');
# 删除行政边界外的格网,以减少计算量
# 2. 空间关联计算每个单元格里的poi数量
arcpy.analysis.SpatialJoin("grid","poi_yuelu","d:/temp/poi_yuelu/grid_pcount.shp","JOIN_ONE_TO_ONE",match_option="COMPLETELY_CONTAINS");
#删除所有poi点数量为0的单元格,以减少计算量
with arcpy.da.UpdateCursor("grid_pcount","*","join_count=0") as cursor:
for row in cursor:
cursor.deleteRow();
# 3. 使用自然分类法,将单元格的poi数量划分为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. 根据breaks,为每个单元格设置分组号
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");
# 六边形鱼网
arcpy.stats.OptimizedHotSpotAnalysis("poi_yuelu","d:/temp/poi_hotarea.shp",
Incident_Data_Aggregation_Method="COUNT_INCIDENTS_WITHIN_HEXAGON_POLYGONS",
Cell_Size="20 Meters");