When I create domains, I start with an Excel workbook with one domain per tab. I then run a python script to create a table in a file geodatabase for each domain and import the data from the spreadsheet. A second python script converts the table to a domain. I will use a third script, similar to the first, to create my feature layers.
To create the tables:
import xlrd
import arcpy
from arcpy import env
geoDB = r"C:\Path\To\filedb.gdb"
excelWB = r"C:\Path\To\workbook.xlsx"
env.workspace = geoDB
def new_table(dbTable, dbFields, geoDB, excelWB):
print "\nCreating table: " + dbTable
template = ""
config_keyword = ""
arcpy.CreateTable_management(geoDB, dbTable, template, config_keyword)
print "Adding fields: "
for new_field in dbFields:
print "\t" + new_field[0]
if (new_field[4] == "
nullable = "NULLABLE"
else:
nullable = "NON_NULLABLE"
arcpy.AddField_management(dbTable,new_field[0],new_field[1],"
new_field[2],new_field[3],nullable,"NON_REQUIRED",new_field[4])
print "Adding data from Excel"
workbook = xlrd.open_workbook(excelWB)
worksheet = workbook.sheet_by_name(dbTable)
num_rows = worksheet.nrows - 1
fields = [t.encode('utf8') for t in worksheet.row_values(0)]
cursor = arcpy.da.InsertCursor(dbTable, fields)
curr_row = 0
while curr_row < num_rows:
curr_row += 1
if (dbTable == "Domain2" ) :
encoded = worksheet.row_values(curr_row)
else :
encoded = [t.encode('utf8') for t in worksheet.row_values(curr_row)]
cursor.insertRow(encoded)
del cursor
if __name__ == "__main__":
dbTable = "Domain1"
dbFields = [
["CODE", "TEXT", "1", "CODE", "
["DESCRIPTION", "TEXT", "4", "DESCRIPTION", "
]
new_table(dbTable,dbFields,geoDB,excelWB)
dbTable = "Domain2"
dbFields = [
["CODE", "TEXT", "4", "CODE", "
["DESCRIPTION", "TEXT", "8", "DESCRIPTION", "
["MyInt", "SMALLINTEGER", "
["MyDate", "DATE", "
["MyLong", "INTEGER", "
]
new_table(dbTable,dbFields,geoDB,excelWB)
print
print "Processing complete."
And to convert the tables to domains:
import arcpy
from arcpy import env
geoDB = r"C:\Path\To\filedb.gdb"
env.workspace = geoDB
domains = [ 'Domain1',
'Domain2' ]
print "Processing Domains:"
for domain in domains:
print "\t" + domain
arcpy.TableToDomain_management(domain,"CODE","DESCRIPTION",geoDB,domain,domain,"REPLACE")
arcpy.SortCodedValueDomain_management(geoDB,domain,"CODE","ASCENDING")
print "Done."
Just another way among many, I suppose.