其他分享
首页 > 其他分享> > [ArcPy Tips-4] 找到一个ShapeFile里面积最大的多边形!

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

作者:互联网

import arcpy,os,sys
import arcpy.da

srcDir = "E:\\Sentinel_1\\2018S1\\"##自行修改,shp所在的目录

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))

 

标签:areaShp,ShapeFile,arcpy,file,input,srcDir,Tips,ArcPy,row
来源: https://www.cnblogs.com/wszhang/p/15477916.html