#-*- coding: utf-8 -*-#made by 汪林_质检处 importos.path importarcpy importsys from arcpy importenv FCDBDir = "E:\cliptest\data"output = "E:\Result"clipshp = "E:\cliptest\clip.shp" GDBAllPath=[] #OID字段名称 ShapeOID = "" print 'processing...' if notisinstance(clipshp,unicode): clipshp = clipshp.decode('utf-8') if notisinstance(FCDBDir,unicode): FCDBDir = FCDBDir.decode('utf-8') if notisinstance(output,unicode): output = output.decode('utf-8') if notos.path.isfile(clipshp): print clipshp +"is not a File"sys.exit(0) fields =arcpy.ListFields(clipshp) if fields isNone: print "Read "+clipshp+"Failed"sys.exit(0) for field infields: if field.type == "OID": ShapeOID =field break ifos.path.exists(FCDBDir): for dirpath,dirnames,filenames inos.walk(FCDBDir): #遍历GDB文件夹 获取GDB for dirname indirnames: if ".gdb" indirname: gdbfilepath =os.path.join(dirpath,dirname) if not gdbfilepath inGDBAllPath: GDBAllPath.append(gdbfilepath) #遍历MDB文件夹 获取MDB for filename infilenames: if os.path.splitext(filename)[1]=='.mdb': mdbfilepath =os.path.join(dirpath,filename) if not mdbfilepath inGDBAllPath: GDBAllPath.append(mdbfilepath) #遍历Shp文件夹 获取Shape for filename infilenames: if os.path.splitext(filename)[1]=='.shp': shpfilepath =os.path.join(dirpath,filename) if not dirpath inGDBAllPath: GDBAllPath.append(dirpath) else: print "Directory "+FCDBDir+"Not Exist"sys.exit(0) arcpy.MakeFeatureLayer_management(clipshp, "lyr") values = [row[0] for row inarcpy.da.SearchCursor(clipshp, ShapeOID.name)] uniqueValues =set(values) for unique inuniqueValues: arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION",ShapeOID.name + "= " +str(unique)) rows = arcpy.SearchCursor("lyr") for row inrows: everyResultPath =os.path.join(output,str(row.getValue(ShapeOID.name))) if notos.path.exists(everyResultPath): print "Directory "+everyResultPath+"Created Succeed"os.makedirs(everyResultPath) for everyfilepath inGDBAllPath: env.workspace =everyfilepath singlefclist = arcpy.ListFeatureClasses("","All") if singlefclist and len(singlefclist)>0: for singlefc insinglefclist: #如果singlefc是unicode则不做改变,否则将utf-8的singlefc编码解码成unicode if notisinstance(singlefc,unicode): singlefc = singlefc.decode('utf-8') #对于Shape FC会带扩展名.shp,如果是gdb或mdb 则不带 if '.shp' insinglefc: #只有GB2312编码才能做替换操作 newoutputsinglefc = str(singlefc.encode('gb2312')).replace('-','_') if notisinstance(newoutputsinglefc,unicode): newoutputsinglefc = newoutputsinglefc.decode('gb2312') singlefc = singlefc[0:singlefc.find('.shp')] arcpy.env.outputMFlag= "disabled"arcpy.Clip_analysis(singlefc+".shp","lyr",everyResultPath+"\"+newoutputsinglefc+".shp",""); else: #只有GB2312编码才能做替换操作 newoutputsinglefc = str(singlefc.encode('gb2312')).replace('-','_') if notisinstance(newoutputsinglefc,unicode): newoutputsinglefc = newoutputsinglefc.decode('gb2312') arcpy.Clip_analysis(singlefc,"lyr",everyResultPath+"\"+newoutputsinglefc+".shp",""); datasetlist = arcpy.ListDatasets("","Feature") for dataset indatasetlist: #如果dataset是unicode则不做改变,否则将utf-8的dataset编码解码成unicode if notisinstance(dataset,unicode): dataset = dataset.decode('utf-8') ifisinstance(everyfilepath,unicode): env.workspace = everyfilepath+"\"+dataset dspath = everyfilepath+"\"+dataset else: env.workspace = everyfilepath+"\"+dataset.encode('gb2312') dspath = everyfilepath+"\"+dataset.encode('gb2312') fclist = arcpy.ListFeatureClasses("") if fclist and len(fclist)>0: for fc infclist: arcpy.Clip_analysis(fc,"lyr",everyResultPath+"\"+fc+".shp",""); print "Done"
上面裁剪结果为shape格式,下面裁剪结果为GDB格式。
#-*- coding: utf-8 -*-#made by 汪林_质检处 importos.path importarcpy importsys from arcpy importenv FCDBDir = "E:\cliptest\data"output = "E:\Result\caituhou"clipshp = "E:\cliptest\clip.shp" GDBAllPath=[] #OID字段名称 ShapeOID = "" print 'processing...' if notisinstance(clipshp,unicode): clipshp = clipshp.decode('utf-8') if notisinstance(FCDBDir,unicode): FCDBDir = FCDBDir.decode('utf-8') if notisinstance(output,unicode): output = output.decode('utf-8') if notos.path.isfile(clipshp): print clipshp +"is not a File"sys.exit(0) fields =arcpy.ListFields(clipshp) if fields isNone: print "Read "+clipshp+"Failed"sys.exit(0) for field infields: if field.type == "OID": ShapeOID =field break ifos.path.exists(FCDBDir): for dirpath,dirnames,filenames inos.walk(FCDBDir): #遍历GDB文件夹 获取GDB for dirname indirnames: if ".gdb" indirname: gdbfilepath =os.path.join(dirpath,dirname) if not gdbfilepath inGDBAllPath: GDBAllPath.append(gdbfilepath) #遍历MDB文件夹 获取MDB for filename infilenames: if os.path.splitext(filename)[1]=='.mdb': mdbfilepath =os.path.join(dirpath,filename) if not mdbfilepath inGDBAllPath: GDBAllPath.append(mdbfilepath) #遍历Shp文件夹 获取Shape for filename infilenames: if os.path.splitext(filename)[1]=='.shp': shpfilepath =os.path.join(dirpath,filename) if not dirpath inGDBAllPath: GDBAllPath.append(dirpath) else: print "Directory "+FCDBDir+"Not Exist"sys.exit(0) arcpy.MakeFeatureLayer_management(clipshp, "lyr") values = [row[0] for row inarcpy.da.SearchCursor(clipshp, ShapeOID.name)] uniqueValues =set(values) for unique inuniqueValues: arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION",ShapeOID.name + "= " +str(unique)) rows = arcpy.SearchCursor("lyr") for row inrows: everyResultPath =os.path.join(output,str(row.getValue(ShapeOID.name))) if notos.path.exists(everyResultPath): os.makedirs(everyResultPath) print "Directory "+everyResultPath+"Created Succeed"outputgdb = os.path.join(everyResultPath,"ClipResult"+str(row.getValue(ShapeOID.name))+".gdb") ifos.path.isdir(outputgdb): ifarcpy.Exists(outputgdb): arcpy.Delete_management(outputgdb) arcpy.CreateFileGDB_management(everyResultPath,"ClipResult"+str(row.getValue(ShapeOID.name))+".gdb") print outputgdb+"Create Succeeded" for everyfilepath inGDBAllPath: env.workspace =everyfilepath singlefclist = arcpy.ListFeatureClasses("","All") if singlefclist and len(singlefclist)>0: for singlefc insinglefclist: #如果singlefc是unicode则不做改变,否则将utf-8的singlefc编码解码成unicode if notisinstance(singlefc,unicode): singlefc = singlefc.decode('utf-8') #对于Shape FC会带扩展名.shp,如果是gdb或mdb 则不带 if '.shp' insinglefc: #只有GB2312编码才能做替换操作 newoutputsinglefc = str(singlefc.encode('gb2312')).replace('-','_') if notisinstance(newoutputsinglefc,unicode): newoutputsinglefc = newoutputsinglefc.decode('gb2312') singlefc = singlefc[0:singlefc.find('.shp')] arcpy.env.outputMFlag= "disabled"arcpy.Clip_analysis(singlefc+".shp","lyr",outputgdb+"\"+newoutputsinglefc,""); printsinglefc else: #只有GB2312编码才能做替换操作 newoutputsinglefc = str(singlefc.encode('gb2312')).replace('-','_') if notisinstance(newoutputsinglefc,unicode): newoutputsinglefc = newoutputsinglefc.decode('gb2312') printsinglefc arcpy.Clip_analysis(singlefc,"lyr",outputgdb+"\"+newoutputsinglefc,""); datasetlist = arcpy.ListDatasets("","Feature") for dataset indatasetlist: #如果dataset是unicode则不做改变,否则将utf-8的dataset编码解码成unicode if notisinstance(dataset,unicode): dataset = dataset.decode('utf-8') ifisinstance(everyfilepath,unicode): env.workspace = everyfilepath+"\"+dataset dspath = everyfilepath+"\"+dataset else: env.workspace = everyfilepath+"\"+dataset.encode('gb2312') dspath = everyfilepath+"\"+dataset.encode('gb2312') fclist = arcpy.ListFeatureClasses("") if fclist and len(fclist)>0: for fc infclist: arcpy.Clip_analysis(fc,"lyr",outputgdb+"\"+fc,""); printfc; print "Done"