按属性分割要素
按某个字段遍历一个表
with arcpy.da.SearchCursor(input_layer, [split_field]) as cursor:
for row in cursor:
print row[0]#row[0]是因为上面的split_field字段只选了一个
1 import arcpy,os 2 3 # input data 4 input_layer = r'D:\test03\shp\all.shp' 5 split_field = r'NAME' 6 7 # 8 with arcpy.da.SearchCursor(input_layer, [split_field]) as cursor: 9 for row in cursor: 10 print row[0]
sorted是进行排序,并且获取唯一值。删除重复的值。
with arcpy.da.SearchCursor(input_layer, [split_field]) as cursor:
Unique=sorted({row[0] for row in cursor})
1 import arcpy, os 2 3 input_layer = r'D:\test03\shp\all.shp' 4 split_field = r'NAME' 5 6 with arcpy.da.SearchCursor(input_layer, [split_field]) as cursor: 7 Unique=sorted({row[0] for row in cursor})#sorted是进行排序,并且获取唯一值。删除重复的值。
8 9 for i in Unique: 10 print i
获得字段的名字和类型
for f in arcpy.ListFields(input_layer):
if f.name.upper() == split_field.upper():#获得字段的名字
field_type = f.type#获得字段的类型
按属性分割 中的表达式: NAME= ‘吉林’
arcpy.Select_analysis(input_layer,outPath, exp):筛选工具,从输入要素类或输入要素图层中提取要素(通常使用选择或结构化查询语言 (SQL) 表达式),并将其存储于输出要素类中。
1 import arcpy, os 2 3 input_layer = r'D:\test03\shp\all.shp' 4 split_field = r'NAME' 5 outFolder=r'D:\test03\shps' 6 7 with arcpy.da.SearchCursor(input_layer, [split_field]) as cursor: 8 Unique=sorted({row[0] for row in cursor}) 9 10 for f in arcpy.ListFields(input_layer): 11 if f.name.upper() == split_field.upper(): #获得字段的名字 12 field_type = f.type #获得字段的类型 13 14 for i in Unique: 15 16 17 18 if field_type == "String": 19 exp = split_field+"= '"+i+"'" #按属性分割 中的表达式: NAME= ‘吉林’ 20 else: 21 exp = split_field+"="+i+"" 22 print exp 23 24 25 outPath=outFolder+os.sep+i 26 27 28 arcpy.Select_analysis(input_layer,outPath, exp)
arcpy.env.overwriteOutput = True:输出文件夹里面已经有内容的,就覆盖掉。
1 import arcpy, os 2 3 input_layer = r'D:\test03\shp\all.shp' 4 split_field = r'NAME' 5 outFolder=r'D:\test03\shps' 6 7 arcpy.env.overwriteOutput = True#覆盖已经存在输出内容的文件 8 9 with arcpy.da.SearchCursor(input_layer, [split_field]) as cursor: 10 Unique=sorted({row[0] for row in cursor}) 11 12 for f in arcpy.ListFields(input_layer): 13 if f.name.upper() == split_field.upper(): 14 field_type = f.type 15 16 for i in Unique: 17 if field_type == "String": 18 exp = split_field+"= '"+i+"'" 19 else: 20 exp = split_field+"="+i+"" 21 print exp 22 23 outPath=outFolder+os.sep+i 24 arcpy.Select_analysis(input_layer,outPath, exp)
try-except
1 import arcpy, os 2 3 try: 4 input_layer = arcpy.GetParameterAsText(0) 5 split_field = arcpy.GetParameterAsText(1) 6 outFolder=arcpy.GetParameterAsText(2) 7 8 arcpy.env.overwriteOutput = True 9 10 with arcpy.da.SearchCursor(input_layer, [split_field]) as cursor: 11 Unique=sorted({row[0] for row in cursor}) 12 13 for f in arcpy.ListFields(input_layer): 14 if f.name.upper() == split_field.upper(): 15 field_type = f.type 16 17 for i in Unique: 18 19 if field_type == "String": 20 exp = split_field+"= '"+i+"'" 21 else: 22 exp = split_field+"="+i+"" 23 24 outPath=outFolder+os.sep+i 25 arcpy.Select_analysis(input_layer,outPath, exp) 26 arcpy.AddMessage('select:'+outPath) 27 28 except arcpy.ExecuteError: 29 arcpy.GetMessages()
结果:
NAME= '上海'
NAME= '云南'
NAME= '内蒙古'
NAME= '北京'
NAME= '台湾'
NAME= '吉林'
NAME= '四川'
NAME= '天津'
NAME= '宁夏'
NAME= '安徽'
NAME= '山东'
NAME= '山西'
NAME= '广东'
NAME= '广西'
NAME= '新疆'
NAME= '江苏'
NAME= '江西'
NAME= '河北'
NAME= '河南'
NAME= '浙江'
NAME= '海南'
NAME= '湖北'
NAME= '湖南'
NAME= '澳门'
NAME= '甘肃'
NAME= '福建'
NAME= '西藏'
NAME= '贵州'
NAME= '辽宁'
NAME= '重庆'
NAME= '陕西'
NAME= '青海'
NAME= '香港'
NAME= '黑龙江'