將一個shp中所有的要素都導出為shp
流域.shp屬性表:
import arcpy import os from arcpy import env shp ="D:/01RiverPro/01DATA/流域.shp" #要處理的shp文件路徑 out_path="D:/01RiverPro/01DATA/子流域"#輸出文件夾的路徑 with arcpy.da.SearchCursor(shp, ["SHAPE@",'ID']) as cursor: #SHAPE@指代單個要素,ID是一個字段,該字段也是我們想要作為每個polygon命名的值,也可以改為其他的字段 for row in cursor: out_name=str(row[1])+'.shp'#輸出文件名 確保均為字符串類型,注意命名是唯一的,這樣就能將該shp數據中的所有要素都導出 arcpy.FeatureClassToFeatureClass_conversion (row[0],out_path,out_name)
注意:
row[0]代表的就是polygon要素
row[1]表示的是ID字段,最好確保該字段的唯一性
修改的時候,不需要該row[0] row[1] 只需要改‘ID’為其他即可
結果:
地圖展示:
另外一種方式:
參考:https://www.cnblogs.com/yhpan/p/13556106.html
import re import arcpy from arcpy import env def validateTitle(title): rstr = r"[\/\\\:\*\?\"\<\>\|\,\.\ ]" new_title = re.sub(rstr, "_", title) return new_title env.workspace = "D:/01RiverPro/01DATA/01Headwater/hill_watershed3/test" in_features = "D:/01RiverPro/01DATA/01Headwater/hill_watershed3/test/三級河網.shp" rows = arcpy.SearchCursor(in_features, fields="GRID_CODE") #遍歷每一行 對符合要求的polygon進行分割 for row in rows: selected_field_value = row.getValue("GRID_CODE") where_clause = '"GRID_CODE" =3' #outfilename = validateTitle(selected_field_value) out_feature_class = "D:/01RiverPro/01DATA/01Headwater/hill_watershed3/test/main.shp" arcpy.Select_analysis(in_features, out_feature_class, where_clause) print(selected_field_value+' has done!!!') #將 GRID_CODE字段作為名字 如果是“name” 則需改為%s for row in rows: selected_field_value = row.getValue("GRID_CODE") where_clause = '"GRID_CODE" = %x'%(selected_field_value) #outfilename = validateTitle(selected_field_value) out_feature_class = "D:/01RiverPro/01DATA/01Headwater/hill_watershed3/test/%x.shp"%(selected_field_value) arcpy.Select_analysis(in_features, out_feature_class, where_clause) #print(selected_field_value+' has done!!!')
參考: