Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

How can I check if a domain contains a particular coded value?

How to check if domain already exists? shows how to verify whether a domain exists in a geodatabase.

However, I can't see how to check that a particular coded value exists, before I use arcpy.DeleteCodedValueFromDomain_management

A workaround is to use Domain To Table and read the table - is that the best option?

def removeFromDomain(domain,value):
try:
    #Check if the coded value exists. For this we need to export the domain to a table, and read the table
    if arcpy.Exists(workspace + os.sep + tempTbl):
        arcpy.Delete_management(workspace + os.sep + tempTbl)
    arcpy.DomainToTable_management(workspace + os.sep + GDB, domain, workspace + os.sep + tempTbl, "code", "descript")
    rows = arcpy.SearchCursor(workspace + os.sep + tempTbl)
    valueExists = False
    for row in rows:
        if row.getValue("code") == value:
            valueExists = True
    if valueExists:        
        arcpy.DeleteCodedValueFromDomain_management(workspace + os.sep + GDB, domain, [value])
        print "Deleted coded value " + str(value) + " from domain " + str(domain)
    else:
        print "domain value " + str(value) + " does not exist on domain " + domain
    arcpy.Delete_management(workspace + os.sep + tempTbl)
except Exception, err:
    print err
    raise

Thanks

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

You will need to be using ArcGIS 10.1 to use the da (Data Access) module but it looks like the Domain class has a codedValues property that can be used to obtain a Python dictionary containing the coded values for attribute domains.

share|improve this answer
    
Thanks, I'm currently stuck on 10.0 so I'll use my workaround for now and da once I can upgrade. –  Stephen Lead Oct 4 '12 at 1:46
add comment

As @PolyGeo mentioned, If you are using 10.1, the arcpy.da (Data Access) module contains a ListDomains function which returns a list of Domain objects, each with a codedValues property you can examine to determine if the specified value exists in a given domain.

Alternatively, if this is an SDE geodatabase at version 10 or greater (in earlier versions, coded value domains were stored as BLOBs, while at 10 they are stored as XML), you could use SQL to to find the domain and its values.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.