码迷,mamicode.com
首页 > 数据库 > 详细

删除GIS数据库空层(GDB,MDB,Shape)

时间:2016-01-07 14:51:33      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

批量删除GIS数据库空层。

技术分享
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Merge.py
# Created on: 2013-01-21 10:25:22.00000
#   (generated by WangLin_TJCH)
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy
import os.path
import time
import random
from arcpy import env



FCDBDir = "D:\\aaa"
dicAllFC={}
fcall=[]
GDBAllPath=[]


if not isinstance(FCDBDir,unicode):
    FCDBDir = FCDBDir.decode(utf-8)

#Get Dataset and FeatureClass,Store in dicAllFC,Key =fc value= ds
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)
    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)
                if isinstance(everyfilepath,unicode):
                    fcfullpath = everyfilepath+"\\"+singlefc
                else:
                   fcfullpath = everyfilepath+"\\"+singlefc.encode(gb2312)
                rows = arcpy.SearchCursor(fcfullpath, "", "", "","")
                count = 0
                for featurecount in rows:
                    count=count+1
                if count ==0:
                    arcpy.Delete_management(fcfullpath)
                    print "Delete "+fcfullpath+" Succeed At"+time.strftime("%Y-%m-%d %X",time.localtime())
        datasetlist = arcpy.ListDatasets("","Feature")
        for dataset in datasetlist:
            # 如果dataset是unicode则不做改变,否则将utf-8的dataset编码解码成unicode
            if isinstance(dataset,unicode):
                dataset = dataset
            else:
                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:
                    # 如果fc是unicode则不做改变,否则将utf-8的fc编码解码成unicode
                    if isinstance(fc,unicode):
                        fc = fc
                    else:
                        fc = fc.decode(utf-8)
                    if isinstance(dspath,unicode):
                        fcFullPath = dspath+"\\"+fc
                    else:
                        fcFullPath = dspath+"\\"+fc.encode(gb2312)
                    randomviewname = fc+"view"+str(random.randint(1,100000))
                    arcpy.MakeTableView_management(fc,randomviewname)
                    count =0
                    count = int(arcpy.GetCount_management(randomviewname).getOutput(0))
                    if count ==0:
                        arcpy.Delete_management(fcFullPath)
                        print "Delete "+fcFullPath+" Succeed At"+time.strftime("%Y-%m-%d %X",time.localtime())
else:
    print "Dir Not Exist"
print "Done"
                                
                                
                
                    
    
                            
                    
                        
                        
                
                        
                        
                    
                
View Code

 

删除GIS数据库空层(GDB,MDB,Shape)

标签:

原文地址:http://www.cnblogs.com/514687800/p/5109826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!