Group ArcGIS Online WebMap Layers using 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 creating, maintaining, and updating webmaps and feature layers in ArcGIS Online. In this post we will focus on updating a webmap by creating a group layer from existing layers. As of writing this post, there is no neat method for creating group layers, although I have it under good authority that this functionality is being added to the API before the end of 2024.

For this example, I am going to group four of the layers from the list below.

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 WebMap class to create a WebMap object. The WebMap class is a part of the mapping module. This mapping module provides components for working with 2D and 3D maps and scenes, and also includes classes for map layers such as the MapFeatureLayer, MapImageLayer, and VectorTileLayer

				
					## import GIS which provides the gateway entry to your AGOL
from arcgis.gis import GIS
## import WebMap from the mapping module
from arcgis.mapping import WebMap
				
			

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

Create a WebMap Object

We create the WebMap object from an Item object.
				
					## access the WebMap Item
wm_item = agol.content.get("WM_ITEM_ID")

## create a WebMap object
webmap = WebMap(wm_item)
				
			

Get the Layer Definitions

We use the WebMap object get_layer() method to retrieve the layer definitions for the layers of interest that we wish to group.
				
					spa = webmap.get_layer(title="Special Protected Areas (SPA)")
sac = webmap.get_layer(title="Special Areas of Conservation (SAC)")
nha = webmap.get_layer(title="National Heritage Areas (NHA)")
pnha = webmap.get_layer(title="Proposed Natural Heritage Areas (pNHA)")
				
			

Group the Layer Definitions

Place the definitions in a group with the bottom-most layer as the first entry and as we add in each extra layer it is stacked on top of the next with the pnha below the top most layer in the group.

				
					group_lyrs = [
        spa,
        sac,
        nha,
        pnha
]

				
			

Create a New Operational Layers Definition

The operationalLayers property for a WebMap item definition is a list. We create the dictionary definition for a group layer and add this dictionary as the solo entry to a group that will represent the operationalLayers property of our WebMap. The name of the group layer is NPWS Designated Booundaries. We append any other layers not in the group layer into out new operationalLayers. Since the group layer is the first in the new operationalLayers definition, it will be the bottom-most layer in our WebMap.

				
					## get all layer titles for the group
lyr_names = [lyr.title for lyr in group_lyrs]

## create the dictionary that defines a group layer in a WebMap
group = {
    "title" : "NPWS Designated Boundaries",
    "layers" : group_lyrs,
    "layerType": "GroupLayer",
    "visibilityMode": "independent"
}

## create a group to store the updated operation layers property
## the group layers are added to the group
new_ol = [group]

## append in any other layers not in the group
for lyr in webmap.definition.operationalLayers:
    if lyr.title not in lyr_names:
        new_ol.append(lyr)
				
			

Update the WebMap Item Definition

Commit the change and witness the group layer added as the bottom layer of the WebMap layers. You could use the code from this blog post to reorder the WebMap layers.

				
					## set the operationalLayers property to our new list of layers reordered
webmap.definition.operationalLayers = new_ol

## we need to update the item definition for the reorder to take affect
item_properties = {"text":webmap.definition}
wm_item.update(item_properties=item_properties)
				
			

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.mapping import WebMap

################################################################################
## API Reference Links:
##  https://developers.arcgis.com/python/api-reference/arcgis.mapping.toc.html#webmap
##  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.update
##
## Description:
##  There is currently no nice method to create a group layer. Here's a workaround.
##
## Notes:
##
## API Version: 2.3.0
##
################################################################################

################################################################################
## ACCESS AGOL #################################################################

agol = GIS("home")

################################################################################
## ACCESS THE WEBMAP ITEM ######################################################

wm_item = agol.content.get("WM_ITEM_ID")

################################################################################
## CREATE A WEBMAP OBJECT ######################################################

webmap = WebMap(wm_item)

################################################################################
## GET LAYER DEFINITIONS #######################################################

spa = webmap.get_layer(title="Special Protected Areas (SPA)")
sac = webmap.get_layer(title="Special Areas of Conservation (SAC)")
nha = webmap.get_layer(title="National Heritage Areas (NHA)")
pnha = webmap.get_layer(title="Proposed Natural Heritage Areas (pNHA)")

################################################################################
## GROUP LAYER DEFINITIONS #####################################################

group_lyrs = [
        spa,
        sac,
        nha,
        pnha
]

################################################################################
## CREATE NEW OPERATIONAL LAYERS GROUP #########################################

## get all layer titles for the group
lyr_names = [lyr.title for lyr in group_lyrs]

## create the dictionary that defines a group layer in a WebMap
group = {
    "title" : "NPWS Designated Boundaries",
    "layers" : group_lyrs,
    "layerType": "GroupLayer",
    "visibilityMode": "independent"
}

## create a group to store the updated operation layers property
## the group layers are added to the group
new_ol = [group]

## append in any other layers not in the group
for lyr in webmap.definition.operationalLayers:
    if lyr.title not in lyr_names:
        new_ol.append(lyr)

################################################################################
## UPDATE THE WEBMAP DEFINITION ################################################

webmap.definition.operationalLayers = new_ol

item_properties = {"text":webmap.definition}

wm_item.update(item_properties=item_properties)

################################################################################

				
			

1 thought on “Group ArcGIS Online WebMap Layers using the ArcGIS API for Python”

  1. Pingback: Ungroup ArcGIS Online WebMap Layers from a Group Layer using the ArcGIS API for Python – FDM Geospatial Academy

Leave a Comment

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