Select to view content in your preferred language

Copy Pop-ups Between Web Map Layers help (2.3 to 2.4 Update)

465
2
Jump to solution
11-19-2024 07:16 AM
Labels (2)
David_SumanHCCSD
Emerging Contributor

Hi Everyone,

Over the past year or two I've been using an older script posted that copied popups between webmap layers. See the script below.

 

# This script assigns a pop up from one web map layer (source) to another web map layer (target).
# User to provide the following inputs:
    # web map 32-character id for both the source and target web map. If the layers are in the same webmap the id will be the same.
    # exact name of layer in source web map with existing popup to copy (source layer)
    # exact name of layer in target web map to which the popup will be assigned to (target layer)
    # ArcGIS Online credentials: url, username & password

### WARNING: THIS SCRIPT WILL COMPLETELY REMOVE THE EXISTING POPUP FROM THE TARGET LAYER AND ASSIGN IT WITH THE SOURCE LAYER POPUP ###
    
############## USER INPUTS ###################################################################
    
account = '' # AGOL/Portal url
username = '' # AGOL Username
password = '' # AGOL password
webmap_id_source = '' #  32-character id of web map with the source layer
webmap_id_target = '' #  32-character id of web map with the target layer 
copy_popup_from_this_layer = '' # insert the exact layer name as it is in the source webmap
paste_popup_to_this_layer = ''  # insert the exact layer name as it is in the target webmap

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

### EXECUTE ###

#Import
from arcgis.gis import GIS
from arcgis.mapping import WebMap
import sys

# Check layers names and web maps are not identical
if webmap_id_source == webmap_id_target and copy_popup_from_this_layer == paste_popup_to_this_layer:
    sys.exit("Script cancelled: Source and Target layers have the same name and are within the same web map. Please rename one layer before continuing")
    
#Connect to GIS
gis = GIS(account,username, password)
source_webmap = gis.content.get(webmap_id_source)
target_webmap = gis.content.get(webmap_id_target)

# Get web map and all its layers if the source and target layers are in different web maps. Define source and target layers.
if webmap_id_source != webmap_id_target:
    web_map_obj_source = WebMap(source_webmap)
    web_map_obj_target = WebMap(target_webmap)
    layers_sourceWM = web_map_obj_source.layers
    layers_targetWM = web_map_obj_target.layers
    source_layer = [i for i in layers_sourceWM if i.title == copy_popup_from_this_layer]
    target_layer = [i for i in layers_targetWM if i.title == paste_popup_to_this_layer]
   
# Get web map and all its layers if the source and target layers are in the same web map. Define source and target layers.
if webmap_id_source == webmap_id_target:
    web_map_obj_target = WebMap(source_webmap)
    layersWM = web_map_obj_target.layers
    source_layer = [i for i in layersWM if i.title == copy_popup_from_this_layer]
    target_layer = [i for i in layersWM if i.title == paste_popup_to_this_layer]

# Check there's one source or target layer with the same name in the map. If not, report and cancel script.
if len(source_layer) == 0:
    sys.exit("Script cancelled: source layer name doesn't exist in web map")
if len(target_layer) == 0:
    sys.exit("Script cancelled: target layer name doesn't exist in web map")
if len(source_layer) >1:
    sys.exit("Script cancelled: more than one layer in the web map has the source name. Not sure which one you want to copy the pop-up from! Please make sure the layer has a unique name (you can change it back after you run the script)")
if len(target_layer) >1:
    sys.exit("Script cancelled: more than one layer in the target web map has the target name. Not sure which one you want to copy the pop-up to! Please make sure the layer has a unique name (you can change it back after you run the script)")
    
# Paste the source popup to the target layer
target_layer[0].update({'popupInfo':source_layer[0].popupInfo})

# Update/save webmap
web_map_obj_target.update()

 

 

With the update from 2.3 to 2.4 it seems to have broken many parts of the code. I'm aware of the module and class change from arcgis.mapping/Webmap to arcgis.map/Map but I'm having issues with other parts of the code, such as the Webmap function and calling layers from these webmaps.

 

 

 

