批量追加数据库(GDB,MDB,Shp)

程序需要追加的要素类的结构Schema格式一致,将遍历后的第一个工作空间为范本,后续进行追加。

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# 质检处_汪林
# Created on: 2014-09-29 10:25:22.00000
#   (generated by ArcGIS/ModelBuilder)
# Description: 可以追加Shape、MDB、GDB  结果存放在AppendResult文件夹中
# ---------------------------------------------------------------------------

# Import arcpy module
import os
import shutil
import sys
import arcpy
import os.path
import time
from arcpy import env

FCDBDir = "E:\\复杂繁体"
dicAllFC={}
fcall=[]
GDBAllPath=[]

#复制函数
def copytree(src,dst,symlinks=False):
    #如果目标目录不存在,则创建目录
    if not os.path.isdir(dst):
        os.makedirs(dst)
    errors=[]
    if os.path.isdir(src):
        names = os.listdir(src)
        for name in names:
            srcname = os.path.join(src,name)
            dstname = os.path.join(dst,name)
            try:
                #链接地址
                if symlinks and os.path.islink(srcname):
                    linkto = os.readlink(srcname)
                    os.symlink(linkto,dstname)
                #目录
                elif os.path.isdir(srcname):
                    copytree(srcname,dstname,symlinks)
                #文件
                else:
                    #复制前删除目录和文件
                    if os.path.isdir(dstname):
                        os.rmdir(dstname)
                    elif os.path.isfile(dstname):
                        os.remove(dstname)
                    shutil.copy2(srcname,dstname)
            except (IOError,os.error) as why:
                errors.append((srcname,dstname,str(why)))
            except OSError as err:
                errors.extend(err.args[0])
            try:
                shutil.copystat(src,dst)
            except WindowsError:
                pass
            except OSError as why:
                errors.extend((src,dst,str(why)))
            if errors:
                raise Exception(errors)
    elif os.path.isfile(src):
        try:
            parentdir = os.path.dirname(src)
            filename = os.path.basename(src)
            srcname = src
            dstname = os.path.join(dst,filename)
            #复制前删除目录和文件
            if os.path.isdir(dstname):
                os.rmdir(dstname)
            elif os.path.isfile(dstname):
                os.remove(dstname)
            shutil.copy2(srcname,dstname)
        except (IOError,os.error) as why:
            errors.append((srcname,dstname,str(why)))
        except OSError as err:
            errors.extend(err.args[0])
        try:
            shutil.copystat(src,dst)
        except WindowsError:
            pass
        except OSError as why:
            errors.extend((src,dst,str(why)))
        if errors:
            raise Exception(errors)






if not isinstance(FCDBDir,unicode):
    FCDBDir = FCDBDir.decode('utf-8')
    
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)
if os.path.exists(FCDBDir):
    if GDBAllPath and len(GDBAllPath)>0:
        if len(GDBAllPath) == 1:
            print "Directory "+FCDBDir+" Has Only One GDB or MDB or Shape WorkeSpace"
            sys.exit(0)
        firstPath = GDBAllPath[0]
        # 判断工作空间一致性
        for i in range(1,len(GDBAllPath)):
            tempPath = GDBAllPath[i]
            if ".gdb" in firstPath:
                if not ".gdb" in tempPath:
                    print "Directory "+FCDBDir+" First Item Is GDB WorkSpace But "+tempPath +" Is Not All GDB WorkeSpace"
                    sys.exit(0)
                    break
            elif ".mdb" in firstPath:
                if not ".mdb" in tempPath:
                   print "Directory "+FCDBDir+" First Item Is MDB WorkSpace But "+tempPath +" Is Not All MDB WorkeSpace"
                   sys.exit(0)
                   break
            else:
                if not os.path.isdir(tempPath):
                    print "Directory "+FCDBDir+" First Item Is SHP WorkSpace But "+tempPath +" Is Not All SHP WorkeSpace"
                    sys.exit(0)
                    break
        #获取上层目录
        parentDir = os.path.dirname(firstPath)
        resultDir = os.path.join(parentDir,"AppendResult")
        try:
            if ".gdb" in firstPath:
                basename = os.path.basename(firstPath)
                resultDir = os.path.join(resultDir,basename)
            copytree(firstPath,resultDir,False)
        except Exception as ex:
            print "copy From "+firstPath+" To "+resultDir +" Failed "+str(ex)
            sys.exit(0)
    else:
        print "Directory "+FCDBDir+" Not Found GDB or MDB Or Shape"


