Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
replaces the code for value in a new _value field
def replace_codes_with_values(feature_class, field_info, domain_info):
field_domain_map = [(field, field.domain) for field in field_info if field.domain]
fields = [field[0] for field in field_domain_map]
field_names = [field.name for field in fields]
domain_names = [domain[1] for domain in field_domain_map]
domain_lookup = {}
for name in domain_names:
domain_lookup[name] = [domain for domain in domain_info if domain.name == name][0]
[arcpy.management.RemoveDomainFromField(feature_class, field.name) for field in fields]
[arcpy.management.AddField(feature_class, '{}_Value'.format(field.name), 'TEXT', field_alias=field.aliasName) for field in fields]
def swap_code_for_value(code, domain_name):
domain = domain_lookup[domain_name].codedValues
if not domain:
return code
if code not in domain:
return code
return domain[code]
combined_fields = field_names + ['{}_Value'.format(field) for field in field_names]
with arcpy.da.UpdateCursor(feature_class, combined_fields) as cursor:
for row in cursor:
values_row = [swap_code_for_value(code, domain_names[i]) for i, code in enumerate(row) if i < len(field_names)]
row = row[:len(field_names)] + values_row
cursor.updateRow(row)
arcpy.management.DeleteField(feature_class, field_names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.