ArcPy脚本——优化版删除GIS数据库空层(支持GDB,mdb,shp文件夹)
之前有人写了一个批量删除gdb,mdb和shp当中数据为空的图层的脚本,原文参考:https://blog.csdn.net/wl05031/article/details/50476251
使用过程中发现,某些mdb中的空图层无法删除,该mdb并非是由arcgis创建的。分析mdb发现,使用arcgis打开数据库虽然能够看到该图层,但是在“GDB_Items”表当中并没有该图层,造成使用ListFeatureClasses读取图层列表的时候获取不到该图层,从而也就无法进行下面的删除操作了。针对这种情况,增加了一个手动输入图层的选项,强制指向目录,即可进行下一步的操作了。对于手动收入的图层,工具只会对空图层进行处理,对于有数据的图层是不会执行删除操作的。
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Created on: 2018-09-29
# (generated by GL:qq,695396984)
# Description:
# ---------------------------------------------------------------------------
# Import arcpy module
import arcpy
import os.path
import time
import random
from arcpy import env
FCDBDir = arcpy.GetParameterAsText(0)
Lyrs = arcpy.GetParameterAsText(1)
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()
singlefclist2 = Lyrs.split(';')
#执行对手动输入数据的操作
if singlefclist2 and len(singlefclist2)>0:
for singlefc2 in singlefclist2:
#arcpy.AddMessage("custom layer:"+singlefc2)
# 如果singlefc是unicode则不做改变,否则将utf-8的singlefc编码解码成unicode
if not isinstance(singlefc2,unicode):
singlefc2 = singlefc2.decode('utf-8')
if isinstance(everyfilepath,unicode):
fcfullpath = everyfilepath+"\\"+singlefc2
else:
fcfullpath = everyfilepath+"\\"+singlefc2.encode('gb2312')
if arcpy.Exists(fcfullpath):
count=arcpy.GetCount_management(fcfullpath)
#arcpy.AddMessage("table:"+fcfullpath+",count:"+str(count))
if int(str(count))==0:
arcpy.AddMessage("table:"+fcfullpath+",count:"+str(count))
arcpy.Delete_management(fcfullpath)
arcpy.AddMessage("Delete CustomLayer "+fcfullpath+" Succeed At"+time.strftime("%Y-%m-%d %X",time.localtime()))
print "Delete "+fcfullpath+" Succeed At"+time.strftime("%Y-%m-%d %X",time.localtime())
if singlefclist and len(singlefclist)>0:
for singlefc in singlefclist:
#arcpy.AddMessage("featurename:"+singlefc)
# 如果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')
count=arcpy.GetCount_management(fcfullpath)
#rows = arcpy.SearchCursor(fcfullpath, "", "", "","")
#count = 0
#for featurecount in rows:
#count=count+1
if int(str(count))==0:
arcpy.Delete_management(fcfullpath)
arcpy.AddMessage("Delete "+fcfullpath+" Succeed At"+time.strftime("%Y-%m-%d %X",time.localtime()))
print "Delete "+fcfullpath+" Succeed At"+time.strftime("%Y-%m-%d %X",time.localtime())
datasetlist = arcpy.ListDatasets("","Feature")
for dataset in datasetlist:
arcpy.AddMessage(dataset)
# 如果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))
arcpy.AddMessage(str(count)+fc)
if count ==0:
arcpy.Delete_management(fcFullPath)
arcpy.AddMessage("Delete "+fcFullPath+" Succeed At"+time.strftime("%Y-%m-%d %X",time.localtime()))
print "Delete "+fcFullPath+" Succeed At"+time.strftime("%Y-%m-%d %X",time.localtime())
else:
arcpy.AddMessage("Dir Not Exist")
arcpy.AddMessage("Done")
转载自:https://blog.csdn.net/geliang0021/article/details/82900398