本文主要是介绍python27获取gdb里所有要素类及字段和别名,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#!/usr/bin/python
# -*- coding:utf-8 -*-import os
import arcpy
import json
import sys
reload(sys)
sys.setdefaultencoding('utf8')#Author By Kgdblist=[] #新建空白列表
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。
这篇关于python27获取gdb里所有要素类及字段和别名的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!