arcpy 按属性分割要素类

 

# coding=gbk
import arcpy
from arcpy import da
import os
import re

# 过滤路径非法字符串
def filename_filter(filename):
    filename = re.sub(r'[\/:*?"<>|]', '', filename)
    return filename


def main():
    in_fc=arcpy.GetParameter(0)
    in_field=arcpy.GetParameterAsText(1)
    out_workspace=arcpy.GetParameterAsText(2)
    # 字段唯一值列表
    lst=[]
    with da.SearchCursor(in_fc,[in_field]) as cursor:
        for row in cursor:
            lst.append(row[0])
    lst = set(lst)
    # 类型个数
    record_count = len(lst)
    arcpy.SetProgressor("step","{} feature classes will be exporting...".format(record_count).encode('gbk'),0,record_count,1)
    # 数据库描述
    des_workspace=arcpy.Describe(out_workspace)
    # 如果工作空间是文件夹,那么输出应该是shapefile,应该有扩展名.shp
    if des_workspace.workspaceType=='FileSystem':
        for item in lst:
            arcpy.Select_analysis(in_fc,os.path.join(out_workspace,filename_filter(item) +'.shp'),in_field+'='+"'"+item+"'")
            arcpy.SetProgressorPosition()
    else:
        for item in lst:
            arcpy.Select_analysis(in_fc,os.path.join(out_workspace,filename_filter(item)),in_field+'='+"'"+item+"'")
            arcpy.SetProgressorPosition()
    arcpy.AddMessage('... work done.')


if __name__ == '__main__':
    main()

 

posted @ 2021-02-15 19:40  yzhyingcool  阅读(485)  评论(0编辑  收藏  举报