【ArcGIS】修改mdbgdb要素类字段顺序脚本

【ArcGIS】修改mdbgdb要素字段顺序脚本
⼀、修改思路
1. 将需要修改字段顺序要素类的所有字段信息导出,形成字段信息⽂本⽂件
2. 打开字段信息⽂本,⼿动添加字段顺序
3. 新建空⽩要素类,读取字段信息⽂本,按照字段顺序新建与原数据⼀致的字段。字段新建完毕后,将数据逐个复制到新建的要素类
中。
⼆、脚本实现
2.1 导出字段信息
# s1_exportField.py
# -*- coding: utf-8 -*-总砷
import argparse, sys
witsimport 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:必要参数,要素类字段保存位置,默认值为', required=True, default='./ t')
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 +", "+ pe+", "+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')
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]
fc_new_name = os.path.split(args.featureClassPath)[1]+'_new'
geometry_type ="POLYGON"
tygygeometry_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.py和s2_adjustFieldOrder.py。
⾸先,运⾏s1_exportField.py,读取待排序要素类test,⽣成字段⽂件。
python s1_exportField.py -fc ./test.mdb/test -
接着,打开,按照需要,在字段⽂件每⼀⾏末尾添加数字,数字⼤⼩代表字段顺序。
最后,运⾏s2_adjustFieldOrder.py,读取编辑过的字段⽂件,⾃动⽣成带有重新排序字段的新要素类,同时将原要素类内的所有要素复制到新要素类中。
python s2_adjustFieldOrder.py -fc ./test.mdb/test -

本文发布于:2024-09-22 12:25:39,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/24837.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:要素   顺序   字段
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议