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 creating, maintaining, and updating webmaps and feature layers in ArcGIS Online. In this post we will focus on extracting a Group Layer from a WebMap and saving it as a Group Layer item.
For this example, I have a WebMap that contains a Group Layer called “Base Layers” as shown below. This Group layer contains Feature Layers across two different Feature Services. The goal is to create a Group Layer item in our Content that we can use across our future WebMaps.
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.
## import GIS which provides the gateway entry to your AGOL
from arcgis.gis import GIS
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 (WebMap) Item Object
ContentManager get()
method to return an Item
object.
## access the WebMap Item
wm_item = agol.content.get("WM_ITEM_ID")
Get the WebMap Item Data Properties
Item
object get_data()
method returns a dictionary describing the WebMap configuration such as operationalLayers
, which will provide access to the Group Layer information.
## get the WebMap Item data properties
wm_item_data = wm_item.get_data()
Get the Definition for the Group Layer from the operationalLayers
In the snippet below we use list comprehension to return the entry from the operationalLayer property for our Group Layer of interest. We know have the dictionary that describes the Group Layer from the WebMap.
## the get_data() provides access to the operationalLayer property that contains
## information about the layers in the WebMap, we are interested in the Group Layer
group_lyr = [lyr for lyr in wm_item_data["operationalLayers"] if lyr["layerType"] == "GroupLayer"][0]
Define the Item Properties for the new Group Layer Item
For the Item properties we set the "type"
to “Group Layer”, provide a "description", "snippet",
and "tags"
, and one of the important elements, the "title"
.
It is also important to set the "typeKeywords"
as they are displayed below.
Lastly we use the "text"
property to supply our Group Layer information. The "text"
property provides access to the information returned from the get_data()
method for an Item
object.
## define Item properties for a new item - a Group Layer Item
item_properties = {
"type": "Group Layer",
"description": "Base layers for biodiversty mapping",
"title": "Biodiversity_BaseLayers",
"tags": ["Biodiversity", "BIO", "BaseLayers"],
"snippet": "Base layers for biodiversty mapping",
"typeKeywords": [
"ArcGIS API for JavaScript",
"Group Layer",
"Map"
],
"text" : group_lyr
}
Add the Group Layer Content Item to your ArcGIS Online
add()
method from the ContentManager
and supplying the Item properties we defined above.
## add the Item to your GIS
agol.content.add(
item_properties = item_properties
)
The Group Layer Item
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
################################################################################
## 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.gis.toc.html#arcgis.gis.Item.get_data
## https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.ContentManager.add
##
##
## API Version: 2.3.0
##
################################################################################
## access ArcGIS Online
agol = GIS("home")
## get the WebMap Item object
wm_item = agol.content.get("WM_ITEM_ID")
## get the WebMap Item data properties
wm_item_data = wm_item.get_data()
## the get_data() provides access to the operationalLayer property that contains
## information about the layers in the WebMap, we are interested in the Group Layer
group_lyr = [lyr for lyr in wm_item_data["operationalLayers"] if lyr["layerType"] == "GroupLayer"][0]
## define Item properties for a new item - a Group Layer Item
item_properties = {
"type": "Group Layer",
"description": "Base layers for biodiversty mapping",
"title": "Biodiversity_BaseLayers",
"tags": ["Biodiversity", "BIO", "BaseLayers"],
"snippet": "Base layers for biodiversty mapping",
"typeKeywords": [
"ArcGIS API for JavaScript",
"Group Layer",
"Map"
],
"text" : group_lyr
}
## add the Item to your GIS
agol.content.add(
item_properties = item_properties
)
################################################################################
print("\nSCRIPT COMPLETE")