import arcpy from arcpy import env env.overwriteOutput = 1 env.workspace = r"C:\temp\python\test.gdb" lstFCs = arcpy.ListFeatureClasses("*") for fc in lstFCs: lstFields = arcpy.ListFields(fc) for field in lstFields: if field.domain: print fc + ": " + field.name + ": " + field.domain arcpy.DomainToTable_management(env.workspace, field.domain, "tb_" + field.domain, "code", "description") rows = arcpy.SearchCursor(fc, "", "", field.name) listValues = [] for row in rows: fieldValues = row.getValue(field.name) listValues.append(fieldValues) del row, rows rows = arcpy.SearchCursor("tb_" + field.domain, "", "", "code") listCodes = [] for row in rows: fieldCodes = row.getValue("code") listCodes.append(fieldCodes) del row, rows for n in listValues: if n not in listCodes: print fc + " contains field " + field.name + " with domain " + field.domain + " containing value " + n + " that is not a coded value" lstTables = arcpy.ListTables("tb_*") for table in lstTables: arcpy.Delete_management(table)
import arcpy from arcpy import env env.overwriteOutput = 1 gdb = "C:/MyTempTossDir/test.gdb" env.workspace = gdb lstFDs = arcpy.ListDatasets("*", "Feature") for fd in lstFDs: env.workspace = gdb + "/" + fd lstFCs = arcpy.ListFeatureClasses("*") for fc in lstFCs: lstFields = arcpy.ListFields(fc) for field in lstFields: if field.domain: print fc + ": " + field.name + ": " + field.domain arcpy.DomainToTable_management(gdb, field.domain, gdb + "/" + "tb_" + field.domain, "code", "description") rows = arcpy.SearchCursor(fc, field.name + " IS NOT NULL", "", field.name) listValues = [] for row in rows: fieldValues = row.getValue(field.name) if len(fieldValues.strip()) <> 0: listValues.append(fieldValues) del row, rows rows = arcpy.SearchCursor(gdb + "/" + "tb_" + field.domain, "", "", "code") listCodes = [] for row in rows: fieldCodes = row.getValue("code") listCodes.append(fieldCodes) del row, rows for n in listValues: if n not in listCodes: print fc + " contains field " + field.name + " with domain " + field.domain + " containing value " + n + " that is not a coded value"
#========================== #**** Check_InvalidDomains ***** # This code creates a text log to identify feature classes # that has fields that violate domain values #--------------------------------------------------- # Limitations/ What to Improve: # > It does not account for subtypes. # > There's always room to improve error handling #========================== import arcpy, os from arcpy import env banner = "******************************************************************" basepath = "C:/MyTempTossDir" gdb = basepath + "/test.gdb" env.overwriteOutput = 1 try: env.workspace = gdb logbasepath = basepath logname = "GeoDB_DomainValueCheck.txt" logpath = logbasepath + "/" + logname if os.path.exists(logpath): os.remove(logpath) logfile = open(logpath, "w") #========================== logfile.write(banner + "\n") msgline = "Checking for Invalid Domain Values in Feature Classes Inside Datasets" logfile.write(msgline + "\n") logfile.write(banner + "\n") print banner print msgline msgline = "Dataset" + "\t" + "FeatureClass" + "\t" + "Field" + "\t" + "Domain" + "\t" + "InvalidValue" logfile.write(msgline + "\n") lstFDs = arcpy.ListDatasets("*", "Feature") for fd in lstFDs: env.workspace = gdb + "/" + fd lstFCs = arcpy.ListFeatureClasses("*") for fc in lstFCs: lstFields = arcpy.ListFields(fc) for field in lstFields: if field.domain: arcpy.DomainToTable_management(gdb, field.domain, gdb + "/" + "tb_" + field.domain, "code", "description") rows = arcpy.SearchCursor(fc, field.name + " IS NOT NULL", "", field.name) listValues = [] for row in rows: fieldValues = row.getValue(field.name) if len(fieldValues.strip()) <> 0: listValues.append(fieldValues) del row, rows listUniqValues = list(set(listValues)) listUniqValues.sort() rows = arcpy.SearchCursor(gdb + "/" + "tb_" + field.domain, "", "", "code") listCodes = [] for row in rows: fieldCodes = row.getValue("code") listCodes.append(fieldCodes) del row, rows if len(listUniqValues)>1: for n in listUniqValues: if n not in listCodes: msgline = fd + "\t" + fc + "\t" + field.name + "\t" + field.domain + "\t" + n logfile.write(msgline + "\n") msgline = fd + "/" + fc + " contains field " + field.name + " with domain " + field.domain + " with the following invalid domain value: " + n print msgline #========================== logfile.write(banner + "\n") msgline = "Checking for Invalid Domain Values in Standalone Feature Classes" logfile.write(msgline + "\n") logfile.write(banner + "\n") print banner print msgline msgline = "FeatureClass" + "\t" + "Field" + "\t" + "Domain" + "\t" + "InvalidValue" logfile.write(msgline + "\n") env.workspace = gdb lstFCs = arcpy.ListFeatureClasses("*") for fc in lstFCs: lstFields = arcpy.ListFields(fc) for field in lstFields: if field.domain: arcpy.DomainToTable_management(env.workspace, field.domain, "tb_" + field.domain, "code", "description") rows = arcpy.SearchCursor(fc, field.name + " IS NOT NULL", "", field.name) listValues = [] for row in rows: fieldValues = row.getValue(field.name) if len(fieldValues.strip()) <> 0: listValues.append(fieldValues) del row, rows listUniqValues = list(set(listValues)) listUniqValues.sort() rows = arcpy.SearchCursor("tb_" + field.domain, "", "", "code") listCodes = [] for row in rows: fieldCodes = row.getValue("code") listCodes.append(fieldCodes) del row, rows if len(listUniqValues)>1: for n in listUniqValues: if n not in listCodes: msgline = fc + "\t" + field.name + "\t" + field.domain + "\t" + n logfile.write(msgline + "\n") msgline = fc + " contains field " + field.name + " with domain " + field.domain + " with the following invalid domain value: " + n print msgline env.workspace = gdb lstTables = arcpy.ListTables("tb_*") for table in lstTables: arcpy.Delete_management(table) del lstFCs del lstFDs del lstFields del listValues del listUniqValues del listCodes # ISSUE: RefreshCatalog is not working, I don't know why.. Kept this to remind me to figure it out later... arcpy.RefreshCatalog(env.workspace) except: print arcpy.GetMessages(2) finally: env.overwriteOutput = "" env.workspace = "" logfile.close()
Diana Umpierre I am worried that you missed to include a check if a particular domain is Range type or Coded values. Since this check is not in place, your script will result in invalid values for a domain if it is Range type. Here is an example, Let's say you have a range domain on a particular field ranging from 500 to 1000, now the values inside this field may be anything between 500 and 1000 so all values except 500 and 1000 will be marked as invalid values, the reason being when you export such a range domain, your exported table will have only two rows with values i.e. 500 and 1000 and hence rest of the values become invalid.
In short, once you identify that a particular domain is Range, you simply need to check with an if statement if given value is in the range of that range domain or not.
Cheers!