I don't have a script that creates nested group layers, but I have one that updates pop-ups in webmaps. These snippets may be of use. I have webmaps that have group layers nested 2 or 3 levels down.
def find_target_group_layer(layers: List[Dict[str, Any]], target_name: str) -> Optional[Dict[str, Any]]:
"""
Find the target group layer by name, searching recursively if needed.
Args:
layers: List of layer dictionaries to search
target_name: Name of the group layer to find
Returns:
The target group layer dictionary if found, None otherwise
"""
logger.info(f"Searching for target group layer: '{target_name}'")
for layer in layers:
# Check the title first
layer_title = layer.get("title", "")
if layer_title == target_name:
logger.info(f"[SUCCESS] Found target group layer: '{target_name}'")
return layer
# If this is a group layer, search its nested layers
if "layers" in layer and layer["layers"]:
result = find_target_group_layer(layer["layers"], target_name)
if result:
return result
logger.warning(f"[WARNING] Target group layer '{target_name}' not found")
return None
def process_layers_recursive(layers: List[Dict[str, Any]], layer_name_map: Dict[int, str],
layer_path: str = "") -> int:
"""
Recursively process all layers, including nested layers within group layers.
Args:
layers: List of layer dictionaries to process
layer_name_map: Mapping of layer IDs to names
layer_path: String representing the current path (for logging)
Returns:
Count of layers successfully updated
"""
updated_count = 0
for layer in layers:
layer_title = get_layer_name(layer, layer_name_map)
full_path = f"{layer_path}/{layer_title}" if layer_path else layer_title
# Check if this is a group layer with nested layers
if "layers" in layer and layer["layers"]:
logger.info(f"Found group layer: {full_path} with {len(layer['layers'])} nested layer(s)")
# Recursively process nested layers
nested_count = process_layers_recursive(layer["layers"], layer_name_map, full_path)
updated_count += nested_count
else:
# This is a regular layer, update its popup
if update_layer_popup(layer, layer_name_map, layer_path):
updated_count += 1
return updated_count