#Get Dataset and FeatureClass,Store in dicAllFC,Key =ds value= fc 如果是游离的shape 和 shape  dataset特殊
if os.path.exists(FCDBDir):
    if GDBAllPath and len(GDBAllPath)>0:
        firstPath = GDBAllPath[0]
        env.workspace = firstPath
        singlefclist = arcpy.ListFeatureClasses("","All")
        if singlefclist and len(singlefclist)>0:
            for singlefc in singlefclist:
                if '.shp' in singlefc:
                    if not dicAllFC.has_key('@shpFC$'):
                        #set表示无序不重复集合
                        dicAllFC.setdefault('@shpFC$',set()).add(singlefc)
                    else:
                        if not singlefc in dicAllFC['@shpFC$']:
                            dicAllFC.setdefault('@shpFC$',set()).add(singlefc)
                else:
                    #list表示无序可重复集合
                    if not dicAllFC.has_key('@singleFC$'):
                        dicAllFC.setdefault('@singleFC$',[]).append(singlefc)
                    else:
                        if not singlefc in dicAllFC['@singleFC$']:
                            dicAllFC.setdefault('@singleFC$',[]).append(singlefc)
        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(firstPath,unicode):
                env.workspace = firstPath+"\\"+dataset
            else:
                env.workspace = firstPath+"\\"+dataset.encode('gb2312')
            fclist = arcpy.ListFeatureClasses("")
            if fclist and len(fclist)>0:
                if not dicAllFC.has_key(dataset):
                    dicAllFC[dataset]=fclist
if dicAllFC:
    firstPath = GDBAllPath[0]
    for (k,v) in dicAllFC.items():
        for singlefc in v:
             # 如果fc是unicode则不做改变,否则将utf-8的fc编码解码成unicode
            if not isinstance(singlefc,unicode):
                singlefc = singlefc.decode('utf-8')
            # 如果ds是unicode则不做改变,否则将utf-8的ds编码解码成unicode   
            if not isinstance(k,unicode):
                k = k.decode('utf-8')
            for singlegdbPath in GDBAllPath:
                if isinstance(singlegdbPath,unicode):
                    fcFullPath =singlegdbPath+"\\"+k+"\\"+singlefc if k!='@shpFC$' and k!='@singleFC$' else singlegdbPath+"\\"+singlefc
                else:
                    fcFullPath = singlegdbPath+"\\"+k.encode('gb2312')+"\\"+singlefc.encode('gb2312') if k!='@shpFC$' and k!='@singleFC$' else singlegdbPath+"\\"+singlefc.encode('gb2312')
                if arcpy.Exists(fcFullPath) and fcFullPath not in fcall:
                    fcall.append(fcFullPath)
                else:
                    print fcFullPath + " Not Exist ****************************************"
            if fcall and len(fcall)>=2:
                targetFC = fcall.pop(0)
                # Append的要素类名称
                fcname = os.path.basename(targetFC)                
                basename = os.path.basename(firstPath)
                parentDir = os.path.dirname(firstPath)
                resultDir = os.path.join(parentDir,"AppendResult")
                if ".gdb" in firstPath or ".mdb" in firstPath:
                    resultDir = os.path.join(resultDir,basename)
                targetFC = resultDir+"\\"+k+"\\"+fcname if k!='@shpFC$' and k!='@singleFC$' else resultDir+"\\"+fcname                    
                try:
                    arcpy.Append_management(fcall,targetFC,"TEST","","");
                    print targetFC+"@@@Succeeded At "+time.strftime("%Y-%m-%d %X",time.localtime())+"###Contains "+str(len(fcall)+1)
                    fcall=[]
                except Exception as inst:
                    if not isinstance(str(inst),unicode):
                        instunicode = str(inst).decode('utf-8')
                        print targetFC+"@@@Failed At "+time.strftime("%Y-%m-%d %X",time.localtime())+"###Contains "+str(len(fcall)+1)+instunicode
                    else:
                        print targetFC+"@@@Failed At "+time.strftime("%Y-%m-%d %X",time.localtime())+"###Contains "+str(len(fcall)+1)+str(inst)
                    fcall=[]
print "Complete At "+time.strftime("%Y-%m-%d %X",time.localtime())

转载自:https://blog.csdn.net/wl05031/article/details/50476534

You may also like...

退出移动版