Select to view content in your preferred language

Add coded values to a domain from a features attribute list?

1566
5
Jump to solution
01-16-2019 03:09 PM
by Anonymous User
Not applicable

I'm aware of the ArcMap tool 'Add coded value to domain' but I'm looking for some help to create a list using a features attribute and use those values to generate coded value domains. I would be using each attribute entry as the coded value and description. I'm looking to accomplish this using ArcPy...Any suggestions?

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

I added some code to my previous post.  I found TableToDomain to be faster than AddCodedValueToDomain when there were lots of values.  Also, I'm not sure how AddCodedValueToDomain would react if it the code was already in the domain, but I expect it will generate an error.

TableToDomain will create the domain and can also append to an existing one.  If the domain exists and the coded value is in the domain, an error will be generated if the update option is "APPEND".  If using "REPLACE", the code and description are overwritten.

View solution in original post

5 Replies
RandyBurton
MVP Alum

I would use a search cursor to read unique values into a dictionary (key and value being the same). A list set would probably accomplish the same thing. Sort if desired.  Convert that into a table (possibly in_memory).  And then use the Table to domain tool.

UPDATE:  Here's an example:

gdb = r"C:\Path\to\file.gdb"
domainName = "DomainName"
domainDesc = "Domain Description"
layer = "featureLayer"
field = ['TextField']

# create a table in memory
arcpy.CreateTable_management('in_memory', 'domainValues')
arcpy.AddField_management("domainValues","CODE","TEXT")
arcpy.AddField_management("domainValues","DESCRIPT","TEXT")

# read field into dictionary (these will be unique values - not tested for None value)
domainDict = {r[0]:r[0] for r in arcpy.da.SearchCursor(layer, field)}

# sort the dictionary keys (optional, you can also sort the domain later)
codes = sorted(k for k in domainDict)

# insert the codes into the in memory table
rows = arcpy.da.InsertCursor('in_memory\domainValues', ['CODE','DESCRIPT'])
for c in codes:
    rows.insertRow((c,c))
del rows

# table to domain
arcpy.TableToDomain_management(in_table="domainValues", # in_memory\domainValues table
                               code_field='CODE', # code
                               description_field='DESCRIPT', # description 
                               in_workspace=gdb,
                               domain_name=domainName,
                               domain_description=domainDesc,
                               update_option="REPLACE" # or "APPEND"
                               )

arcpy.Delete_management("in_memory\domainValues")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I did a quick check of the script inside Arcmap 10.5.  I found creating an in memory table and using TableToDomain to be faster than AddCodedValueToDomain (particularly with lots of values).

by Anonymous User
Not applicable

I ended up running the code below and it runs great except adds (u' ',) around the actual text I want to assign. Ideas on how to remove this?

import arcpy

arcpy.env.overwrite = True

wrkspc = "G:\Temporary\Anthony\Workspaces\New File Geodatabase.gdb"
poly = "G:\Temporary\Anthony\Workspaces\New File Geodatabase.gdb\TempArea"

arcpy.MakeFeatureLayer_management(poly, 'poly_layer')

with arcpy.da.SearchCursor('poly_layer', ['Text']) as poly_cursor:
    for x in poly_cursor:
        print x
        arcpy.AddCodedValueToDomain_management("{}".format(wrkspc), "TempArea", "{}".format(x), "{}".format(x))

print "complete"

output

0 Kudos
RandyBurton
MVP Alum

It should just be in your print output.  For line 12 in your code, you can use:  print x[0]

0 Kudos
RandyBurton
MVP Alum

I added some code to my previous post.  I found TableToDomain to be faster than AddCodedValueToDomain when there were lots of values.  Also, I'm not sure how AddCodedValueToDomain would react if it the code was already in the domain, but I expect it will generate an error.

TableToDomain will create the domain and can also append to an existing one.  If the domain exists and the coded value is in the domain, an error will be generated if the update option is "APPEND".  If using "REPLACE", the code and description are overwritten.

by Anonymous User
Not applicable

Thanks for your help on this