ArcPy脚本——根据指定关键字批量导出


本工具用于批量导出shp数据中指定字段等于XX的数据,并输出为新的shp。例如批量导出shp数据中[系统级别]=‘一级’ 的数据。

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Created on: 2018-09-27 
#   (generated by GL:qq,695396984)
# Description: 
# ---------------------------------------------------------------------------

---------------------
import arcpy, os

#输入文件夹
workspace = arcpy.GetParameterAsText(0)
#查询条件,例如[系统级别]='一级'
whereclause=arcpy.GetParameterAsText(1)
#输出数据的后缀,例如将所有一级的输出内容加"_1",则DLGX.shp输出后即为DLGX_1.shp
suffix = arcpy.GetParameterAsText(2)
#输出文件夹
output_folder = arcpy.GetParameterAsText(3)
#用于过滤的字段名
fieldname=arcpy.GetParameterAsText(4)
#无该字段时是否输出全部内容
isAllExp=arcpy.GetParameterAsText(5)
arcpy.env.workspace = workspace
Dict = {}
 
#"walk" through subfolders and below, and drop all contents into the empty dictionary
#unique shapefile names as keys, and with their file path as values.
for root, dirs, files in os.walk(workspace):
    for fc in arcpy.ListFeatureClasses():
        output = os.path.join(output_folder,fc[:-4]+suffix+".shp")
        if len(arcpy.ListFields(fc,fieldname))>0:
          arcpy.Select_analysis(fc, output, whereclause)
          arcpy.AddMessage(fc+"  export ok")
        else:
          if(isAllExp):
            arcpy.Select_analysis(fc, output, "1=1")
            arcpy.AddMessage(fc+"  field not exist, export all data")
          else:
            arcpy.AddMessage(fc+"  export failed,field not exist")

运行结果

转载自:https://blog.csdn.net/geliang0021/article/details/82884502

You may also like...