本工具用于批量导出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")