Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
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 – Midavalo 6 hours ago
    
Yes I need: {'tree': ['pine', 'magnolia', 'fir', 'grey pine']} – Djb 6 hours ago
    
To append values into your empty list you could use .append() on it. – PolyGeo 5 hours ago

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.