[GIS] Checking if domain contains particular coded value using ArcPy

arcpydomainsesri-geodatabase

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

Best Answer

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.