TransWikia.com

Reordering fields (permanently) in file geodatabase using ArcGIS Desktop?

Geographic Information Systems Asked by Stephen Lead on November 27, 2020

Using ArcGIS 10.0, is it possible to reorder the fields in a file geodatabase, and have the order persisted to the database?

We considered exporting to XML, and writing a parser to change the field order, then re-creating a new database from the XML. This seems like a laborious process.

Another idea is to import the existing field definitionss into Excel, re-order them, export to CSV, then write a Python script to create these fields in a new Geodatabase. Does a script like this exist, before we write it from scratch?

Unfortunately the ArcGIS Diagrammer doesn’t seem to support field re-ordering.

Are there any scripts or hacks we could use to simplify this process?

14 Answers

As mentioned in the comments you can reorder fields with ArcGIS Diagrammer. I posted a more general step by step in this answer: How to update the length property of a feature class field?

Correct answer by blah238 on November 27, 2020

The free version of ET Geowizards will do this. Basic -> Sort shapes. I just tested it on a v10 File Geodatabase and it works. It has to create a new feature class though.

Answered by pgager on November 27, 2020

In answer to another question I said that it is possible to use Summary Statistics to do the equivalent of PULLITEMS (old speak for reorder fields). That technique works well on Tables only but there is another script available now that says it works on both feature classes and table.

I would also recommend the Answer to Re-ordering fields permanently using ArcGIS Make Query Table tool? which was inspired by @klewis' Answer to this one.

Answered by PolyGeo on November 27, 2020

You need to run MakeTableQuery from the Layers and Table Views toolset to reorder fields. From the documentation, The order of the fields in the field list indicates the order the fields will appear in the output layer or table view. Also, If a Shape column is added to the field list, the result is a layer; otherwise, it is a table view. This is available with any license level.

Answered by klewis on November 27, 2020

So far the most efficient method I have found to rename fields, change their default order, and a number of other things is to use Microsoft Access:

  1. Create new Personal Geodatabase
  2. Drag and drop the feature class or table into the p-gdb
  3. Open in Access and:
    • Ignoring all tables beginning GDB_, open tables in table design mode
    • rename, change order, add new, etc.
  4. Drag and drop back to your real operational gdb

Notes:

Always have a current back up. You're messing directly with internal GDB structure and you could easily trash your data by altering the DB to the point ArcGIS doesn't know how to read it any more.

There are some reserved keywords on the ArcGIS side that can't be used for field names. Access will happily create or rename fields with these keywords, but when you bring them back they automatically have an underscore appended. So Current becomes Current_. Also there note the partially overlapping Access reserved keywords.

Personal GDB have a 2GB file size limit, so the total size of all tables and feature classes within the GDB can't exceed that.

It might be tempting to always use personal GDB's for small projects (<2GB). Don't. Performance is abysmal and there are subtle differences in Access' SQL syntax that make Label Expressions, Definition Queries, and so on non-portable.

Answered by matt wilkie on November 27, 2020

This Python Script is kind of a "duct-tape" method for doing this. It is limited to 20 fields but could easily be added to in order to allow it to run for however many fields your data has.

The script is designed to be run as a script tool, so you would need to setup parameters within ArcCatalog.

The parameters should be as follows:

  • InputFC - Feature Class
  • Output Location - Workspace
  • Output Name - String
  • Field1 - Field
  • Field2 - Field
  • All the way to Field20

Code:

import arcpy, os, sys
from arcpy import env

arcpy.env.overwriteOutput = True

inFC = arcpy.GetParameterAsText(0)
outLoc = arcpy.GetParameterAsText(1)
outName = arcpy.GetParameterAsText(2)
field1 = arcpy.GetParameterAsText(3)
field2 = arcpy.GetParameterAsText(4)
field3 = arcpy.GetParameterAsText(5)
field4 = arcpy.GetParameterAsText(6)
field5 = arcpy.GetParameterAsText(7)
field6 = arcpy.GetParameterAsText(8)
field7 = arcpy.GetParameterAsText(9)
field8 = arcpy.GetParameterAsText(10)
field9 = arcpy.GetParameterAsText(11)
field10 = arcpy.GetParameterAsText(12)
field11 = arcpy.GetParameterAsText(13)
field12 = arcpy.GetParameterAsText(14)
field13 = arcpy.GetParameterAsText(15)
field14 = arcpy.GetParameterAsText(16)
field15 = arcpy.GetParameterAsText(17)
field16 = arcpy.GetParameterAsText(18)
field17 = arcpy.GetParameterAsText(19)
field18 = arcpy.GetParameterAsText(20)
field19 = arcpy.GetParameterAsText(21)
field20 = arcpy.GetParameterAsText(22)

fieldList = ["SHAPE@"]

arcpy.AddMessage(" ")

arcpy.AddMessage("Appending field choices to new List ")

arcpy.AddMessage(" ")

if (field1 != ""):
    fieldList.append(field1)
if (field2 != ""):
    fieldList.append(field2)
if (field3 != ""):
    fieldList.append(field3)
if (field4 != ""):
    fieldList.append(field4)
if (field5 != ""):
    fieldList.append(field5)
if (field6 != ""):
    fieldList.append(field6)
if (field7 != ""):
    fieldList.append(field7)
if (field8 != ""):
    fieldList.append(field8)
if (field9 != ""):
    fieldList.append(field9)
if (field10 != ""):
    fieldList.append(field10)
if (field11 != ""):
    fieldList.append(field1)
if (field12 != ""):
    fieldList.append(field12)
if (field13 != ""):
    fieldList.append(field13)
if (field14 != ""):
    fieldList.append(field14)
if (field15 != ""):
    fieldList.append(field15)
if (field16 != ""):
    fieldList.append(field16)
if (field17 != ""):
    fieldList.append(field17)
if (field18 != ""):
    fieldList.append(field18)
if (field19 != ""):
    fieldList.append(field19)
if (field20 != ""):
    fieldList.append(field20)

arcpy.AddMessage(" ")

#arcpy.AddMessage(fieldList)

oldFieldList = arcpy.ListFields(inFC)

fieldTypes = []

numOfFields = len(fieldList)

fieldIndex = 1

reorderedFields = []

for fld in fieldList:
    for f in oldFieldList:
        if f.name == fld:
            arcpy.AddMessage(f.name)
            reorderedFields.append(f)
            break

arcpy.AddMessage(" ")

arcpy.AddMessage(reorderedFields)

desc = arcpy.Describe(inFC)
geoType = desc.shapeType.upper()
spatRef = arcpy.Describe(inFC).spatialReference

arcpy.CreateFeatureclass_management(outLoc, outName, geoType, "", "", "", spatRef)
newFeat = os.path.join(outLoc, outName)

for flds in reorderedFields:
    if (flds.type == "String"):
        fLength = flds.length
        arcpy.AddField_management(newFeat, flds.name, flds.type, "", "", str(fLength))
    else:
        arcpy.AddField_management(newFeat, flds.name, flds.type)

arcpy.AddMessage(" ")

arcpy.AddMessage(fieldList)

arcpy.AddMessage(" ")

arcpy.AddMessage("Features will be copied with new Schema...")

count = 0

with arcpy.da.SearchCursor(inFC, fieldList) as cursor:
    for row in cursor:
        insertCursor = arcpy.da.InsertCursor(newFeat, (fieldList))
        if (numOfFields == 21):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19], row[20]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]) + ', ' + str(row[19]) + ', ' + str(row[20]))
            count += 1
        elif (numOfFields == 20):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]) + ', ' + str(row[19]))
            count += 1
        elif (numOfFields == 19):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]))
            count += 1
        elif (numOfFields == 18):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]))
            count += 1
        elif (numOfFields == 17):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]))
            count += 1
        elif (numOfFields == 16):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]))
            count += 1
        elif (numOfFields == 15):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]))
            count += 1
        elif (numOfFields == 14):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]))
            count += 1
        elif (numOfFields == 13):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]))
            count += 1
        elif (numOfFields == 12):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]))
            count += 1
        elif (numOfFields == 11):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]))
            count += 1
        elif (numOfFields == 10):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]))
            count += 1
        elif (numOfFields == 9):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]))
            count += 1
        elif (numOfFields == 8):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]))
            count += 1
        elif (numOfFields == 7):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]))
            count += 1
        elif (numOfFields == 6):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]))
            count += 1
        elif (numOfFields == 5):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]))
            count += 1
        elif (numOfFields == 4):
            insertCursor.insertRow((row[0], row[1], row[2], row[3]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]))
            count += 1
        elif (numOfFields == 3):
            insertCursor.insertRow((row[0], row[1], row[2]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]))
            count += 1
        elif (numOfFields == 2):
            insertCursor.insertRow((row[0], row[1]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]))
            count += 1

I realize this isn't the prettiest or most efficient way to do this but it actually works quite nice and only took about half an hour to write. I use this all the time when I need to reorder fields, and it doesn't require Diagrammer or Microsoft Access. Of course the max number of fields can be an issue but again that can be customized within the script.

Answered by GeoJohn on November 27, 2020

