Skip to content

Instantly share code, notes, and snippets.

@cindygis
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cindygis/e353d9c9e652c2b7833d to your computer and use it in GitHub Desktop.
Save cindygis/e353d9c9e652c2b7833d to your computer and use it in GitHub Desktop.
Creates a new coded value domain from values in a feature class using a generator expression.
#
# @date 25/02/2015
# @author Cindy Williams
#
# Extracts a list of values from a field in an attribute table
# to be used in a coded value domain.
#
# For use in the Python window in ArcCatalog.
#
import arcpy
arcpy.env.workspace = r"C:\Some\Arb\Folder\source.gdb" # File GDB containing source feature class or table
# Input variables
gdb = r"C:\Some\Arb\Folder\work.gdb" # File GDB that will contain the domain
ftr = "ftr_class" # Feature class or table
field_nam = "DC_NAME" # Field name containing values
cvd = "cvd_Dist" # Name of new coded value domain
# Generator of values (from http://stackoverflow.com/questions/4154571/sorted-using-generator-expressions-rather-than-lists/4155652#4155652)
gen_cvd = sorted(set(row[0] for row in arcpy.da.SearchCursor(ftr, field_nam)), key=str.lower)
# Create empty coded value domain of type SHORT
arcpy.management.CreateDomain(in_workspace=gdb,
domain_name=cvd,
domain_description="Dist",
field_type="SHORT",
domain_type="CODED")
# Populate the domain with values from the generator
for dom_code, dom_value in enumerate(gen_cvd):
arcpy.management.AddCodedValueToDomain(in_workspace=gdb,
domain_name=cvd,
code=dom_code,
code_description=dom_value)
print("Added {0}: {1}".format(dom_code, dom_value))
print("Script complete.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment