10 Replies Latest reply on Jan 31, 2012 8:28 AM by csny490

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

    dom426
      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!
        • Re: How can I get the description value of a field that has a domain?
          csny490
          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&viewfull=1#post30010
          • Re: How can I get the description value of a field that has a domain?
            rbrennan-esristaff
            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.
            • Re: How can I get the description value of a field that has a domain?
              dom426
              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. :)
              • Re: How can I get the description value of a field that has a domain?
                csny490
                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)
                • Re: How can I get the description value of a field that has a domain?
                  dom426
                  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?
                  • Re: How can I get the description value of a field that has a domain?
                    csny490
                    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
                    • Re: How can I get the description value of a field that has a domain?
                      dom426
                      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!!
                      • Re: How can I get the description value of a field that has a domain?
                        csny490
                        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
                        • Re: How can I get the description value of a field that has a domain?
                          dom426
                          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!!
                          • Re: How can I get the description value of a field that has a domain?
                            csny490
                            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'