To reorder GIS fields that have like fields per feature class and table so those fields are reordered in front of all the rest of the fields. This works on the complete dataset. Make sure fields exist and are aliased properly.
Import Libraries
import arcpy
import sys
Declare Workspace
path="D:/temp"
dbname="/gis"
arcpy.env.workspace=path+dbname+'.gdb'
demogis=arcpy.env.workspace
newdb=path+dbname+'_New.gdb'
print(path+dbname+'.gdb')
print(demogis)
print (newdb)
Reorder Fields
Create New File Geodatabase and Feature Datasets
arcpy.management.Delete(path+dbname+'_New'+'.gdb')
arcpy.management.CreateFileGDB(path,dbname + '_New')
try:
for ds in arcpy.ListDatasets():
output_dpath = (path+dbname + '_New' + '.gdb')
arcpy.CreateFeatureDataset_management(output_dpath, ds, ds)
except Exception:
e = sys.exc_info()[1]
print(e.args[0], ds)
Reorder Field Mapping in Feature Classes
'pfields' list needs to be updated to fields and alias field names in order. This will set order for the fields within the feature classes. Fields must exist in feature class.
pfields=[('AssetID','Asset Identifier'),('Display','Display Name'),('OpStatus','Operational Status'),('LifeCycleStatus','Lifecycle Status'),('Location','Location Description'),('Address','Address'),('OwnedBy','Owned By'),('MaintBy','Maintained By'),('PrimaryImage','Primary Image'),('CondInspScore','Condition Score'),('CondInspDate','Condition Date'),('WarrantyDate','Warranty Date'),('InstallDate','Install Date'),('InstallCost','Install Cost'),('ExpReplaceDate','Exp Replace Date'),('ExpReplaceCost','Exp Replace Cost'),('Age','Age'),('Criticality','Criticality'),('iPoF','Failure Probability'),('iCoF', 'Failure Consequence'),('iBRE','Business Risk'),('iMSPU','Maint Under Perform'),('iMSPO','Maint Over Perform'),('iMSR','Maint Score'),('iMCFY','Maint Curve Fast Yr'),('iMCSY','Maint Curve Slow Yr'),('iMCAY','Maint Curve Avg Yr'),('iMSName','Maint Strategy Name'),('iCalcDate','OpInsights Calc Date'),('IndFacID','Indoors Facility Identifier'),('IndLevID','Indoors Level Identifier'),('Comments','Comments')]
datasets = arcpy.ListDatasets(feature_type='feature')
datasets = [''] + datasets if datasets is not None else []
for ds in datasets:
for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
pfield=[item[0] for item in pfields]
palias=[item[1] for item in pfields]
desc = arcpy.Describe(fc)
output_dpath = (path+dbname + '_New' + '.gdb'+'/'+ds)
input_fpath = demogis+'/'+ds+'/'+fc
output_fname = fc
fms = arcpy.FieldMappings()
fcall = [(f.name,f.aliasName) for f in arcpy.ListFields(demogis+'/'+ds+'/'+fc) if f.editable]
ffield = [item[0] for item in fcall]
falias=[item[1] for item in fcall]
fields2Add = list(set(fcall) - set(pfields))
try:
for pfield,palias in pfields:
fm = arcpy.FieldMap()
fm.addInputField(input_fpath,pfield)
of = fm.outputField
of.name = pfield
of.aliasName = palias
fm.outputField = of
fms.addFieldMap(fm)
except Exception:
e = sys.exc_info()[1]
print(e.args[0], fc,pfield)
try:
for ffield,falias in fields2Add:
if ffield != "SHAPE" and ffield != 'Shape':
fm = arcpy.FieldMap()
fm.addInputField(input_fpath,ffield)
of = fm.outputField
of.name = ffield
of.aliasName = falias
fm.outputField = of
fms.addFieldMap(fm)
except Exception:
e = sys.exc_info()[1]
print(e.args[0], fc,ffield)
arcpy.conversion.FeatureClassToFeatureClass(input_fpath,output_dpath,output_fname,"",field_mapping=fms)
Reorder Field Mapping in Tables
'pfields' list needs to be updated to fields and alias field names in order. This will set order for the fields within the tables. Fields must exist in tables.
pfields=[('AssetID','Asset Identifier'),('Display','Display Name'),('OpStatus','Operational Status'),('LifeCycleStatus','Lifecycle Status'),('Location','Location Description'),('Address','Address'),('OwnedBy','Owned By'),('MaintBy','Maintained By'),('PrimaryImage','Primary Image'),('CondInspScore','Condition Score'),('CondInspDate','Condition Date'),('WarrantyDate','Warranty Date'),('InstallDate','Install Date'),('InstallCost','Install Cost'),('ExpReplaceDate','Exp Replace Date'),('ExpReplaceCost','Exp Replace Cost'),('Age','Age'),('Criticality','Criticality'),('iPoF','Failure Probability'),('iCoF','Failure Consequence'),('iBRE','Business Risk'),('iMSPU','Maint Under Perform'),('iMSPO','Maint Over Perform'),('iMSR','Maint Score'),('iMCFY','Maint Curve Fast Yr'),('iMCSY','Maint Curve Slow Yr'),('iMCAY','Maint Curve Avg Yr'),('iMSName','Maint Strategy Name'),('iCalcDate','OpInsights Calc Date'),('IndFacID','Indoors Facility Identifier'),('IndLevID','Indoors Level Identifier'),('Comments','Comments')]
tables = arcpy.ListTables()
for tb in tables:
output_dpath = (path+dbname + '_New' + '.gdb')
input_fpath = demogis+'/'+tb
pfield=[item[0] for item in pfields]
palias=[item[1] for item in pfields]
output_fname = tb
fms = arcpy.FieldMappings()
fcall = [(f.name,f.aliasName) for f in arcpy.ListFields(demogis+'/'+tb) if f.editable]
ffield = [item[0] for item in fcall]
falias=[item[1] for item in fcall]
fields2Add = list(set(fcall) - set(pfields))
try:
for pfield,palias in pfields:
fm = arcpy.FieldMap()
fm.addInputField(input_fpath,pfield)
of = fm.outputField
of.name = pfield
of.aliasName = palias
fm.outputField = of
fms.addFieldMap(fm)
except Exception:
e = sys.exc_info()[1]
print(e.args[0], tb,pfield)
try:
for ffield,falias in fields2Add:
if ffield != "SHAPE" and ffield != 'Shape':
fm = arcpy.FieldMap()
fm.addInputField(input_fpath,ffield)
of = fm.outputField
of.name = ffield
of.aliasName = falias
fm.outputField = of
fms.addFieldMap(fm)
except Exception:
e = sys.exc_info()[1]
print(e.args[0], tb,ffield)
arcpy.conversion.TableToTable(input_fpath,output_dpath,output_fname,"",field_mapping=fms)