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

8992
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
You would have to export the domain to a table 1st and use it as a look up table.

Domain To Table tool: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000022000000

I'd recomend loading the domain table into a python dictionary and using like in this example: http://forums.arcgis.com/threads/9555-Modifying-Permanent-Sort-script-by-Chris-Snyder?p=30010&viewfu...
0 Kudos
RussellBrennan
Esri Contributor
This probably does not help you right now but with version 10.1 of ArcGIS there will be an arcpy.da.listdomains function that will return domain objects with the properties you are looking for.
0 Kudos
DominickAlfonso
New Contributor
Thanks, csny490, that actually helped me get started with what I need to accomplish... Is there a built-in function that will help me in finding the domain name of a field using the field's name so that I can use the domain name in the DomainToTable function?

Also, russellb, I will definitely keep that in mind! That's good to know info for a future update down the road. 🙂
0 Kudos
ChrisSnyder
Regular Contributor III
Is there a built-in function that will help me in finding the domain name of a field using the field's name so that I can use the domain name in the DomainToTable function?


Yes there is. I've never done anything with it, but it'd look something like this:

fieldList = arcpy.ListFields(myFC)
for field in fieldList:
   print field.name + " - " + str(field.domain)
0 Kudos
DominickAlfonso
New Contributor
Okay, so I think I'm just about there now...

For some reason the DomainToTable function is not liking the input I am giving it. I find the domain name the way you suggested, csny490, and stick that into the function with the rest of my parameters:

#workspace is a parameter taken as input on the tool (example.gdb would be the workspace, for example)
#domain is a field.domain value: fields are listed using arcpy.ListFields(aFeatureClass) function, iterated through to find the correct field and then that field's domain is placed into a variable called "domain"
#the last 3 inputs are made up names for the table, code and description values respectively

arcpy.DomainToTable_management(workspace, domain, "ExampleTable", "code", "description")


It errors out with:
File "...\arcpy\management.py", line...
ERROR 999999: Error executing function.
Failed to execute (DomainToTable).

Any ideas?
0 Kudos
ChrisSnyder
Regular Contributor III
For the "workspace" variable, make sure you are specify the entire path. For example:
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)
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 cosde and description are the names


If you want to export all the domains in a workspace:
workspace = r"C:\temp\test.gdb"
dsc = arcpy.Describe(workspace)
for domain in dsc.domains:
   domainTable = workspace + "\\domain_table_for_" + domain.replace(" ", "_") #replace any blanks with an underscore
   arcpy.DomainToTable_management(workspace, domain, domainTable, "code", "description") #assuming cosde and description are the name
0 Kudos
DominickAlfonso
New Contributor
Oh, I see now! My issue was with the name of the table itself in the parameters to the function... I did not think that I would need to include the workspace it had to be placed inside, I just assumed it would know this from the workspace parameter.

Thanks for all your help!!
0 Kudos
ChrisSnyder
Regular Contributor III
I did not think that I would need to include the workspace it had to be placed inside


You actually don't if you 1st set the workspace using this comand.

arcpy.env.workspace = r"C:\temp\test.gdb"


So this "should" work I think:

myWorkspace = r"C:\temp\test.gdb"
arcpy.env.workspace = myWorskpace
dsc = arcpy.Describe(arcpy.env.workspace)
for domain in dsc.domains:
   domainTable = "domain_table_for_" + domain.replace(" ", "_") #replace any blanks with an underscore NOTE I ONLY INCLUDE THE TABLE NAME HERE, SINCE I ALREADY SET THE WORSKPACE
   arcpy.DomainToTable_management(workspace, domain, domainTable, "code", "description") #assuming cosde and description are the name 



However, I generally don't set the workspace like that, and instead rely on proving the full paths to any input/output data files. Setting the workspace tends to be a tiny bit slower (takes > 0 second o set it) and it gets somewhat confusing when you are reading/writting data to many different workspaces.

The only time I set the workspace is when I have to, for example when listing the featureclasses within a .gdb

arcpy.env.workspace = r"C:\temp\test.gdb"
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
   print fc
0 Kudos
DominickAlfonso
New Contributor
I am working with multiple workspaces which is why I needed to specify the full path, exactly as you say!

After successfully running the DomainToTable function, I am having trouble reading the values (description values) from the table itself. Shouldn't I just be able to do this by using a cursor just like when trying to access data in a feature class, etc? Or would this be different because of the structure of the table object? I want to try and create a python dict from the values in the table (linking "code" fields to their respective "description" fields so that I can reference them in another area of the script).

Your help is much appreciated!!
0 Kudos