Automate Field Creation

232
9
07-25-2011 07:56 AM
Highlighted
New Contributor III
I'm trying to figure out a way to easily create hundreds of fields across different feature classes in the same geodatabase. I have a break down of the schema (field type, name, scale, precision, etc.) and just need to find a way to automate the entire process. I tried creating an xml file via an xml map using excel, but it won't allow you to create nested xml files. I next tried using model builder (see attached graphic) to get the field value from an Access Database table and use the value to Add a field to the appropriate Feature class but for some reason it doesn't seem to want to accept all of the values that are being passed in from the table. I'm hoping someone can help me figure out an easy solution, or can point out what I'm doing wrong. Thanks in advance!
0 Kudos
9 Replies
Highlighted
Esri Esteemed Contributor
You can create a script to do this.  Below is an example:

import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.mdb"

inputTable = "XY"

lstFields = arcpy.ListFields(inputTable, "Lat")

for field in lstFields:
    name = field.name
    precision = field.precision
    length = field.length
    fieldtype = field.type
    scale = field.scale
    domain = field.domain
    alias = field.aliasName

env.workspace = r"C:\temp\python\Test2.mdb"

lstTables = arcpy.ListTables("*")

for table in lstTables:
    arcpy.AddField_management(table, name, fieldtype, precision, scale, length, alias, "", "", domain)

print "Successfully added field"
 

The code exports the name, precision, etc from a table called 'XY' in geodatabase Test1 and then adds a new field to each table in geodatabase Test2.
0 Kudos
Highlighted
New Contributor III
Thanks for the sample code. It seems like the code that you provided applies a set of attributes to every feature class in a single geodatabase. What I'm working with is a series of tables in an Access database that correspond to specific feature classes in a geodatabase which contains all of the feature classes for a model, but only contains the default attributes (OID, Shape, etc.) Each table in the Access database contains all of the attributes that are to be applied to different feature classes in the geodatabase. Each row in a single table contains all of the information for a single attribute such as name, domain, alias, etc. What I was hoping to be able to do was to utilize a FOR loop in model builder to iterate through one table and create all of the fields that belong to a particular feature class. Is this not possible in model builder? Thanks again.
0 Kudos
Highlighted
Esri Esteemed Contributor
To create the FOR loop you would need to do this in IDLE or pythonwin.  Once you have the script created, you can import the script to your Toolbox and execute it from ArcMap/ArcCatalog.
0 Kudos
Highlighted
New Contributor III
I'm able to loop through all of the rows in the specified table using the For Iteration tool in model builder, it just doesn't seem to want to apply to values as parameters in the Add Field tool.
0 Kudos
Highlighted
Esri Esteemed Contributor
I would post this question on the Python forum.  You will be able to receive more intuitive help from that forum.
0 Kudos
Highlighted
New Contributor III
Thanks for all of your quick responses!
0 Kudos
Highlighted
Occasional Contributor II
Nathan,

We had a session on this topic at the User Conference this year and have written a sample script tool that will do what you are looking for.  See the example attached. 

This will end up on the resource center in the near future and we'll also be making some blog posts on the topic of geodatabase schema creation via model builder/python. 

Have you been working to build your schema via geoprocessing?  How large of a geodatabase have you built?

Feel free to email me to share your experiences with this workflow at iwittenmyer@esri.com

Ian
0 Kudos
Highlighted
New Contributor
I have a question that looks to be similar. I want to automate the creation of 23 fields (so far). 4 of the fields will get duplicated 5 times but with a increment number in the name (ie A_NAME, A2_Name, A3_NAME, etc). Each of the 4 fields have different parameters. The last three fields will just have to be created once at the end. In the end I want to have a copy of the layer. I've been playing around with creating each step in the process. Is there a way to group the process and save from adding the add field tool 23 times?

Thanks
0 Kudos
Highlighted
Esri Esteemed Contributor
Hi Steven,

You could do something similar to below:


table = "data.dbf"

x = 1
firstfield = True
while x <= 5:
    if firstfield:
        arcpy.AddField_management(table, "A_Name", "Text", "", "", "10")
        arcpy.AddField_management(table, "B_Name", "Text", "", "", "15")
        arcpy.AddField_management(table, "C_Name", "Text", "", "", "20")
        arcpy.AddField_management(table, "D_Name", "Text", "", "", "25")
        firstfield = False
    else:
        arcpy.AddField_management(table, "A" + str(x) + "_Name", "Text", "", "", "10")
        arcpy.AddField_management(table, "B" + str(x) + "_Name", "Text", "", "", "15")
        arcpy.AddField_management(table, "C" + str(x) + "_Name", "Text", "", "", "20")
        arcpy.AddField_management(table, "D" + str(x) + "_Name", "Text", "", "", "25")
        x += 1


At the end of the code you will just need to add the 'arcpy.AddField_management' for the remainding three fields.
0 Kudos