#!/usr/bin/python
# -*- coding:utf-8 -*-

import os
import arcpy
import json
import sys
reload(sys)
sys.setdefaultencoding('utf8')


#Author By K


gdblist=[] #新建空白列表 
file_dir=r"C:\Users\Administrator\Desktop\test" #输入待合并GDB数据库所在的文件夹的路径
dirlist=os.listdir(file_dir) #获取 C:\\Desktop 下一层级目录的路径
for dir in dirlist: #循环dirlist列表
    if ".gdb" in dir: #如果路径名称中包含 .gdb 字符串
        gdblist.append(file_dir+"\\"+dir) #将待合并GDB数据库的路径添加到gdblist列表中
print(gdblist)
print("gdb number is:" +str(len(gdblist)))


for gdb in gdblist: #循环待合并GDB数据库

    arcpy.env.workspace = gdb #将循环到的数据库作为工作空间

    datasets = arcpy.ListDatasets() #列出该数据库包含的要素数据集

#考虑GDB中含要素集  
  
    if(len(datasets)!=0):
        for ds in datasets: #循环要素数据集
            print("------" + ds)  # 打印该要素数据集的名称
            fcs=arcpy.ListFeatureClasses(feature_dataset=ds) #获取该要素数据集下的所有要素类

            filePath= fileName + "\\" + str(ds)
            print(filePath)
            for fc in fcs: #循环该要素数据集下的所有要素类
                print("-------" + fc)  # 打印要素类名称便于查看进度
                cnt=arcpy.GetCount_management(fc).getOutput(0) #获取该要素类的要素总个数
                print fc," feature number is ",cnt

#考虑GDB中不含要素集只有要素类
    else:
        print"feature_dataset is None! "   # 打印无要素集说明

        fcs=arcpy.ListFeatureClasses() #直接获取GDB下的所有要素类
        for fc in fcs: #循环所有要素类
            print("-------" + fc)  # 打印要素类名称便于查看进度
            cnt=int(arcpy.GetCount_management(fc).getOutput(0)) #获取该要素类的要素总个数
            print fc," feature number is ",cnt

#遍历要素类的字段名
            fcfields =arcpy.ListFields(fc)
            for fcfield in fcfields:
#                print(fc,":","{0} is a type of {1} with a length of {2} and aliasname is {3} "
#                    .format(fcfield.name, fcfield.type, fcfield.length,fcfield.aliasName))
                fielddefinition = (fc,":","{0} is a type of {1} with a length of {2} and aliasname is {3} "
                    .format(fcfield.name, fcfield.type, fcfield.length,fcfield.aliasName))


#输出为txt           
                with codecs.open(file_dir + "/" + "DataStructureTable" +".txt", "a",encoding='utf-8') as fd:
                    fd.write(json.dumps(fielddefinition,encoding='UTF-8', ensure_ascii=False) +'\n')

参考以下文章

https://blog.csdn.net/weixin_38415513/article/details/104762342
https://blog.csdn.net/gognzixiaobai666/article/details/113864222
https://www.cnblogs.com/pengwue/p/9382741.html
http://zhihu.geoscene.cn/article/4062
 

抛砖引玉,希望有大佬能优化下,将结果导出至excel。

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