The following script can do what you are asking. There are some catches though...

  1. This will not change the original feature class, it makes a copy with reordered fields.
  2. There is a limit of twenty fields. This could be adjusted by adding more lines of code for each additional field beyond twenty.
  3. This is kind of a dirty way of doing things. It isn't very pythonic, efficient, or cool. But it works.
  4. It is designed to work with feature classes(not tables). However, a table could be run with it by removing the line that adds the "SHAPE@" token to the field list.

Script:

import arcpy, os, sys
from arcpy import env

arcpy.env.overwriteOutput = True

inFC = arcpy.GetParameterAsText(0)
outLoc = arcpy.GetParameterAsText(1)
outName = arcpy.GetParameterAsText(2)
field1 = arcpy.GetParameterAsText(3)
field2 = arcpy.GetParameterAsText(4)
field3 = arcpy.GetParameterAsText(5)
field4 = arcpy.GetParameterAsText(6)
field5 = arcpy.GetParameterAsText(7)
field6 = arcpy.GetParameterAsText(8)
field7 = arcpy.GetParameterAsText(9)
field8 = arcpy.GetParameterAsText(10)
field9 = arcpy.GetParameterAsText(11)
field10 = arcpy.GetParameterAsText(12)
field11 = arcpy.GetParameterAsText(13)
field12 = arcpy.GetParameterAsText(14)
field13 = arcpy.GetParameterAsText(15)
field14 = arcpy.GetParameterAsText(16)
field15 = arcpy.GetParameterAsText(17)
field16 = arcpy.GetParameterAsText(18)
field17 = arcpy.GetParameterAsText(19)
field18 = arcpy.GetParameterAsText(20)
field19 = arcpy.GetParameterAsText(21)
field20 = arcpy.GetParameterAsText(22)

fieldList = ["SHAPE@"]

arcpy.AddMessage(" ")

arcpy.AddMessage("Appending field choices to new List ")

arcpy.AddMessage(" ")

if (field1 != ""):
    fieldList.append(field1)
if (field2 != ""):
    fieldList.append(field2)
if (field3 != ""):
    fieldList.append(field3)
if (field4 != ""):
    fieldList.append(field4)
if (field5 != ""):
    fieldList.append(field5)
if (field6 != ""):
    fieldList.append(field6)
if (field7 != ""):
    fieldList.append(field7)
if (field8 != ""):
    fieldList.append(field8)
if (field9 != ""):
    fieldList.append(field9)
if (field10 != ""):
    fieldList.append(field10)
if (field11 != ""):
    fieldList.append(field1)
if (field12 != ""):
    fieldList.append(field12)
if (field13 != ""):
    fieldList.append(field13)
if (field14 != ""):
    fieldList.append(field14)
if (field15 != ""):
    fieldList.append(field15)
if (field16 != ""):
    fieldList.append(field16)
if (field17 != ""):
    fieldList.append(field17)
if (field18 != ""):
    fieldList.append(field18)
if (field19 != ""):
    fieldList.append(field19)
if (field20 != ""):
    fieldList.append(field20)

arcpy.AddMessage(" ")

oldFieldList = arcpy.ListFields(inFC)

fieldTypes = []

numOfFields = len(fieldList)

fieldIndex = 1

reorderedFields = []

for fld in fieldList:
    for f in oldFieldList:
        if f.name == fld:
            arcpy.AddMessage(f.name)
            reorderedFields.append(f)
            break

arcpy.AddMessage(" ")

arcpy.AddMessage(reorderedFields)

desc = arcpy.Describe(inFC)
geoType = desc.shapeType.upper()
spatRef = arcpy.Describe(inFC).spatialReference

arcpy.CreateFeatureclass_management(outLoc, outName, geoType, "", "", "", spatRef)
newFeat = os.path.join(outLoc, outName)

for flds in reorderedFields:
    if (flds.type == "String"):
        fLength = flds.length
        arcpy.AddField_management(newFeat, flds.name, flds.type, "", "", str(fLength))
    else:
        arcpy.AddField_management(newFeat, flds.name, flds.type)

arcpy.AddMessage(" ")

arcpy.AddMessage(fieldList)

arcpy.AddMessage(" ")

arcpy.AddMessage("Features will be copied with new Schema...")

count = 0

with arcpy.da.SearchCursor(inFC, fieldList) as cursor:
    for row in cursor:
        insertCursor = arcpy.da.InsertCursor(newFeat, (fieldList))
        if (numOfFields == 21):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19], row[20]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]) + ', ' + str(row[19]) + ', ' + str(row[20]))
            count += 1
        elif (numOfFields == 20):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]) + ', ' + str(row[19]))
            count += 1
        elif (numOfFields == 19):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]))
            count += 1
        elif (numOfFields == 18):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]))
            count += 1
        elif (numOfFields == 17):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]))
            count += 1
        elif (numOfFields == 16):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]))
            count += 1
        elif (numOfFields == 15):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]))
            count += 1
        elif (numOfFields == 14):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]))
            count += 1
        elif (numOfFields == 13):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]))
            count += 1
        elif (numOfFields == 12):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]))
            count += 1
        elif (numOfFields == 11):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]))
            count += 1
        elif (numOfFields == 10):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]))
            count += 1
        elif (numOfFields == 9):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]))
            count += 1
        elif (numOfFields == 8):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]))
            count += 1
        elif (numOfFields == 7):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]))
            count += 1
        elif (numOfFields == 6):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]))
            count += 1
        elif (numOfFields == 5):
            insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]))
            count += 1
        elif (numOfFields == 4):
            insertCursor.insertRow((row[0], row[1], row[2], row[3]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]))
            count += 1
        elif (numOfFields == 3):
            insertCursor.insertRow((row[0], row[1], row[2]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]))
            count += 1
        elif (numOfFields == 2):
            insertCursor.insertRow((row[0], row[1]))
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]))
            count += 1

You will need to setup the script tool with all of the parameters for each field.

Answered by GeoJohn on November 27, 2020

With the Merge Tool, you can easily reorder fields permanently. It works with tables and feature classes. The reordering can be done through python script and even with the Tool dialog (By removing a field and re-adding it in the dialog). Although re-ordering via the dialog is not a perfect approach.

It is recommended to use Merge tool once and then use Copy As Python Snippet and then manually change the fields orders and then paste the python code in python windows.

Here is a python script that uses the Merge Tool to reorder fields (Copied from here)

import arcpy

def reorder_fields(table, out_table, field_order, add_missing=True):
    """ 
    Reorders fields in input featureclass/table
    :table:         input table (fc, table, layer, etc)
    :out_table:     output table (fc, table, layer, etc)
    :field_order:   order of fields (objectid, shape not necessary)
    :add_missing:   add missing fields to end if True (leave out if False)
    -> path to output table
    """
    existing_fields = arcpy.ListFields(table)
    existing_field_names = [field.name for field in existing_fields]

    existing_mapping = arcpy.FieldMappings()
    existing_mapping.addTable(table)

    new_mapping = arcpy.FieldMappings()

    def add_mapping(field_name):
        mapping_index = existing_mapping.findFieldMapIndex(field_name)

        # required fields (OBJECTID, etc) will not be in existing mappings
        # they are added automatically
        if mapping_index != -1:
            field_map = existing_mapping.fieldMappings[mapping_index]
            new_mapping.addFieldMap(field_map)

    # add user fields from field_order
    for field_name in field_order:
        if field_name not in existing_field_names:
            raise Exception("Field: {0} not in {1}".format(field_name, table))

        add_mapping(field_name)

    # add missing fields at end
    if add_missing:
        missing_fields = [f for f in existing_field_names if f not in field_order]
        for field_name in missing_fields:
            add_mapping(field_name)

    # use merge with single input just to use new field_mappings
    arcpy.Merge_management(table, out_table, new_mapping)
    return out_table

USAGE:

new_field_order = ["field2", "field3", "field1"]
reorder_fields(in_fc, out_fc, new_field_order)

Answered by Farid Cheraghi on November 27, 2020

If you don't have Visio for Diagramer- All of the other answers here breaks any joins. I found this to work well in my case:

  1. Export the schema for the whole db using Arccatalog (Export - XML) - I didnt export the data, schema only
  2. Use some online Pretty Print service to format the xml Open the XML file in a text editor and Reorder the fields as you like
  3. Re-Import the schema to create a new database
  4. Reload the data using the original database as the source

Answered by Ozmo on November 27, 2020

There is an Esri How To: Change the order of field names in an attribute table solution.

It requires editing the XML, but once you get the schema right you are good.

Answered by Wes H. on November 27, 2020

There is a free extension from Esri called X-Ray which contains a tool made to Reorder fields. Has worked great for us and is easy to use.

The X-Ray add-in for ArcCatalog can be used to develop, refine and document your geodatabase designs

Answered by Mark on November 27, 2020

X-ray for ArcCatalog allows field re-ordering. Its an add-in that also allows you to export GDB summaries, and compare schema.

X-Ray for ArcCatalog

Answered by atxgis on November 27, 2020

If you don't mind using Arcpy, there's an easy and effective solution here.

If you used this code, the usage/format would look like this (and this works effectively with almost any ArcMap version including and after 9):

new_field_order = ["field2", "field3", "field1"]
reorder_fields(in_fc, out_fc, new_field_order)

Answered by AlecZ on November 27, 2020

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)

Answered by Luke Savage on November 27, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP