Get a list of domain values from feature layer?

1775
3
Jump to solution
10-23-2020 01:07 PM
JaneSkillman
New Contributor II

Hi,

I'm new to arcgis API for python. I can't figure out how to get a list of domain values from a hosted feature layer. I did read the api-reference, and can see that there is a query_domain method for the FeatureLayerCollection class (here: arcgis.features module — arcgis 1.8.2 documentation ) but I don't understand the syntax well enough to figure out how to actually write the code.

Thank you,

Jes

0 Kudos
1 Solution

Accepted Solutions
Kevin_McIntyre
New Contributor III

The best I could come up with is that you'll have to access the properties of the FL and loop through it to find coded values. 

Here's a sample:

from arcgis import GIS

gis = ("https://arcgis.com", "username", "password")

#Get the arcgis item (this would be your hosted FGDB).
item = gis.content.get("itemID")

#Get the layers list from the item.
layers = item.layers

#Select the layer from the list you want to use.
fl = layers[0]

#Get the properties.
properties = fl.properties

#Loop through the JSON to extract the coded values and save them to a list. 

lst = []

for row in properties[fields]:

   if row['domain']:

      lst.append(row['domain'])

That should get you a list of the dictionaries associated with each coded domain into a neat list. 

View solution in original post

3 Replies
Kevin_McIntyre
New Contributor III

The best I could come up with is that you'll have to access the properties of the FL and loop through it to find coded values. 

Here's a sample:

from arcgis import GIS

gis = ("https://arcgis.com", "username", "password")

#Get the arcgis item (this would be your hosted FGDB).
item = gis.content.get("itemID")

#Get the layers list from the item.
layers = item.layers

#Select the layer from the list you want to use.
fl = layers[0]

#Get the properties.
properties = fl.properties

#Loop through the JSON to extract the coded values and save them to a list. 

lst = []

for row in properties[fields]:

   if row['domain']:

      lst.append(row['domain'])

That should get you a list of the dictionaries associated with each coded domain into a neat list. 

JaneSkillman
New Contributor II

Thank you Kevin!

Your post was very helpful and set me on the right path. This is what I ended up doing, in case this is useful to others.

from arcgis import GIS

def getDomainNames(itemID, layerNumber):
    
    """
    Returns a set of domain names from a feature layer within a feature layer collection.
    
    :param itemID: identification number for an ArcOnline or Portal item (Feature Layer 
     Collection or FGDB)
    :type itemID: str
    :param layerNumber: the integer [0 and up] that calls the feature layer of interest
    :type layerNumber: int
    
    """
    
    # Assumes user is connected to their ArcOnline account in ArcPro.
    gis = GIS("pro")

    # Get the arcgis item (this would be your hosted FGDB).
    item = gis.content.get(itemID)

    # Select the layer within the feature layer collection.
    fl = item.layers[0]

    # Get the properties.
    properties = fl.properties
    
    # create an empty set
    nameSet = set()
    
    # Loop through properties, get a list of all coded values for the given domain.
    
    for row in properties["fields"]:

        # if domain exists for the field and the domain type is codedValue:
        if row['domain'] and row['domain']['type'] == 'codedValue':
            
            # add the domain name to the set.
            nameSet.add(row['domain']['name'])
                
    return (nameSet)



def getDomainValues(itemID, layerNumber, domName):
    
    """
    Returns a list of domain codes from a specific domain within a feature layer.
    
    :param itemID: identification number for an ArcOnline or Portal item (Feature Layer 
     Collection or FGDB)
    :type itemID: str
    :param layerNumber: the integer [0 and up] that calls the feature layer of interest
    :type layerNumber: int
    :param domName: name of the domain of interest
    
    """
    
    # Assumes connected to duinc in ArcPro
    gis = GIS("pro")

    #Get the arcgis item (this would be your hosted FGDB).
    item = gis.content.get(itemID)

    # Select the layer within the feature layer collection.
    fl = item.layers[layerNumber]

    # Get the properties.
    properties = fl.properties
    
    # Loop through properties, get a list of all coded value domains.
    for row in properties["fields"]:
        # if domain exists and the domain name is correct:
        if row['domain'] and row['domain']['name'] == domName:

            # create a list of all codes included in that domain
            valueList = [i['code'] for i in row['domain']['codedValues']]
    
    return (valueList)

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Kevin_McIntyre
New Contributor III

Edit: After tinkering with the query domains method for Feature Layer Collections, I found out that it wasn't working at all and submitted the issue to ESRI. I think it's an issue with the REST API.

0 Kudos