+ Reply to Thread
Results 1 to 11 of 11

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

  1. #1
    Dominick Alfonso
    Join Date
    Jan 2012
    Posts
    6
    Points
    0
    Answers Provided
    0


    0

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

    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:

    Code:
    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!

  2. #2
    Chris Snyder

    Join Date
    May 2010
    Posts
    1,263
    Points
    418
    Answers Provided
    38


    2

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

    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/arcgisdesk...00000022000000

    I'd recomend loading the domain table into a python dictionary and using like in this example: http://forums.arcgis.com/threads/955...ll=1#post30010
    Last edited by csny490; 01-27-2012 at 01:23 PM.

  3. #3
    Russell Brennan
    Join Date
    Oct 2009
    Posts
    161
    Points
    70
    Answers Provided
    9


    1

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

    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.
    Last edited by russellb; 01-30-2012 at 09:42 AM. Reason: changed version 10.1 of 'the software' to version 10.1 of 'ArcGIS'
    Russell
    esri

  4. #4
    Dominick Alfonso
    Join Date
    Jan 2012
    Posts
    6
    Points
    0
    Answers Provided
    0


    0

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

    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.

  5. #5
    Chris Snyder

    Join Date
    May 2010
    Posts
    1,263
    Points
    418
    Answers Provided
    38


    1

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

    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:

    Code:
    fieldList = arcpy.ListFields(myFC)
    for field in fieldList:
       print field.name + " - " + str(field.domain)

  6. #6
    Dominick Alfonso
    Join Date
    Jan 2012
    Posts
    6
    Points
    0
    Answers Provided
    0


    0

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

    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:

    Code:
    #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?

  7. #7
    Chris Snyder

    Join Date
    May 2010
    Posts
    1,263
    Points
    418
    Answers Provided
    38


    1

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

    For the "workspace" variable, make sure you are specify the entire path. For example:
    Code:
    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:
    Code:
    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

  8. #8
    Dominick Alfonso
    Join Date
    Jan 2012
    Posts
    6
    Points
    0
    Answers Provided
    0


    0

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

    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!!

  9. #9
    Chris Snyder

    Join Date
    May 2010
    Posts
    1,263
    Points
    418
    Answers Provided
    38


    0

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

    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.

    Code:
    arcpy.env.workspace = r"C:\temp\test.gdb"
    So this "should" work I think:

    Code:
    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

    Code:
    arcpy.env.workspace = r"C:\temp\test.gdb"
    fcList = arcpy.ListFeatureClasses()
    for fc in fcList:
       print fc

  10. #10
    Dominick Alfonso
    Join Date
    Jan 2012
    Posts
    6
    Points
    0
    Answers Provided
    0


    0

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

    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!!

  11. #11
    Chris Snyder

    Join Date
    May 2010
    Posts
    1,263
    Points
    418
    Answers Provided
    38


    0

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

    It might look something like this then...

    Code:
    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'

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts