[GIS] Creating Domains and Coded Values using ArcPy

arcgis-10.2arcpydomains

The Coded Values are not being displayed within numeric order.

Final results look like: [10][13][12][27C][21][22][27A][33][31][30][27B]..
How would we display Coded Values in numerical 0-36?

# Name: NEO Gas Systems.py
# Description: Create an attribute domain
# Author: jordan N. Miller
# Date: 02/17/2017


#Import system modules
import arcpy

try:
# Set the workspace (to avoid having to type in the full path to the data        every time)
arcpy.env.workspace = "C:/eis"

# Set local parameters
domName = "Gas Systems"
gdb = "UPDM.gdb"

# Process: Create the coded value domain
arcpy.CreateDomain_management("UPDM.gdb", domName, "Gas Systems", "TEXT", "CODED")

# Store all the domain values in a dictionary with the domain code as the  "key" and the
# domain description as the "value" (domDict[code])
domDict = {"9":"New Bedford", \
"10": "West Wooster",
"12": "Summitville Tile",
"13": "West Warren",
"21": "Winesburg",
"22": "Bowerston",
"27": "NEO/TCO/Timken",
"27A": "Timken Header Line",
"27B": "Timken Industrial Gases",
"27C": "Timken Houseline",
"30": "North Mount Hope",
"31": "Gross Lumber",
"33": "Nova Tech",
"34": "North Lawrence",
"35": "Anchor Hocking Plant #1",
"36": "Anchor Hocking Plant #2"}

# Process: Add valid material types to the domain
# use a for loop to cycle through all the domain codes in the dictionary
for code in domDict:
    arcpy.AddCodedValueToDomain_management(gdb, domName, code, domDict[code])

except Exception as err:
print(err.args[0])

Best Answer

If I'm understanding your problem correctly, the reason your domains aren't showing in numeric order is because a python dictionary is not ordered. Python won't necessarily process it in the order you've written it.

As python lists are processed in order, to overcome this I would create a python list containing the Domain values, and then use the Dictionary to pull through the descriptions.

# Name: NEO Gas Systems.py
# Description: Create an attribute domain
# Author: jordan N. Miller
# Date: 02/17/2017

#Import system modules
import arcpy

# Set the workspace (to avoid having to type in the full path to the data        every time)
arcpy.env.workspace = "C:/eis"

# Set local parameters
domName = "Gas Systems"
gdb = "UPDM.gdb"

# Process: Create the coded value domain
arcpy.CreateDomain_management("UPDM.gdb", domName, "Gas Systems", "TEXT", "CODED")

# List of domain values in load order
domainList = ["9", "10", "12", "13", "21", "22", "27", "27A", "27B", "27C", "30", "31", "33", "34", "35", "36"]

# Store all the domain values in a dictionary with the domain code as the  "key" and the
# domain description as the "value" (domDict[code])
domDict = {"9": "New Bedford",
           "10": "West Wooster",
           "12": "Summitville Tile",
           "13": "West Warren",
           "21": "Winesburg",
           "22": "Bowerston",
           "27": "NEO/TCO/Timken",
           "27A": "Timken Header Line",
           "27B": "Timken Industrial Gases",
           "27C": "Timken Houseline",
           "30": "North Mount Hope",
           "31": "Gross Lumber",
           "33": "Nova Tech",
           "34": "North Lawrence",
           "35": "Anchor Hocking Plant #1",
           "36": "Anchor Hocking Plant #2"}

# Process: Add valid material types to the domain
# use a for loop to cycle through all the domain codes in the dictionary
for code in domainList:
    arcpy.AddCodedValueToDomain_management(gdb, domName, code, domDict[code])

print "Finished!"

If you wish to add another value, add the new code into the domainList and the code/description to the domDict. Just remember to add any new codes in the position you want to see it in the domain drop-down