0

I have a hosted feature layer with a field with a domain (coded values).

How do I get the description instead of the coded value?

The layer is in the TOC of an aprx, and I can retrieve the domain name with arcpy.ListFields(layer.dataSource, field)[0].domain but that's just a string, I can't get its properties. Also I don't think arcpy.da.ListDomains() applies to hosted feature layers.

1 Answer 1

5

Use the ArcGIS for Python API

from arcgis.gis import GIS

## or use GIS("agol_url", "username", "password")
agol = GIS("home")

item = agol.content.get("FS_ITEM_ID")

## key: domain name, value: dictionary code:description
domains_dict = {}

## for each layer/table
## replace .layers with .tables to access tables instead
for lyr in item.layers:
    ## for each field in the layer/table
    for fld in lyr.properties.fields:
        ## if there is a coded domain
        if fld.domain and fld.domain.type == "codedValue":
            ## get the domain name
            domain_name = fld.domain.name
            ## this dictionary will represent the key as the code
            ## and the value as the description
            entry_dict = {}
            ## for each code value pair
            for entry in fld.domain.codedValues:
                for code, value in entry.items():
                    ## add them to the entry_dict
                    entry_dict[entry.code] = entry.name

            ## add to the domain_dict
            domains_dict[domain_name] = entry_dict

for name, codes in domains_dict.items():
    print(name, codes)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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