Table of Contents
The Video
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 creating, maintaining, and updating Feature Services and Feature Layers in ArcGIS Online. The Feature Layer Collection insert_layer() method allows you to add a new Feature Layer to an existing Feature Service, however, if the Feature Service item has JSON data set against it, the newly inserted layer will not display in ArcGIS Online until configured in the JSON data element.
Automate ArcGIS Online Feature Service Workflows with the ArcGIS API for Python | A Complete Guide from Beginner to Advanced | Full Course
Free on YouTube! A set of videos detailing ArcGIS Online Feature Services workflows using the ArcGIS API for Python. We will do a deep dive into properties and methods available, along with custom workflows for automating with Feature Services and the ArcGIS API for Python.
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 the Feature Layer information to aid with populating the Item JSON data.
## provides access to ArcGIS Online
from arcgis.gis import GIS
## the FeatureLayerCollection class
from arcgis.features import FeatureLayerCollection
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"
)
Create a FeatureLayerCollection Object
We first create an Item object and create a FeatureLayer Collection object from the feature service item
## get the item object
item = agol.content.get("FS_ITEM_ID")
## create the FeatureLayerObject
flc = FeatureLayerCollection.fromitem(
item = item
)
Insert Layer
Use the FeatureLayerManager object insert_layer() method to insert a layer to the feature service based on a feature class in a file geodatabase.
""" filepath to file geodatabase """
gdb_path = "C:\path\to\zipped\gdb.zip"
""" use the flc object insert_layer() method to create a feature layer """
status = flc.manager.insert_layer(
data_path=gdb_path,
name="Ireland Landmask" # this is not honoured
)
print(status)
Check that the feature layer has been inserted
You can print the FeatureLayer objects to screen that now make up the feature layers in the service. You will see the newly added (inserted) feature layer.
print(*flc.layers, sep="\n")
Fix the feature layer
You mighty notice that the feature layer is present in the FeatureLayer objects returned, but the feature layer is not showing on the feature service item homepage. You fix that by updating the item JSON definition using the item object get_data() mthod to retrieve the current definition and manipulating to add a definition for the inserted layer.
The lyr_dict dictionary below is added to the definition.
Once the definition is updated, the feature layer now shows on the item homepage. Check out the video above to see the workflow in action.
""" item data layer dictionary template """
lyr_dict = {
"id" : None,
"blendMode" : "normal",
"layerDefinition" : {},
"disablePopup" : False,
"popupInfo" : {
"popupElements" : [
{
"type" : "fields",
"fieldInfos" : None
}
]
}
}
""" get the FeatureLayer object """
fl = flc.layers[1]
""" update the id key """
lyr_dict["id"] = fl.properties.id
""" update the layerDefinition key (symbology)) """
lyr_dict["layerDefinition"]["drawingInfo"] = fl.properties.drawingInfo
""" get the field info for the popup """
field_infos = []
for field in fl.properties.fields:
field = dict(field)
field_dict = {
"fieldName" : field["name"],
"isEditable" : field["editable"],
"label" : field["alias"],
"visible" : True
}
field_infos.append(field_dict)
""" apply the field infos """
lyr_dict["popupInfo"]["popupElements"][0]["fieldInfos"] = field_infos
""" get the item JSON data """
item_data = item.get_data()
""" update layers """
item_data["layers"] = item_data["layers"] + [lyr_dict]
""" update the Item """
update_dict = {
"text" : item_data
}
status = item.update(
item_properties = update_dict
)
If you’ve found these blogs helpful and would like to support the project, please consider making a donation. All learning material is provided free of charge to help GIS professionals, students, and developers learn ArcPy, the ArcGIS API for Python, ArcGIS Pro, and ArcGIS Online. Donations help cover website hosting, software licensing, domain costs, and the time involved in creating and maintaining tutorials, courses, and learning resources. Your support helps keep this content freely available to everyone and allows new courses and materials to be developed for the GIS community. If the content has helped you solve a problem, learn a new skill, or advance your career, please consider supporting the project with a donation. Every contribution, no matter the size, is greatly appreciated.
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#contentmanager
## 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.gis.toc.html#item
## 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.fromitem
## https://developers.arcgis.com/python/latest/api-reference/arcgis.features.toc.html#arcgis.features.FeatureLayerCollection.manager
## https://developers.arcgis.com/python/latest/api-reference/arcgis.features.managers.html#featurelayercollectionmanager
## https://developers.arcgis.com/python/latest/api-reference/arcgis.features.managers.html#arcgis.features.managers.FeatureLayerCollectionManager.insert_layer
## https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.get_data
## https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.update
##
########################################################################################
"""Access AGOL"""
agol = GIS("home")
## get the item object
item = agol.content.get("FS_ITEM_ID")
## create the FeatureLayerObject
flc = FeatureLayerCollection.fromitem(
item = item
)
########################################################################################
## Insert Layer
########################################################################################
""" filepath to file geodatabase """
gdb_path = "C:\path\to\zipped\gdb.zip"
""" use the flc object insert_layer() method to create a feature layer """
status = flc.manager.insert_layer(
data_path=gdb_path,
name="Ireland Landmask" # this is not honoured
)
print(status)
########################################################################################
## Check Layer
########################################################################################
print(*flc.layers, sep="\n")
########################################################################################
## Fix Layer
########################################################################################
""" item data layer dictionary template """
lyr_dict = {
"id" : None,
"blendMode" : "normal",
"layerDefinition" : {},
"disablePopup" : False,
"popupInfo" : {
"popupElements" : [
{
"type" : "fields",
"fieldInfos" : None
}
]
}
}
""" get the FeatureLayer object """
fl = flc.layers[1]
""" update the id key """
lyr_dict["id"] = fl.properties.id
""" update the layerDefinition key (symbology)) """
lyr_dict["layerDefinition"]["drawingInfo"] = fl.properties.drawingInfo
""" get the field info for the popup """
field_infos = []
for field in fl.properties.fields:
field = dict(field)
field_dict = {
"fieldName" : field["name"],
"isEditable" : field["editable"],
"label" : field["alias"],
"visible" : True
}
field_infos.append(field_dict)
""" apply the field infos """
lyr_dict["popupInfo"]["popupElements"][0]["fieldInfos"] = field_infos
""" get the item JSON data """
item_data = item.get_data()
""" update layers """
item_data["layers"] = item_data["layers"] + [lyr_dict]
""" update the Item """
update_dict = {
"text" : item_data
}
status = item.update(
item_properties = update_dict
)
print(status)
########################################################################################
print("\nSCRIPT COMPLETE")