How can I get the description value of a field that has a domain?

9073
10
01-27-2012 12:06 PM
DominickAlfonso
New Contributor
Hello, this is my first time posting on these forums so I apologize if I am doing something wrong beforehand!

I am trying to find a way to get a value from a field that has a domain. I noticed that getting a value using the normal approach only gives me the coded value of the field, but not the description value. Is there any way to retrieve the description? The coded value is not needed for what I am trying to accomplish. Here is the function I am using this in:

def getValues(featureClass,FieldName):
    desc = arcpy.Describe(featureClass)
    cur = arcpy.SearchCursor(featureClass)
    row = cur.next()
    names = []
    while row:
        name = str(row.getValue(FieldName)) #would like to get description and not code value on this line...
        try:
            ind = names.index(name)
        except:
            names.append(name)
        row = cur.next()
        
    return(names)


Thanks!
Tags (2)
0 Kudos
10 Replies
ChrisSnyder
Regular Contributor III
It might look something like this then...

workspace = r"C:\temp\test.gdb"
myFC = workspace + "\\my_fc"
domainList = []
fieldList = arcpy.ListFields(myFC)
for field in fieldList:
   if field.domain != '' and field.domain not in domainList:
      domainList.append(field.domain)
domainDict = {}
for domain in domainList:
   domainTable = workspace + "\\domain_table_for_" + domain.replace(" ", "_") #replace any blanks with an underscore
   arcpy.DomainToTable_management(workspace, domain, domainTable, "CODE", "DESCRIPTION") #assuming code and description are the names
   searchRows = arcpy.SerachCursor(domainTable)
   for row in searchRow:
      domainDict[domain, searchRow.CODE] = searchRow.DESCRIPTION
   del searchRow, searchRows



Then to rerieve the descriptions from the domainDict, the key is domain name and code value. For example:

>>> print domainDict["BOAT_TYPE", 3] #access CODE = 3 from the 'BOAT_TYPE' domain
'Melges 24'
>>> print domainDict["BOAT_TYPE", 4] #access CODE = 4 from the 'BOAT_TYPE' domain
'Laser 2'
>>> print domainDict["BOAT_TYPE", 5] #access CODE = 5 from the 'BOAT_TYPE' domain
'Catalina 30'
0 Kudos