标签:
# -*- coding: utf-8 -*- # made by 汪林_质检处 import os.path import arcpy import sys from arcpy import env FCDBDir = "E:\\cliptest\\data" output = "E:\\Result" clipshp = "E:\\cliptest\\clip.shp" GDBAllPath=[] # OID字段名称 ShapeOID = "" print ‘processing...‘ if not isinstance(clipshp,unicode): clipshp = clipshp.decode(‘utf-8‘) if not isinstance(FCDBDir,unicode): FCDBDir = FCDBDir.decode(‘utf-8‘) if not isinstance(output,unicode): output = output.decode(‘utf-8‘) if not os.path.isfile(clipshp): print clipshp +" is not a File" sys.exit(0) fields = arcpy.ListFields(clipshp) if fields is None: print "Read "+clipshp+"Failed" sys.exit(0) for field in fields: if field.type == "OID": ShapeOID = field break if os.path.exists(FCDBDir): for dirpath,dirnames,filenames in os.walk(FCDBDir): # 遍历GDB文件夹 获取GDB for dirname in dirnames: if ".gdb" in dirname: gdbfilepath = os.path.join(dirpath,dirname) if not gdbfilepath in GDBAllPath: GDBAllPath.append(gdbfilepath) # 遍历MDB文件夹 获取MDB for filename in filenames: if os.path.splitext(filename)[1]==‘.mdb‘: mdbfilepath = os.path.join(dirpath,filename) if not mdbfilepath in GDBAllPath: GDBAllPath.append(mdbfilepath) # 遍历Shp文件夹 获取Shape for filename in filenames: if os.path.splitext(filename)[1]==‘.shp‘: shpfilepath = os.path.join(dirpath,filename) if not dirpath in GDBAllPath: GDBAllPath.append(dirpath) else: print "Directory "+FCDBDir+" Not Exist" sys.exit(0) arcpy.MakeFeatureLayer_management(clipshp, "lyr") values = [row[0] for row in arcpy.da.SearchCursor(clipshp, ShapeOID.name)] uniqueValues = set(values) for unique in uniqueValues: arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION",ShapeOID.name + " = " + str(unique)) rows = arcpy.SearchCursor("lyr") for row in rows: everyResultPath = os.path.join(output,str(row.getValue(ShapeOID.name))) if not os.path.exists(everyResultPath): print "Directory "+everyResultPath+" Created Succeed" os.makedirs(everyResultPath) for everyfilepath in GDBAllPath: env.workspace = everyfilepath singlefclist = arcpy.ListFeatureClasses("","All") if singlefclist and len(singlefclist)>0: for singlefc in singlefclist: # 如果singlefc是unicode则不做改变,否则将utf-8的singlefc编码解码成unicode if not isinstance(singlefc,unicode): singlefc = singlefc.decode(‘utf-8‘) # 对于Shape FC会带扩展名.shp,如果是gdb或mdb 则不带 if ‘.shp‘ in singlefc: # 只有GB2312编码才能做替换操作 newoutputsinglefc = str(singlefc.encode(‘gb2312‘)).replace(‘-‘,‘_‘) if not isinstance(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 not isinstance(newoutputsinglefc,unicode): newoutputsinglefc = newoutputsinglefc.decode(‘gb2312‘) arcpy.Clip_analysis(singlefc,"lyr",everyResultPath+"\\"+newoutputsinglefc+".shp",""); datasetlist = arcpy.ListDatasets("","Feature") for dataset in datasetlist: # 如果dataset是unicode则不做改变,否则将utf-8的dataset编码解码成unicode if not isinstance(dataset,unicode): dataset = dataset.decode(‘utf-8‘) if isinstance(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 in fclist: arcpy.Clip_analysis(fc,"lyr",everyResultPath+"\\"+fc+".shp",""); print "Done"
上面裁剪结果为shape格式,下面裁剪结果为GDB格式。
# -*- coding: utf-8 -*- # made by 汪林_质检处 import os.path import arcpy import sys from arcpy import env FCDBDir = "E:\\cliptest\\data" output = "E:\\Result\\caituhou" clipshp = "E:\\cliptest\\clip.shp" GDBAllPath=[] # OID字段名称 ShapeOID = "" print ‘processing...‘ if not isinstance(clipshp,unicode): clipshp = clipshp.decode(‘utf-8‘) if not isinstance(FCDBDir,unicode): FCDBDir = FCDBDir.decode(‘utf-8‘) if not isinstance(output,unicode): output = output.decode(‘utf-8‘) if not os.path.isfile(clipshp): print clipshp +" is not a File" sys.exit(0) fields = arcpy.ListFields(clipshp) if fields is None: print "Read "+clipshp+"Failed" sys.exit(0) for field in fields: if field.type == "OID": ShapeOID = field break if os.path.exists(FCDBDir): for dirpath,dirnames,filenames in os.walk(FCDBDir): # 遍历GDB文件夹 获取GDB for dirname in dirnames: if ".gdb" in dirname: gdbfilepath = os.path.join(dirpath,dirname) if not gdbfilepath in GDBAllPath: GDBAllPath.append(gdbfilepath) # 遍历MDB文件夹 获取MDB for filename in filenames: if os.path.splitext(filename)[1]==‘.mdb‘: mdbfilepath = os.path.join(dirpath,filename) if not mdbfilepath in GDBAllPath: GDBAllPath.append(mdbfilepath) # 遍历Shp文件夹 获取Shape for filename in filenames: if os.path.splitext(filename)[1]==‘.shp‘: shpfilepath = os.path.join(dirpath,filename) if not dirpath in GDBAllPath: GDBAllPath.append(dirpath) else: print "Directory "+FCDBDir+" Not Exist" sys.exit(0) arcpy.MakeFeatureLayer_management(clipshp, "lyr") values = [row[0] for row in arcpy.da.SearchCursor(clipshp, ShapeOID.name)] uniqueValues = set(values) for unique in uniqueValues: arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION",ShapeOID.name + " = " + str(unique)) rows = arcpy.SearchCursor("lyr") for row in rows: everyResultPath = os.path.join(output,str(row.getValue(ShapeOID.name))) if not os.path.exists(everyResultPath): os.makedirs(everyResultPath) print "Directory "+everyResultPath+" Created Succeed" outputgdb = os.path.join(everyResultPath,"ClipResult"+str(row.getValue(ShapeOID.name))+".gdb") if os.path.isdir(outputgdb): if arcpy.Exists(outputgdb): arcpy.Delete_management(outputgdb) arcpy.CreateFileGDB_management(everyResultPath,"ClipResult"+str(row.getValue(ShapeOID.name))+".gdb") print outputgdb+" Create Succeeded" for everyfilepath in GDBAllPath: env.workspace = everyfilepath singlefclist = arcpy.ListFeatureClasses("","All") if singlefclist and len(singlefclist)>0: for singlefc in singlefclist: # 如果singlefc是unicode则不做改变,否则将utf-8的singlefc编码解码成unicode if not isinstance(singlefc,unicode): singlefc = singlefc.decode(‘utf-8‘) # 对于Shape FC会带扩展名.shp,如果是gdb或mdb 则不带 if ‘.shp‘ in singlefc: # 只有GB2312编码才能做替换操作 newoutputsinglefc = str(singlefc.encode(‘gb2312‘)).replace(‘-‘,‘_‘) if not isinstance(newoutputsinglefc,unicode): newoutputsinglefc = newoutputsinglefc.decode(‘gb2312‘) singlefc = singlefc[0:singlefc.find(‘.shp‘)] arcpy.env.outputMFlag= "disabled" arcpy.Clip_analysis(singlefc+".shp","lyr",outputgdb+"\\"+newoutputsinglefc,""); print singlefc else: # 只有GB2312编码才能做替换操作 newoutputsinglefc = str(singlefc.encode(‘gb2312‘)).replace(‘-‘,‘_‘) if not isinstance(newoutputsinglefc,unicode): newoutputsinglefc = newoutputsinglefc.decode(‘gb2312‘) print singlefc arcpy.Clip_analysis(singlefc,"lyr",outputgdb+"\\"+newoutputsinglefc,""); datasetlist = arcpy.ListDatasets("","Feature") for dataset in datasetlist: # 如果dataset是unicode则不做改变,否则将utf-8的dataset编码解码成unicode if not isinstance(dataset,unicode): dataset = dataset.decode(‘utf-8‘) if isinstance(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 in fclist: arcpy.Clip_analysis(fc,"lyr",outputgdb+"\\"+fc,""); print fc; print "Done"
标签:
原文地址:http://www.cnblogs.com/514687800/p/5109785.html