【ArcGIS】修改mdb/gdb要素类字段顺序脚本

一、修改思路

  1. 将需要修改字段顺序要素类的所有字段信息导出,形成字段信息文本文件
  2. 打开字段信息文本,手动添加字段顺序
  3. 新建空白要素类,读取字段信息文本,按照字段顺序新建与原数据一致的字段。字段新建完毕后,将数据逐个复制到新建的要素类中。

二、脚本实现

2.1 导出字段信息

# s1_exportField.py

# -*- coding: utf-8 -*-
import argparse, sys
import arcpy

reload(sys)
sys.setdefaultencoding('utf8')

parser = argparse.ArgumentParser(description='Input argument')
parser.add_argument('--featureClassPath', '-fc', help='featureClassPath: 必要参数,待修改字段顺序要素类', required=True)
parser.add_argument('--fieldExportPath', '-fe', help='fieldExportPath:必要参数,要素类字段保存位置,默认值为fields.txt', required=True, default='./fields.txt')
args = parser.parse_args()

try:
    fields = arcpy.ListFields(args.featureClassPath)
    with open(args.fieldExportPath, "w") as wf:
        for field in fields:
            if field.name not in ["Shape", "OBJECTID", "OID", "Shape_Area", "Shape_Length"]:
                line = field.name + ", " + field.aliasName + ", " + field.type + ", " + str(field.length) + ", " + str(field.precision) + ", " + str(field.scale) + ", \n"
                wf.write(line)
except Exception as e:
    print(e)

2.2 读取字段信息,新建字段并复制要素

# s2_adjustFieldOrder.py

# -*- coding: utf-8 -*-
import argparse, os, sys
import arcpy

reload(sys)
sys.setdefaultencoding('utf8')
arcpy.env.overwriteOutput = True

parser = argparse.ArgumentParser(description='Input argument')
parser.add_argument('--featureClassPath', '-fc', help='featureClassPath: 必要参数,待修改字段顺序要素类', required=True)
parser.add_argument('--fieldSavePath', '-fs', help='fieldSavePath:必要参数,要素类字段保存位置', required=True)
args = parser.parse_args()

try:
    workspace = os.path.split(args.featureClassPath)[0]
    arcpy.env.workspace = workspace

    fc_new_name = os.path.split(args.featureClassPath)[1] + '_new'
    geometry_type = "POLYGON"
    template = ""
    has_m = "DISABLED"
    has_z = "DISABLED"
    spatial_reference = arcpy.Describe(args.featureClassPath).spatialReference

    arcpy.CreateFeatureclass_management(workspace, fc_new_name, geometry_type, template, has_m, has_z, spatial_reference)

    fields = []
    with open(args.fieldSavePath, "r") as rf:
        for line in rf:
            line = line.strip()
            line = line.split(", ")
            field_name = line[0]
            field_aliasName = line[1]
            field_type = line[2]
            filed_length = int(line[3])
            filed_precision = int(line[4])
            field_scale = int(line[5])
            field_order = float(line[6])
            fields.append([field_name, field_aliasName, field_type, filed_length, filed_precision, field_scale, field_order])
        fields = sorted(fields, key=lambda x: x[6])

    for field in fields:
        field_name = field[0]
        field_aliasName = field[1]
        field_type = field[2]
        field_length = field[3]
        field_precision = field[4]
        field_scale = field[5]

        field_type = field_type.upper()
        if field_type=="STRING":
            field_type="TEXT"
        elif field_type=="SMALLINTEGER":
            field_type="SHORT"
        elif field_type=="INTEGER":
            field_type="LONG"
        
        if field_precision == 0:
            field_precision = ""
        if field_scale == 0:
            field_scale = ""
        
        if field_type=="FLOAT" or field_type=="DOUBLE":
            arcpy.AddField_management(fc_new_name, field_name, field_type, field_precision, field_scale, "",field_aliasName)
        elif field_type=="TEXT":
            arcpy.AddField_management(fc_new_name, field_name, field_type, "", "", field_length, field_aliasName)
        else:
            arcpy.AddField_management(fc_new_name, field_name, field_type, "", "", "", field_aliasName)


    arcpy.Append_management(args.featureClassPath, fc_new_name, "NO_TEST")

except Exception as e:
    print(e)

三、使用方法

两段脚本分别保存为s1_exportField.pys2_adjustFieldOrder.py
首先,运行s1_exportField.py,读取待排序要素类test,生成字段文件test.txt。

python s1_exportField.py -fc ./test.mdb/test -fe test.txt

接着,打开test.txt,按照需要,在字段文件每一行末尾添加数字,数字大小代表字段顺序。
最后,运行s2_adjustFieldOrder.py,读取编辑过的字段文件,自动生成带有重新排序字段的新要素类,同时将原要素类内的所有要素复制到新要素类中。

python s2_adjustFieldOrder.py -fc ./test.mdb/test -fs test.txt
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值