ArcPy遍历Dataset中的FeatureClass添加GLOBALID字段

某项目的要素类按不同的数据集存放,现需为每个要素类添加一个唯一标识符字段(若已有则跳过)。代码如下:

env.workspace = r'D:\RK20180201.gdb'
print env.workspace
dss = arcpy.ListDatasets()
for ds in dss:
    fcs = arcpy.ListFeatureClasses(feature_dataset = ds)
    print fcs
    for fc in fcs:
        print(u'------current feature class : {0}'.format(fc))
        fds = arcpy.ListFields(fc)
        bGid = False
        for fd in fds:
            print(u'    -- field : {0}'.format(fd.name))
            if fd.name.upper() == 'GLOBALID':
                bGid = True
                print('    -- {0} has GID field'.format(fc))
        if False == bGid:
            print('    ++ {0} has no GID field'.format(fc))
            arcpy.AddField_management(fc,"GLOBALID","TEXT",None,None,40,"唯一标识符","NULLABLE","NON_REQUIRED")
            

注意,测试数据的FeatureClass的字段由于使用了中文名称(是名称,不是别名),故而在print的时候,要指定字符集,即添加u开头。
转载自:https://blog.csdn.net/a_dev/article/details/80108877

You may also like...