Skip to content

Instantly share code, notes, and snippets.

@cindygis
Last active July 29, 2018 11:06
Show Gist options
  • Save cindygis/13398a07da00d558790d to your computer and use it in GitHub Desktop.
Save cindygis/13398a07da00d558790d to your computer and use it in GitHub Desktop.
Extracts a unique list of values from an attribute table and converts it into an coded value domain.
#
# @date 31/05/2013
# @author Cindy Williams
#
# Extracts a unique list of values from an attribute table
# and converts it into an coded value domain.
#
# For use as a script tool in an ArcGIS Toolbox.
#
import arcpy
# Input variables
inTable = arcpy.GetParameterAsText(0) # Table containing domain values
gdb = arcpy.GetParameterAsText(1) # GDB that will contain the new domain
# Build unique list of code descriptions
lst = set([row[0] for row in arcpy.da.SearchCursor(inTable,"TYPE")])
# Build list of code values
codeList = [f for f in range(0,len(lst)+1)]
# Create dictionary of domain values
domDict = dict(zip(codeList,lst))
arcpy.management.CreateDomain(gdb, "cvdPointType", "Elec Point Type","SHORT","CODED")
# Populate coded value domain with <code: description> pairs
for code in domDict:
arcpy.management.AddCodedValueToDomain(gdb, "cvdPointType", code, domDict[code])
arcpy.AddMessage("Added coded domain value {0}:{1}".format(code, domDict[code]))
arcpy.AddMessage("Script tool complete.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment