ArcGIS将shp按照属性字段进行分割为多个polygon矢量
将一个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!!!')
参考: