Table of Contents
The Video
Introduction
Want to know how many features are in your ArcGIS Online Feature Layer while performing a workflow with the ArcGIS API for Python?
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 FeatureLayer class that will enable us to get our feature count.
## provides access to ArcGIS Online
from arcgis.gis import GIS
## the FeatureLayer class
from arcgis.features import FeatureLayer
Accessing ArcGIS Online
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 Feature Layer, although you can access and create the Feature Layer object any way you wish to.
## the URL to the Feature Layer
fl_url = "FEATURE_LAYER_URL"
Create the FeatureLayer object
We create a FeatureLayer object from the user provided URL. You can create a FeatureLayer object in a number of ways such as using the layers property for a Feature Service Item object or via a FeatureLayerCollection object, The choice is yours.
fl = FeatureLayer(
url = fl_url,
gis = agol
)
Get the Feature Count
The FeatureLayer object has a property called estimates that returns a dictionary. Within that dictionary there is a key named “count” that has a value representing the number of features in the feature layer.
We can also use the FeatureLayer object query() method with a solo return_count_only parameter set to True. This will also return the number of features in the feature layer.
## using the estimates property
print(fl.estimates["count"])
## using the query() method
print(fl.query(return_count_only=True))
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 FeatureLayer
################################################################################
## 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.features.toc.html#featurelayer
## https://developers.arcgis.com/python/latest/api-reference/arcgis.features.toc.html#arcgis.features.FeatureLayer.estimates
## https://developers.arcgis.com/python/latest/api-reference/arcgis.features.toc.html#arcgis.features.FeatureLayer.query
##
################################################################################
################################################################################
## ACCESS ARCGIS ONLINE ########################################################
agol = GIS("home")
################################################################################
## USER INPUT ##################################################################
## the URL to the Feature Layer
fl_url = "FEATURE_LAYER_URL"
################################################################################
## CREATE A FEATURELAYER OBJECT ################################################
fl = FeatureLayer(
url = fl_url,
gis = agol
)
################################################################################
## GET FEATURE COUNT ###########################################################
## using the estimates property
print(fl.estimates["count"])
## using the query() method
print(fl.query(return_count_only=True))
################################################################################
print("\nSCRIPT COMPLETE")