基本信息
源码名称:arcgis批量裁剪gdb(批处理)
源码大小:5.33KB
文件格式:
开发语言:Python
更新时间:2021-02-04
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍



# -*- coding: utf-8 -*-
# made by 汪林_质检处
import os.path
import arcpy
import sys
from arcpy import env
 
 
FCDBDir = "D:\\di\\houhe.gdb"
output = "D:\\di\\caituhou"
clipshp = "D:\\di\\tufu.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"