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
2 Answers 2
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.
-
Thanks, I'm currently stuck on 10.0 so I'll use my workaround for now and da once I can upgrade.Stephen Lead– Stephen Lead2012年10月04日 01:46:11 +00:00Commented Oct 4, 2012 at 1:46
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.