Looking for a way in model builder or python to iterate through all domains in a GDB and Sort Coded Value on ASC order.
Solved! Go to Solution.
In addition to using 'domain.name', I would move the line to just after the 'if domainType == CodedValue' line; you only need to sort it once.
import arcpy
domains = arcpy.da.ListDomains("Database Connections\\Server DB Owner.sde")
for domain in domains:
print('Domain name: {0}'.format(domain.name))
if domain.domainType == 'CodedValue':
arcpy.SortCodedValueDomain_management("Database Connections\\Server DB Owner.sde", domain.name, "CODE", "ASCENDING")
coded_values = domain.codedValues
for val, desc in coded_values.items():
print('{0} : {1}'.format(val, desc))
elif domain.domainType == 'Range':
print('Min: {0}'.format(domain.range[0]))
print('Max: {0}'.format(domain.range[1]))
EDIT: I'm not sure if it will print the domain sorted within the loop. You could change lines 8-9:
for val, desc in sorted(coded_values.items()):
print('{0} : {1}'.format(val, desc))
Thank you, Joe,
It is a start, but seem to be missing the value for the SortCodedValueDomain_management command.
import arcpy
domains = arcpy.da.ListDomains("Database Connections\\Server DB Owner.sde")
for domain in domains:
print('Domain name: {0}'.format(domain.name))
if domain.domainType == 'CodedValue':
coded_values = domain.codedValues
for val, desc in coded_values.items():
arcpy.SortCodedValueDomain_management("Database Connections\\Server DB Owner.sde", Which Value?, "CODE", "ASCENDING")
print('{0} : {1}'.format(val, desc))
elif domain.domainType == 'Range':
print('Min: {0}'.format(domain.range[0]))
print('Max: {0}'.format(domain.range[1]))
SortCodedValueDomain_management (in_workspace, domain_name, sort_by, sort_order)
You need the domain name. I would try 'domain.name' :
arcpy.SortCodedValueDomain_management("Database Connections\\Server DB Owner.sde", domain.name, "CODE", "ASCENDING")
You code is a little tough to decipher: My old eyes prefer tabs over single spaced indents. I've copied and pasted it below with what I think they should be:
import arcpy
domains = arcpy.da.ListDomains("Database Connections\\Server DB Owner.sde")
for domain in domains:
print('Domain name: {0}'.format(domain.name))
if domain.domainType == 'CodedValue':
coded_values = domain.codedValues
for val, desc in coded_values.items():
arcpy.SortCodedValueDomain_management("Database Connections\\Server DB Owner.sde", Which Value?, "CODE", "ASCENDING")
print('{0} : {1}'.format(val, desc))
elif domain.domainType == 'Range':
print('Min: {0}'.format(domain.range[0]))
print('Max: {0}'.format(domain.range[1]))
I'm trying your code as I've shown it, and haven't yet figured out which value either.... I'll keep trying as I have time...
edited moments later- looks like Randy got it. Just an FYI: when I run the code on an egdb that we have with domains, I get an error because I can't get an exclusive lock due to several users being connected...
In addition to using 'domain.name', I would move the line to just after the 'if domainType == CodedValue' line; you only need to sort it once.
import arcpy
domains = arcpy.da.ListDomains("Database Connections\\Server DB Owner.sde")
for domain in domains:
print('Domain name: {0}'.format(domain.name))
if domain.domainType == 'CodedValue':
arcpy.SortCodedValueDomain_management("Database Connections\\Server DB Owner.sde", domain.name, "CODE", "ASCENDING")
coded_values = domain.codedValues
for val, desc in coded_values.items():
print('{0} : {1}'.format(val, desc))
elif domain.domainType == 'Range':
print('Min: {0}'.format(domain.range[0]))
print('Max: {0}'.format(domain.range[1]))
EDIT: I'm not sure if it will print the domain sorted within the loop. You could change lines 8-9:
for val, desc in sorted(coded_values.items()):
print('{0} : {1}'.format(val, desc))
Thank you, Randy. The 'domain.name' expression was the key.