Batch export coded value domains to table

9079
5
Jump to solution
08-14-2015 04:27 PM
GeorgeKipp
New Contributor III

I've attempted to write a script that will export all the coded value domains in a geodatabase to a table. Before I dig in the to script and what's wrong, I have looked at the similar question on this topic, but that script would not run for me. Here is my code:

import arcpy

arcpy.env.workspace = r"F:\ITree"

gdb = r"ITree.gdb"

arcpy.env.overwriteOutput = True

domains = arcpy.da.ListDomains(gdb)

for domain in domains:

     if domain.domainType == 'CodedValue':

          domain_name = domain.name

          print 'Exporting {0} coded values to table in {1}'.format(domain_name, gdb)

          coded_value_list = domain.codedValues

          print "The coded values / descriptions are"

          for value, descrip in coded_value_list.iteritems():

               print "{0} : {1}".format(value, descrip)

          out_table_name = domain_name.lower()

          arcpy.DomainToTable_management(gdb, domain_name, out_table_name, "item", "descrip")

     else:

          print "{0} not a coded value domain. Passing it up.".format(domain.name)

When I comment out the Domain to Table, the script runs and produces, this:

Exporting LANDUSE coded values to table in ITree.gdb

The coded values / descriptions are

1 : Single family residential

0 : Not entered

3 : Small commercial

2 : Multi-family residential

5 : Park/vacant/other

4 : Industrial/Large commercial

However, when I uncomment the Domain to Table, it produces the following error:

Exporting LANDUSE coded values to table in ITree.gdb

The coded values / descriptions are

1 : Single family residential

0 : Not entered

3 : Small commercial

2 : Multi-family residential

5 : Park/vacant/other

4 : Industrial/Large commercial

Traceback (most recent call last):

  File "E:/Work/MDC/GISData/MOTools/Scripts/export_gdb_domains.py", line 29, in <module>

    arcpy.DomainToTable_management(gdb, domain_name, out_table_name, "item", "descrip")

  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 1491, in DomainToTable

    raise e

arcgisscripting.ExecuteError: ERROR 999999: Error executing function.

Failed to execute (DomainToTable).

It also produces the first table from the domain, but does not copy the coded values and creates an empty table. I've looked through the help documents for Domain to Table (Data Management) and List Domains, but I cannot figure out why it's throwing an error. Clearly something is off in the Domain to Table function, but I can't figure out what, especially since it prints all the parameters correctly. I would greatly appreciate any suggestions. Thank you for your time.

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

It's also the same inside ArcMap.  When the code is run in ArcMaps Python window, you can check the Geoprocessing:Results window for info.  By right clicking on the Domain To Table line and coping it as a Python snippet to notepad, you can see the parameters being used, even  when the tool fails.

An alternative might be to give a full path to your gdb, such as:

arcpy.env.workspace = r"F:\ITree\ITree.gdb"

View solution in original post

5 Replies
RandyBurton
MVP Alum

If you are running the script outside of ArcMap, I think your "out_table_name" should be something like "dbname.gdb\tablename".  Try these changes:

# add os to your includes:
include os

# change DomainToTable line to
arcpy.DomainToTable_management(gdb, domain_name, gdb + os.sep + out_table_name, "item", "descrip")
0 Kudos
RandyBurton
MVP Alum

It's also the same inside ArcMap.  When the code is run in ArcMaps Python window, you can check the Geoprocessing:Results window for info.  By right clicking on the Domain To Table line and coping it as a Python snippet to notepad, you can see the parameters being used, even  when the tool fails.

An alternative might be to give a full path to your gdb, such as:

arcpy.env.workspace = r"F:\ITree\ITree.gdb"

XanderBakker
Esri Esteemed Contributor

If I run the code below, it just works...

def main():
    import arcpy, os
    gdb = r"C:\GeoNet\Domains2Tables\data.gdb"
    arcpy.env.overwriteOutput = True

    for dom in arcpy.da.ListDomains(gdb):
        if dom.domainType == 'CodedValue':
            arcpy.DomainToTable_management(in_workspace=gdb,
                                          domain_name=dom.name,
                                          out_table=os.path.join(gdb, dom.name),
                                          code_field="item",
                                          description_field="descrip",
                                          configuration_keyword="")

            print " - domain '{0}' of type '{1}' exported to table".format(dom.name, dom.domainType)
        else:
            print " - domain '{0}' of type '{1}' skipped".format(dom.name, dom.domainType)

if __name__ == '__main__':
    main()

... and prints this output:

- domain 'domainrange' of type 'Range' skipped
- domain 'domain1' of type 'CodedValue' exported to table
- domain 'domain2' of type 'CodedValue' exported to table
- domain 'domain3' of type 'CodedValue' exported to table

... and creates the tables.

Is the output name of the table valid? Do you by any chance have a LANDUSE featureclass in the output gdb?

GeorgeKipp
New Contributor III

This works too. Thanks Xander.

0 Kudos
GeorgeKipp
New Contributor III

That was what worked for me Randy. Working code below. Thanks for the help.

import arcpy

arcpy.env.workspace = r"F:\ITree\ITree.gdb"
gdb = arcpy.env.workspace
arcpy.env.overwriteOutput = True

domains = arcpy.da.ListDomains(gdb)

for domain in domains:
     if domain.domainType == 'CodedValue':
         domain_name = domain.name
         print 'Exporting {0} coded values to table in {1}'.format(domain_name, gdb)
         coded_value_list = domain.codedValues
         print "The coded values / descriptions are"
         for value, descrip in coded_value_list.iteritems():
             print "{0} : {1}".format(value, descrip)
         out_table_name = domain_name.lower()
         arcpy.DomainToTable_management(gdb, domain_name, out_table_name, "item", "descrip")
     else:
         print "{0} not a coded value domain. Passing it up.".format(domain.name)

I changed the env.workspace to the full path and set that to the gdb variable