if webmap_id_source != webmap_id_target:
    web_map_obj_source = WebMap(source_webmap)
    web_map_obj_target = WebMap(target_webmap)
    layers_sourceWM = web_map_obj_source.layers
    layers_targetWM = web_map_obj_target.layers
    source_layer = [i for i in layers_sourceWM if i.title == copy_popup_from_this_layer]
    target_layer = [i for i in layers_targetWM if i.title == paste_popup_to_this_layer]

 

I've spent a bit trying to fix the script myself but I'm still fairly new to coding and haven't been able to figure it out. This script saves a massive amount of time, if anyone could help me out I'd greatly appreciate it!!

1 Solution

Accepted Solutions
Clubdebambos
MVP Regular Contributor

Hi @David_SumanHCCSD,

Try the below. Each section is commented. No need to utilise WebMap (2.3.0) or Map (2.4.0) and just manipulate the JSON directly.

As always, test before implementing. 

from arcgis.gis import GIS

## access AGOL
agol = GIS("home")

## the source and target layer names
source_layer_name = "SOURCE_LAYER_NAME"
target_layer_name = "TARGET_LAYER_NAME"

## get the source WebMap Item
source_wm_item = agol.content.get("SOURCE_WM_ID") # add Item ID
## get the source WebMap JSON definition
source_wm_data = source_wm_item.get_data()
## get the popupInfo definition from the source layer
popup_def = [lyr["popupInfo"] for lyr in source_wm_data["operationalLayers"] if lyr["title"] == source_layer_name][0]


## get the target WebMap Item
target_wm_item = agol.content.get("TARGET_WM_ID") # add Item ID
## get the target WebMap JSON definition
target_wm_data = target_wm_item.get_data()
## get the index in the operationalLayers that the target layer sits
target_lyr_index = [index for index, lyr in enumerate(target_wm_data["operationalLayers"]) if lyr["title"] == target_layer_name][0]

## update the target layer popupInfo definition
target_wm_data["operationalLayers"][target_lyr_index]["popupInfo"] = popup_def


## apply the update
item_properties = {"text" : target_wm_data}
target_wm_item.update(item_properties=item_properties)

 

All the best,

Glen

~ learn.finaldraftmapping.com

View solution in original post

0 Kudos
2 Replies
Clubdebambos
MVP Regular Contributor

Hi @David_SumanHCCSD,

Try the below. Each section is commented. No need to utilise WebMap (2.3.0) or Map (2.4.0) and just manipulate the JSON directly.

As always, test before implementing. 

from arcgis.gis import GIS

## access AGOL
agol = GIS("home")

## the source and target layer names
source_layer_name = "SOURCE_LAYER_NAME"
target_layer_name = "TARGET_LAYER_NAME"

## get the source WebMap Item
source_wm_item = agol.content.get("SOURCE_WM_ID") # add Item ID
## get the source WebMap JSON definition
source_wm_data = source_wm_item.get_data()
## get the popupInfo definition from the source layer
popup_def = [lyr["popupInfo"] for lyr in source_wm_data["operationalLayers"] if lyr["title"] == source_layer_name][0]


## get the target WebMap Item
target_wm_item = agol.content.get("TARGET_WM_ID") # add Item ID
## get the target WebMap JSON definition
target_wm_data = target_wm_item.get_data()
## get the index in the operationalLayers that the target layer sits
target_lyr_index = [index for index, lyr in enumerate(target_wm_data["operationalLayers"]) if lyr["title"] == target_layer_name][0]

## update the target layer popupInfo definition
target_wm_data["operationalLayers"][target_lyr_index]["popupInfo"] = popup_def


## apply the update
item_properties = {"text" : target_wm_data}
target_wm_item.update(item_properties=item_properties)

 

All the best,

Glen

~ learn.finaldraftmapping.com
0 Kudos
David_SumanHCCSD
Emerging Contributor

This worked perfectly, thank you very much!!

0 Kudos