Ungroup ArcGIS Online WebMap Layers 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 updating a webmap by altering the definition to ungroup layers and remove the group layer from the WebMap. As of writing this post, there is no neat method for ungrouping a group layer, although I have it under good authority that this functionality is being added to the API before the end of 2024.

For this example, we will follow on from the other blogs in this series where we grouped four layers, removed a single layer from the group, and now we will ungroup the other three layers all at once from the NPWS Designated Boundaries group layer 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)
				
			

Reconstruct the Operational Layers

Here, we will create a list and append in the layer definitions, removing the group layer definition.

				
					## the name of the group layer to ungroup
group_name = "NPWS Designated Boundaries"

## this will hold a new operational layers definition
new_ol = []

## for each layer in the ol
for lyr in webmap.definition.operationalLayers:
    ## find the group layer
    if lyr.title == group_name:
        ## for each layer in the group layer
        for sub_lyr in lyr.layers:
            ## append to the new ol
            new_ol.append(sub_lyr)
    else:
        ## append all other layers into th new ol
        new_ol.append(lyr)
				
			

Update the WebMap Item Definition

Commit the change and witness the group layer has been removed from the WebMap and each individual layer moved out of the group. 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:
##  Ungroup layers from a group layer.
##
## API Version: 2.2.0.1
##
################################################################################

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

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

## the name of the group layer to ungroup
group_name = "NPWS Designated Boundaries"

## this will hold a new operational layers definition
new_ol = []

## for each layer in the ol
for lyr in webmap.definition.operationalLayers:
    ## find the group layer
    if lyr.title == group_name:
        ## for each layer in the group layer
        for sub_lyr in lyr.layers:
            ## append to the new ol
            new_ol.append(sub_lyr)
    else:
        ## append all other layers into th new ol
        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)

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

Leave a Comment

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