Geographic Information Systems Asked by Jakub Sisak GeoGraphics on March 4, 2021
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.
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.
Correct answer by blah238 on March 4, 2021
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:gisGeodatabasestemp.gdbin_fc"
output_dpath = "c:gisGeodatabasestemp.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)
Answered by Tom Roussell on March 4, 2021
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
Answered by namenotallowed on March 4, 2021
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)
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)
Answered by Luke Savage on March 4, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP