Turn On/Off ArcGIS Online WebMap Layer Labels with 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 layer and turning labels off and on! In the video below we have a layer called County Boundaries that have the county names as lables turned on. We will use the ArcGIS API for Python to turn the labels off and back on again. Please note, for this workflow, the layer labels were already set and exist in ArcGIS Online.

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

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

Access the WebMap Layer of Interest

The WebMap object has a get_layer() method where we can supply the layer name to retrieve the dictionary definition for the layer of interest. 

				
					## get the layer of interest
lyr = webmap.get_layer(title="Name of layer as it appears in the WebMap")

				
			

Print the Current showLabels Property

If showLabels is True, labels are turned on, if False, labeling for the layer is turned off. In the example below, labeling is turned on for the County Boundaries layer.
				
					## print the layer definition "showLabels" property
if "showLabels" in lyr:
    print(lyr.showLabels)

>>> True
				
			

WebMap object update_drawing_info() method

We can use the WebMap object update_drawing_info() method and setting the show_labels parameter to False to turn off the labels for the County Boundaries layer. We call the WebMap object update() method to commit and save the change.
				
					## set show_labels to False/True
webmap.update_drawing_info(lyr, show_labels=False)

## Update and save the WebMap
webmap.update()
				
			
When we refresh the WebMap page, the labels have been turned off. You can perform the reverse and set show_labels to True for the update_drawing_info() method to turn labels on.

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.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.mapping.toc.html#webmap
##  https://developers.arcgis.com/python/api-reference/arcgis.mapping.toc.html#arcgis.mapping.WebMap.get_layer
##  https://developers.arcgis.com/python/api-reference/arcgis.mapping.toc.html#arcgis.mapping.WebMap.update_drawing_info
##  https://developers.arcgis.com/python/api-reference/arcgis.mapping.toc.html#arcgis.mapping.WebMap.update
##
## Description:
##  Turn on/off labelling with webmap pobject update_drawing_info() method
##
## API Version: 2.2.0.1
##
################################################################################

"""Access AGOL"""
agol = GIS("home")

## Access the WebMap Item
wm_item = agol.content.get("WebMap_Item_ID")

## create a WebMap object from the Item object
webmap = WebMap(wm_item)

## use the WebMap get_layer() to access the layer of interest
lyr = webmap.get_layer(title="Name of layer as it appears in the WebMap")

## print the layer definition "showLabels" property
##if "showLabels" in lyr:
##    print(lyr.showLabels)

## set show_labels to False/True
webmap.update_drawing_info(lyr, show_labels=False)

## Update and save the WebMap
webmap.update()

				
			

Leave a Comment

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