Copy an ArcGIS Online WebMap Popup and Apply to another Layer 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 components of ArcGIS Online along with performing analysis tasks. In this blog post we will focus on updating a popup for a layer in a WebMap based on the popup from a layer in another WebMap.

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.

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.

				
					## provides access to ArcGIS Online
from arcgis.gis import GIS
				
			

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

User Inputs

We need some user inputs. We want to get the popup definition from a source layer in a source WebMap so we require the Item ID for the source WebMap and the name of the layer to get the popup definition from. We also need the same information for the target WebMap and the target layer name.

				
					## the Item Id for the WebMap that contains the source layer
source_item_id = "SOURCE_WM_ITEM_ID"

## the source layer name as it appears in the WebMap
source_lyr_name = "SOURCE_LAYER_NAME"

## the Item Id for the WebMap that contains the target layer
target_item_id = "TARGET_WM_ITEM_ID"

## the target layer name as it appears in the WebMap
target_lyr_name = "TARGET_LAYER_NAME"
				
			

Get the Item Objects

We use the Item IDs for our source and target WebMaps and create Item objects. These objects will enable us to access the information we need from one WebMap and update in the other.

				
					## get the source WebMap as an Item object
source_wm_item = agol.content.get(source_item_id)

## get the target WebMap as an Item object
target_wm_item = agol.content.get(target_item_id)
				
			

Get the WebMap JSON Definition

We get the WebMap JSON definition for both our source and target WebMaps. The Item Object get_data() method returns the definition as a dictionary. We are interested in the opertaionalLayers property returned as it contains the information for each layer and will allow us the extract the popup definition for our layer of interest.

				
					## get the source WebMap JSON definition
source_wm_item_data = source_wm_item.get_data()

## get the target WebMap JSON definition
target_wm_item_data = target_wm_item.get_data()
				
			

Get the Popup Definition from our Source Layer

We iterate over every layer in the operationalLayers list until we find the layer that matches the layer name we are after. Once found, we assign the popupInfo definition to a variable that we can use later to update the popup definition in another WebMap. In the code below, we also cater for digging into Group Layers.

				
					## get the list of layers
source_ol = source_wm_item_data["operationalLayers"]

## iterate over each layer
for lyr in source_ol:
    ## if it is a group layer
    if lyr["layerType"] == "GroupLayer":
        ## iterate over layer in the group
        for group_lyr in lyr["layers"]:
            ## if you find the layer
            if group_lyr["title"] == source_lyr_name:
                ## get the popupInfo
                popup_info = group_lyr["popupInfo"]
                break
    ## if not a group layer and a standalone layer
    elif lyr["layerType"] == "ArcGISFeatureLayer":
        ## if you find the layer
        if lyr["title"] == source_lyr_name:
            ## get the popupInfo
            popup_info = lyr["popupInfo"]
            break

## no need to keep the below as we have the popupInfo
del source_item_id, source_lyr_name, source_ol, source_wm_item, source_wm_item_data
				
			

Update the operationaLayer List in the Target WebMap

Now that we have our popup definition, we iterate over the layers in our target WebMap until we find the layer of interest and update the popup definition. This alone does not update the target WebMap. See the next section for information.

				
					## get the list of layers
target_ol = target_wm_item_data["operationalLayers"]

## iterate over each layer
for lyr in target_ol:
    ## if it is a group layer
    if lyr["layerType"] == "GroupLayer":
        ## iterate over layer in the group
        for group_lyr in lyr["layers"]:
            ## if you find the layer
            if group_lyr["title"] == target_lyr_name:
                ## assign the popupInfo
                group_lyr["popupInfo"] = popup_info
                break
    ## if not a group layer and a standalone layer
    elif lyr["layerType"] == "ArcGISFeatureLayer":
        ## if you find the layer
        if lyr["title"] == target_lyr_name:
            ## assign the popupInfo
            lyr["popupInfo"] = popup_info
            break
				
			

Update the Target WebMap Item Object

All there is left to do is to update the target WebMab Item Object with a new operationalLayer property that represents the update to the popup definition performed. You could update any component of the layer that is accessible via the operationalLayers JSON definition. In this workflow we simply focused on popups.

				
					## update to the new operationLayers list
target_wm_item_data["operationalLayers"] = target_ol

## set the update properties
update_properties = {"text" : target_wm_item_data}

## update the target WebMap Item Object
target_wm_item.update(item_properties=update_properties)
				
			

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.

				
					## provides access to ArcGIS Online
from arcgis.gis import GIS

################################################################################
## API Reference Links:
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#gis
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#arcgis.gis.ContentManager.get
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#item
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.get_data
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.update
##
## API Version: 2.1.0.2, 2.2.0.1, 2.3.0, 2.4.0
##
################################################################################

################################################################################
## ACCESS ARCGIS ONLINE ########################################################

agol = GIS("home")

################################################################################
## USER INPUTS #################################################################

## the Item Id for the WebMap that contains the source layer
source_item_id = "SOURCE_WM_ITEM_ID"

## the source layer name as it appears in the WebMap
source_lyr_name = "SOURCE_LAYER_NAME"

## the Item Id for the WebMap that contains the target layer
target_item_id = "TARGET_WM_ITEM_ID"

## the target layer name as it appears in the WebMap
target_lyr_name = "TARGET_LAYER_NAME"

################################################################################
## GET THE ITEM OBJECTS ########################################################

## get the source WebMap as an Item object
source_wm_item = agol.content.get(source_item_id)

## get the target WebMap as an Item object
target_wm_item = agol.content.get(target_item_id)

################################################################################
## GET THE WEBMAP JSON DEFINITIONS #############################################

## get the source WebMap JSON definition
source_wm_item_data = source_wm_item.get_data()

## get the target WebMap JSON definition
target_wm_item_data = target_wm_item.get_data()

################################################################################
## GET THE POPUP DEFINITION FOR THE SOURCE LAYER ###############################

## get the list of layers
source_ol = source_wm_item_data["operationalLayers"]

## iterate over each layer
for lyr in source_ol:
    ## if it is a group layer
    if lyr["layerType"] == "GroupLayer":
        ## iterate over layer in the group
        for group_lyr in lyr["layers"]:
            ## if you find the layer
            if group_lyr["title"] == source_lyr_name:
                ## get the popupInfo
                popup_info = group_lyr["popupInfo"]
                break
    ## if not a group layer and a standalone layer
    elif lyr["layerType"] == "ArcGISFeatureLayer":
        ## if you find the layer
        if lyr["title"] == source_lyr_name:
            ## get the popupInfo
            popup_info = lyr["popupInfo"]
            break

## no need to keep the below as we have the popupInfo
del source_item_id, source_lyr_name, source_ol, source_wm_item, source_wm_item_data

################################################################################
## UPDATE THE TARGET LAYER POPUP ###############################################

## get the list of layers
target_ol = target_wm_item_data["operationalLayers"]

## iterate over each layer
for lyr in target_ol:
    ## if it is a group layer
    if lyr["layerType"] == "GroupLayer":
        ## iterate over layer in the group
        for group_lyr in lyr["layers"]:
            ## if you find the layer
            if group_lyr["title"] == target_lyr_name:
                ## assign the popupInfo
                group_lyr["popupInfo"] = popup_info
                break
    ## if not a group layer and a standalone layer
    elif lyr["layerType"] == "ArcGISFeatureLayer":
        ## if you find the layer
        if lyr["title"] == target_lyr_name:
            ## assign the popupInfo
            lyr["popupInfo"] = popup_info
            break

################################################################################
## UPDATE THE WEBMAP ITEM OBJECT ###############################################

## update to the new operationLayers list
target_wm_item_data["operationalLayers"] = target_ol

## set the update properties
update_properties = {"text" : target_wm_item_data}

## update the target WebMap Item Object
target_wm_item.update(item_properties=update_properties)

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

Leave a Comment

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