Retrieving Domain Information from ArcGIS Online with the ArcGIS API for Python

The ArcGIS API for Python has a query_domains() method for a Feature Layer Collection object. A Feature Layer Collection object represents a Feature Service. In previous versions of the documentation it stated that this method was only available for Portal (Enterprise), but this has since been removed (in 2.2.0.1). Calling the query_domains() method on a Feature Service in ArcGIS Online (AGOL) used to return the following error…

				
					The requested layer (layerId: queryDomains) was not found.
(Error Code: 400)
				
			

I had hoped that the query_domains() method would now work with AGOL, but alas, it is not quite there yet. While the method does not throw an error, it simply returns an empty list (as of API version 2.2.0.1). You can try this out with the code below.

				
					from arcgis import GIS
from arcgis.features import FeatureLayerCollection

## connect to agol
agol = GIS("home")

## get feature service as an item object
item = agol.content.get("FS_ITEM_ID")

## create a flc object from item object
flc = FeatureLayerCollection(item.url, agol)

## use the query_domains() method on the layer(s) of choice
domains = flc.query_domains(layers=[0])

print(domains)
				
			

And the result…

				
					[]
				
			

If you had the means to try this in Portal with a referenced Feature Service (not Hosted), you would get a lovely print out as shown below.

				
					[{
    'type': 'codedValue',
    'name': 'Domain_Name',
    'description': '',
    'codedValues': [
        {   'name': 'Description1',
            'code': 1
        },
        {   'name': 'Description2',
            'code': 2
        },
        {   'name': 'Description3',
            'code': 3
        }
    ],
    'fieldType': 'esriFieldTypeSmallInteger',
    'mergePolicy': 'esriMPTDefaultValue',
    'splitPolicy': 'esriSPTDefaultValue'
}]
				
			

A workaround...

We can dive into the JSON of the Feature Layer using the ArcGIS API for Python to extract the information of the domain set for that particular layer.

				
					from arcgis import GIS
from arcgis.features import FeatureLayerCollection

## access AGOL
agol = GIS("home")

## get the feature service as an Item object
item = agol.content.get("FS_ITEM_ID")

## the list to hold the domain information
domains_list = []

## get domains for specific layer
for fld in item.layers[0].properties.fields:
    if fld.domain:
         append each domain into the list
        if fld.domain not in domains_list:
            domains_list.append(fld.domain)

print(domains_list)
				
			

 We don’t get all the information as listed in the previous output code snippet for Portal, but we have access to the information shown below.

				
					[{
  "type": "codedValue",
  "name": "Domain_Name",
  "codedValues": [
    {
      "name": "Description1",
      "code": 1
    },
    {
      "name": "Description2",
      "code": 2
    },
    {
      "name": "Description3",
      "code": 3
    }
  ]
}]
				
			

I have submitted as a bug to Esri, you can see the issue entry here.

Leave a Comment

Your email address will not be published. Required fields are marked *