Iterate through a GDB to Sort Coded Value Domains

416
6
Jump to solution
10-25-2018 07:40 AM
Brian_McLeer
Occasional Contributor II

Looking for a way in model builder or python to iterate through all domains in a GDB and Sort Coded Value on ASC order. 

Brian
0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

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))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

6 Replies
JoeBorgione
MVP Esteemed Contributor

Does this help?

ListDomains—Help | ArcGIS Desktop 

That should just about do it....
0 Kudos
Brian_McLeer
Occasional Contributor II

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]))‍‍‍‍‍‍‍‍‍‍‍‍
Brian
0 Kudos
RandyBurton
MVP Regular Contributor

 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")‍‍
JoeBorgione
MVP Esteemed Contributor

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...

That should just about do it....
0 Kudos
RandyBurton
MVP Regular Contributor

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))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Brian_McLeer
Occasional Contributor II

Thank you, Randy. The 'domain.name' expression was the key. 

Brian
0 Kudos