Would someone be able to share the logistics on how to permanently programmatically reorder fields in a feature class with ArcObjects, VB.Net.
I want to be able to do this in the geodatabase not in the IOrderedLayerFields Interface of the Carto assembly.
-
2I think it's the same as with geoprocessing, you need to drop and re-create the table/feature class completely. Is that acceptable?blah238– blah2382013年01月25日 19:51:51 +00:00Commented Jan 25, 2013 at 19:51
-
I was hoping for a more "elegant" solution.Jakub Sisak GeoGraphics– Jakub Sisak GeoGraphics2013年01月25日 19:58:59 +00:00Commented Jan 25, 2013 at 19:58
-
I don't think there is one, but people have certainly asked for this before: ideas.arcgis.com/ideaView?id=0873000000087wgblah238– blah2382013年01月25日 20:41:01 +00:00Commented Jan 25, 2013 at 20:41
-
1Related: How to reorder fields (permanently) in a file geodatabaseblah238– blah2382013年01月25日 20:43:07 +00:00Commented Jan 25, 2013 at 20:43
-
They gave us "rename field" in 10.1 but not reordering. Maybe in 10.2 and 12,000ドル in maintenance fees later. I can manage the "drop and re-create" solution. I guess we can close this post.Jakub Sisak GeoGraphics– Jakub Sisak GeoGraphics2013年01月25日 21:27:12 +00:00Commented Jan 25, 2013 at 21:27
4 Answers 4
Even with ArcObjects, the only way to permanently reorder fields in a table or feature class is to drop the table completely and re-create it. There is a very popular ArcGIS Idea to get this implemented, so vote for it if you want to see such a feature.
This is possible in python using FeatureClasstoFeatureClass with Fieldmappings. You can also rename fields at the same time.
So if you have a Featureclass with FIELD3,FIELD2,FIELD1 and you want the result to be FIELD1,F2,F3 then the following code should accomplish this.
arcpy.env.overwriteOutput = True
input_fpath = "c:\\gis\\Geodatabases\\temp.gdb\\in_fc"
output_dpath = "c:\\gis\\Geodatabases\\temp.gdb"
output_fname = "out_fc"
fms = arcpy.FieldMappings()
fm = arcpy.FieldMap()
fm.addInputField(input_fpath,"FIELD1")
fms.addFieldMap(fm)
fm = arcpy.FieldMap()
fm.addInputField(input_fpath,"FIELD2")
of = fm.outputField
of.name = "F2"
of.aliasName = "F2"
fm.outputField = of
fms.addFieldMap(fm)
fm = arcpy.FieldMap()
fm.addInputField(input_fpath,"FIELD3")
of = fm.outputField
of.name = "F3"
of.aliasName = "F3"
fm.outputField = of
fms.addFieldMap(fm)
arcpy.conversion.FeatureClassToFeatureClass(input_fpath,output_dpath,output_fname,"",fms)
I know this is totally out of the scope of the question, but could be useful for those who want to reorder fields inside a table only once by hand. You can do the trick by importing the classes (or the whole geodatabase) into a personal geodatabase. Then open it with Access and reorder the fields (yeah, cool) and export it back to the original format.
Hope it helps
-
I like this out of the box solution. This worked really well for me.Fezter– Fezter2015年10月27日 01:26:00 +00:00Commented Oct 27, 2015 at 1:26
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)