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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 21, 2023 at 9:34

1 Answer 1

6

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)
answered Jun 21, 2023 at 11:11
0

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.