ArcPy导出GDB数据库要素类字段信息

ArcPy导出GDB数据库要素类字段信息


近期在做一个数据处理项目时,写总结报告需要把GDB库体的数据库结构给写进去,本着 “偷懒” 的原则,花时间写了个ArcPy脚本,几分钟就搞定了(主要用到了arcpy库和xlwt库),在这里记录一下。

效果预览

导出表格如下图所示,清晰罗列了字段名称、别名、类型、长度等信息。

实现代码

将输入GDB文件路径和输出EXCEL表格的路径修改之后,就可以正常运行了。输出的字段可以根据自己的需求进行优化调整。

# -*- coding: utf-8 -*-
import os
import sys
import time
from datetime import datetime
import arcpy
import xlwt

reload(sys)
sys.setdefaultencoding("utf-8")

def makedirs_file(input_path):
    """
    检查文件夹是否存在,否则创建文件夹
    @param input_path: 文件或文件夹名称
    @return:
    """
    if os.path.isdir(input_path):
        if os.path.exists(input_path) is False:
            os.makedirs(input_path)
    else:
        input_path = os.path.dirname(input_path)
        if os.path.exists(input_path) is False:
            os.makedirs(input_path)


if __name__ == '__main__':
    # 设置文件路径和工作区间
    path_gdb = r'F:\gisData\慈溪变更数据\基础数据\地类图斑.gdb'
    # 输出表格的路径,注意: 表格的名称最好不要包含中文,否则可能会报错
    time_str = str(datetime.now().strftime("%Y%m%d%H%M"))
    excelpath = './results/field-info{}.xls'.format(time_str)
    # 创建输出文件夹
    makedirs_file(excelpath)
    if not arcpy.Exists(path_gdb):
        raise IOError(u"错误,未发现GDB数据库文件!")
    arcpy.env.workspace = path_gdb
    # 程序运行开始时间
    startTime = time.time()
    # 创建Excel
    xls = xlwt.Workbook(encoding='utf-8')
    # 设置工作表的名称
    sht1 = xls.add_sheet(u'字段信息表')

    fieldList = [u'序号', u'字段序号', u'要素类名称', u'字段名称', u'别称', u'字段类型', u'字段长度', u'要素类型',
                 u'要素路径', u'坐标系']
    for column in range(len(fieldList)):
        sht1.write(0, column, fieldList[column])
    fcs = arcpy.ListFeatureClasses()
    if len(fcs) < 1:
        raise IOError(u"错误,未发现矢量文件!")
    row = 0
    for featureClass in fcs:
        print ("current deal ---->" + featureClass)
        fieldList = arcpy.ListFields(dataset=featureClass)

        fieldLen = len(fieldList)
        for index in range(fieldLen):
            field = fieldList[index]
            # 字段名称
            field_name = field.name
            # 无效名称字段
            list_field_filter = ['Shape', 'SHAPE','OBJECTID', 'OBJECTID_1','OBJECTID_2', 'Shape_Length', 'Shape_Area', '']
            if field_name in list_field_filter:
                continue
            # 要素路径
            featureclasspath = os.path.join(path_gdb, featureClass)
            # 序号
            row += 1
            sht1.write(row, 0, row)
            # 字段序号
            sht1.write(row, 1, "字段{}".format(index + 1))
            # 要素名称
            sht1.write(row, 2, featureClass.encode('utf-8'))
            # 字段名称
            sht1.write(row, 3, field_name)
            # 字段别称
            sht1.write(row, 4, field.aliasName)
            # 字段类型
            sht1.write(row, 5, field.type)
            # 字段长度
            sht1.write(row, 6, field.length)
            # 要素几何类型
            sht1.write(row, 7, arcpy.Describe(featureclasspath).shapetype)
            # 要素路径
            sht1.write(row, 8, str(featureclasspath.encode('utf-8')))
            # 坐标系
            sht1.write(row, 9, arcpy.Describe(featureclasspath).spatialReference.name)
    try:
        print(u'开始保存到Excel中...')
        xls.save(excelpath)
        print(u'程序运行结束,字段信息已经保存Excel中:' + excelpath)
    except Exception, arg:
        raise ValueError("错误,导出失败:"+arg.message)
    # end
    endTime = time.time()
    print ("The program run time is : %.02f seconds" % (endTime - startTime))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值