[ArcPy Tips-4] 找到一个ShapeFile里面积最大的多边形!

import arcpy,os,sys
import arcpy.da

srcDir = "E:\\xxx\\xxx\\"

for file in os.listdir(srcDir):
    if os.path.splitext(file)[1] == ".shp":
        print(file)
        input = file
        areaShp = "AREA_"+file
        if not os.path.exists(srcDir+"\\"+areaShp):
            featureCount = arcpy.GetCount_management(srcDir+"\\"+input)[0]
            if int(featureCount)>0:
                print(input)
                arcpy.CalculateAreas_stats(srcDir+"\\"+input, srcDir+"\\"+areaShp)

        maxPolygonID = 0
        maxArea = 0
        with arcpy.da.SearchCursor(srcDir+"\\"+areaShp,("Id","F_AREA")) as cursor: #Id需为唯一值
            for row in sorted(cursor):
                print(str(row[0])+"_"+str(row[1]))
                if row[1] > maxArea:
                    maxArea = row[1]
                    maxPolygonID = row[0]
        
        maxPolygonShp = "MAX_Polygon_"+file
        arcpy.Select_analysis(srcDir+"\\"+areaShp,srcDir+"\\"+maxPolygonShp," Id = "+str(maxPolygonID))

 

posted @ 2021-10-28 21:50  wsZhang  阅读(93)  评论(0编辑  收藏  举报