Ungroup a Single ArcGIS Online WebMap Layer from a Group Layer 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 removing a single layer from a group layer. As of writing this post, there is no neat method for achieving this, but you can utilise components of the ArcGIS API for Python to achieve the desired result.

For this example, I am going to remove the Special Protected Areas (SPA) from the NPWS Designated Boundaries group and place back into the WebMap. This is the state of play after our previous blog post on how to Group Layers.

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 Operational Layer Information to Help Reconstruction

We need to target the correct group layer, get the index of that group layer in the Operational Layers, get the definition of the Layer to remove from the group, and create a group that contains the layers of the group minus the layer we wish to remove.

				
					## the layer to remove from the group
lyr_name = "Special Protected Areas (SPA)"

## this list will hold the group layers minus the one to remove
grp_lyrs = []

## for each layer in the operational layers
for index, lyr in enumerate(webmap.definition.operationalLayers):
    ## if group layer
    if lyr.layerType == "GroupLayer":
        ## if the layer is in the group
        if lyr_name in [l.title for l in lyr.layers]:
            ## get the ol index for the group layer
            grp_index = index
            ## for each layer in the group
            for l in lyr.layers:
                ## if it is the layer to remove, get the definition
                if l.title == lyr_name:
                    remove_lyr = l
                ## append the rest of the layers in the group
                ## into the grp_layers list
                else:
                    grp_lyrs.append(l)

				
			

Reconstruct the Operational Layers

We reconstruct the Operational Layers to or specific definition with the layer removed from the group and placed as the top-most layer in the WebMap.

				
					## make a copy of the current operationa layers
new_ol = webmap.definition.operationalLayers

## update the group layers list of layers
new_ol[grp_index].layers = grp_lyrs

## append the removed layer definition to the operational layers
new_ol.append(remove_lyr)
				
			

Update the WebMap Item Definition

We commit the change and witness that the layer has been removed from the group layer and placed as the top layer in the WebMap. You could use the code from this blog post to reorder the WebMap layers.

				
					webmap.definition.operationalLayers = new_ol

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:
##  Remove a single layer from a group layer.
##
## 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)

################################################################################
## OPERATIONAL LAYERS INFO #####################################################

## the layer to remove from the group
lyr_name = "Special Protected Areas (SPA)"

## this list will hold the group layers minus the one to remove
grp_lyrs = []

## for each layer in the operational layers
for index, lyr in enumerate(webmap.definition.operationalLayers):
    ## if group layer
    if lyr.layerType == "GroupLayer":
        ## if the layer is in the group
        if lyr_name in [l.title for l in lyr.layers]:
            ## get the ol index for the group layer
            grp_index = index
            ## for each layer in the group
            for l in lyr.layers:
                ## if it is the layer to remove, get the definition
                if l.title == lyr_name:
                    remove_lyr = l
                ## append the rest of the layers in the group
                ## into the grp_layers list
                else:
                    grp_lyrs.append(l)

################################################################################
## RECONSTRUCT THE OPERATIONAL LAYERS ##########################################

## make a copy of the current operationa layers
new_ol = webmap.definition.operationalLayers

## update the group layers list of layers
new_ol[grp_index].layers = grp_lyrs

## append the removed layer definition to the operational layers
new_ol.append(remove_lyr)

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

webmap.definition.operationalLayers = new_ol

item_properties = {"text":webmap.definition}

wm_item.update(item_properties=item_properties)

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

				
			

Leave a Comment

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