How to check if an ArcGIS Online Feature Service is a View Layer with the ArcGIS API for Python

Table of Contents

The Video

Introduction

I was recently doing an ArcGIS Online audit for an organisation and needed to know whether a Feature Service was a View Service or the main Hosted Feature Service. When performing a search() with the ContentManager using the ArcGIS API for Python all Feature Services were returned with no distinction between the original and a View. I found I could check the size and it would be zero for a View, but I wondered if there were any definitive properties that I could check to see if the service was indeed a View or not. I found two. One via the typeKeywords property for an Item object and a FeatureLayerCollection object will will have an isView property if it is indeed a view, and omitted if not.

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 with using the ArcGIS API for Python to perform a wide range of content management tasks with ease, such as creating Folders and Groups and managing content within them.

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 also utilise the FeatureLayerCollection class that will enable us to access object properties to check for the isView property.

				
					## provides access to ArcGIS Online
from arcgis.gis import GIS

## the FeatureLayerCollection class
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"
)
				
			

Required inputs

Only one  required parameter, the url to the Item ID for the Feature Service to query.

				
					## the ArcGIS Content Item Id for the Service
item_id = "ITEM_ID"
				
			

Option 1: Checking the Item Object typeKeywords property

We first create an Item object for the Feature Service of interest. An Item object has a typeKeywords property that helps ArcGIS Online know what type of Item the content item is. If the typeKeywords list contain “View Service”, well then the service is a View made from another Hosted Feature Service., otherwise it is a Hosted Feature Service and not a View.

				
					## get the Feature Service as an Item object
item = agol.content.get(item_id)

if "View Service" in item.typeKeywords:
    print("View Service")
else:
    print("Feature Service")
				
			

Option 2: FeatureLayerCollection Object isView property

We can create a FeatureLayerCollection object from an Item object that represents a Feature Service. While an Item object represents the content item in ArcGIS Online, a FeatureLayerCollection object represents a Feature Service. We check if the FeatureLayerCiollection object has a property called “isView”, and if that property is set to True, then the service is a View, otherwise it is a Hosted Feature Service and not a View. 

				
					## create a FeatureLayerCollection object from an Item object
flc = FeatureLayerCollection.fromitem(
    item = item
)

if hasattr(flc.properties, "isView"):
    if flc.properties.isView == True:
        print("View Service")
    else:
        print("Feature Service")

else:
    print("Feature Service")
				
			

Geospatial Professionals, GIS Analysts, and enthusiasts will discover the power of automation and script-based operations to efficiently interact and update WebMaps 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 WebMap tasks with ease.

We will dissect WebMaps for all that they are and by the end of this course you will be comfortable with manipulating the JSON, which is the behind the scenes configuration of a WebMap, to produce scripts for workflows where there is currently no API Python method, such as grouping layers.

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/latest/api-reference/arcgis.gis.toc.html#gis
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#item
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#arcgis.gis.ContentManager.get
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.features.toc.html#featurelayercollection
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.features.toc.html#arcgis.features.FeatureLayerCollection.properties
##
################################################################################


################################################################################
## ACCESS ARCGIS ONLINE ########################################################

agol = GIS("home")

################################################################################
## USER INPUT ##################################################################

## the ArcGIS Content Item Id for the Service
item_id = "ITEM_ID"

################################################################################
## CREATE AN ITEM OBJECT #######################################################

## get the Feature Service as an Item object
item = agol.content.get(item_id)

if "View Service" in item.typeKeywords:
    print("View Service")
else:
    print("Feature Service")

################################################################################
## CREATE A FEATURELAYERCOLLECTION OBJECT ######################################

## create a FeatureLayerCollection object from an Item object
flc = FeatureLayerCollection.fromitem(
    item = item
)

if hasattr(flc.properties, "isView"):
    if flc.properties.isView == True:
        print("View Service")
    else:
        print("Feature Service")

else:
    print("Feature Service")

################################################################################
print("\nSCRIPT COMPLETE")
				
			

Leave a Comment