18

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?

10 Answers 10

6

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?

  • Thanks. The ArcGIS Diagrammer is a tiny bit unstable - it doesn't exactly crash, but it feels home-made sometimes. But for modifying the schema, it seems like the best option. – Stephen Lead Sep 18 '12 at 10:40
  • Might now work past 10.0. It installed ok for me on Win7 x64 with ArcGIS 10.1, but when run it spews Just In Time compilation errors and quits. – matt wilkie May 3 '13 at 21:33
  • 1
    It should be important to note that ArcGIS Diagrammer no longer functions with ArcGIS 10.3 or higher. – Map Man Oct 31 '17 at 13:10
7

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.

6

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.

4

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.

  • Just wondering if you have tested this because I cannot see a way for the Make Query Table tool to re-order the fields - its tool dialog seems to just let you decide whether fields are to be included or not. – PolyGeo Aug 27 '12 at 0:12
  • Yes, I have tested this from a Python script. The tool seems to be designed to combine or join multiple tables. However, I am able to create a new table or feature class with reordered fields by inputing a single feature class. – klewis Aug 27 '12 at 16:38
  • I must be missing something so have posted a new question here gis.stackexchange.com/questions/32310/… - can you perhaps detail the steps there for me, please? – PolyGeo Aug 28 '12 at 5:27
  • I just answered the new question by using not just MakeQueryTable but also CopyFeatures and some minor editing of code I got using Copy As Python Snippet into the Python window. – PolyGeo Aug 28 '12 at 21:59
4

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
  • I dismissed that in the original question as "a laborious process" - but actually when you put it like that, it doesn't sound too bad ;) – Stephen Lead May 25 '16 at 23:08
4

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)
  • That seems to work well from the tool dialog where I first deleted a field that I wanted to move to be last, added a new field and then did Add Input Field. However, as you said, using it in Python is easier than via the tool when more complex re-ordering is needed. – PolyGeo Aug 2 '15 at 8:12
3

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

  • welcome to GIS Stack Exchange, and thanks for the tip. I re-formatted your answer to be more in line with Stack Exchange standards - I included a link and a quote about the product, and removed extraneous info. – Stephen Lead Feb 7 '17 at 23:52
2

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.

  • of course, this method is not officially sanctioned, and if something goes wrong you won't be able to call Tech Support ;) – Stephen Lead Dec 9 '14 at 3:32
2

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.

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.