Get Field Domain Information from ArcGIS Online with the ArcGIS API for Python

Table of Contents

Introduction

The ArcGIS API for Python is a powerful Python library that allows users to interact with and automate tasks in ArcGIS Online (or Portal). The API is excellent for programmatically interacting with Feature Layers, their attributes, and getting even more granular, down to their fields and data schema. Domains are an integral part of data integrity. The API provides a mechanism to extract domain information for Referenced Feature Services in an Enterprise setting (ArcGIS Portal) using query_domains(), but the function does not comply with Hosted Feature Services, unfortunately. We will provide a workaround in this post to extract domain information from a Hosted Feature Service in ArcGIS Online.

arcgis modules

The API provides access to your organisations ArcGIS Online via the GIS class in the gis module. This GIS class is the gateway to ArcGIS Online. We will need to import the FeatureLayerCollection class to create a FeatureLayerCollection object. The FeatureLayerCollection class represent a Feature Service and is a part of the features module. This features module provides components for working with FeatureLayer objects and Table objects along with FeatureSet and individual Feature objects.
				
					## import GIS which provides the gateway entry to your AGOL
from arcgis.gis import GIS
## import FeatureLayerCollection from the features module
from arcgis.features import FeatureLayerCollection
				
			

Accessing ArcGIS Online

Our first port of call is to access your ArcGIS Online via the GIS class. There are a handful of ways to achieve access, if you are logged into your ArcGIS Online in ArcGIS Pro you can simply use "home", otherwise, another common way is to provide the ArcGIS Online URL, followed by your username and password.
				
					## Access AGOL
agol = GIS("home")
				
			
				
					## Access AGOL
agol = GIS(
    url = "https://your_organisation.maps.arcgis.com/",
    username = "Your_Username",
    password = "Your_Password"
)
				
			

Get the Feature Service Content Item

We next get the Feature Service content item as an Item object. We use the ContentManager get() method and supply in the item id for the Feature Service of interest. The get() method returns an Item object.
				
					## get feature service as an item object
item = agol.content.get("FS_ITEM_ID")
				
			

Create a FeatureLayerCollection Object

A FeatureLayerCollection object represents a Feature Service.
				
					## create a flc object from item object
flc = FeatureLayerCollection(item.url, agol)
				
			

Test the API query_domains() method

According to Esri, by design, the query_domain() method will only retrieve information if the Feature service is a Reference service and not a Hosted Service. With a Referenced service, that data points directly to a database, with a Hosted service, the data is stored in ArcGIS Online or Portal. Therefore, the query_domain() method is only applicable in ArcGIS Portal and under the Referenced service condition. Let’s test it out anyway for a Hosted Feature Service in ArcGIS Online
				
					## use the query_domains() method on the layer(s) of choice
domains = flc.query_domains(layers=[0])

print(domains)

				
			

We are returned an empty list.

				
					[]
				
			

Now, 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'
}]
				
			

The 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.

				
					## 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
    }
  ]
}]
				
			

Unlock the full potential of ArcGIS Online by mastering the art of efficient Content Management with the ArcGIS API for Python. In this comprehensive course, you will embark on a journey to streamline your geospatial workflows, enhance data organization, and maximize the impact of your ArcGIS Online platform.

Geospatial Professionals, GIS Analysts, Data Managers, and enthusiasts will discover the power of automation and script-based operations to efficiently manage content in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience in leveraging the ArcGIS API for Python to perform a wide range of content management tasks with ease.

All the code in one place

You can find the entire code workflow below with links to important components in the documentation that were used.

				
					from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

################################################################################
## API Reference Links:
##  https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#gis
##  https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.ContentManager.get
##  https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#featurelayercollection
##  https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#arcgis.features.FeatureLayerCollection.query_domains
##
## Description:
##  Retrieve field domain information from a hosted feature service
##
## API Version: 2.2.0.1
##
################################################################################

## 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)
				
			

Leave a Comment

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