2

I have a File GDB that has domains and coded values. I would like to extract those values from the GDB and build a dictionary. For example:

I have hundreds of domains and with each domain has a set of coded values associated with it.

*Domain* , *Coded Value*
['tree', 'pine']
['tree', 'magnolia']
['tree', 'fir']
['tree', 'grey pine']
['tree', 'oak']
['soil', 'clay']
['soil', 'loam']
['soil', 'sandy']
['brush', 'manz']
['brush', 'short']
['water', 'wetland']
['water', 'lake']
etc.....

I want this:

{'tree': ['pine', 'magnolia', 'fir', 'grey pine']}
{'soil': ['clay', 'loam', 'sandy']}
{'brush': ['manz', 'short']}
{'water': ['wetland', 'lake']}

How do i accomplish this by using arcpy.da.ListDomains? This is what I have so far:

import arcpy
doms = arcpy.da.ListDomains(gdb)
 for dom in doms:
 if dom.domainType == 'CodedValue':
 codedvalues = dom.codedValues
 for code1 in codedvalues:
 list1 = []
 c_data = "{},{}".format(dom.name, code1)
 domain2 = c_data.split(",")[0]
 code2 = c_data.split(",")[1]
 list1 = [domain2, code2]
 print list1 #this prints out the first code block shown above
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 9, 2016 at 23:19
3
  • 1
    {'tree': 'pine', 'magnolia', 'fir', 'grey pine'} isn't a dictionary. A dictionary is {Key: Value}, so you'd probably need a list to record the values e.g. {'tree': ['pine', 'magnolia', 'fir', 'grey pine']} to make it how you're suggesting Commented Jun 9, 2016 at 23:31
  • Yes I need: {'tree': ['pine', 'magnolia', 'fir', 'grey pine']} Commented Jun 9, 2016 at 23:46
  • To append values into your empty list you could use .append() on it. Commented Jun 10, 2016 at 0:14

1 Answer 1

9

The following should do the job:

codedDomains = {domain.name: domain.codedValues.keys() for domain in arcpy.da.ListDomains(gdb) if domain.domainType == 'CodedValue'}

Basically, it uses a list comprehension to populate a dictionary, but only if it is a coded value domain. If you wanted to have it populated with the description instead of the coded value, replace the

domain.codedValues.keys()

with

domain.codedValues.values()

This entry in the ArcGIS help may also give other options on domain objects that might be helpful.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Jun 10, 2016 at 0:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.