Select to view content in your preferred language

Create nested group layers in a Web Map

84
1
Tuesday
Labels (1)
MaryHooper
New Contributor

I am trying to create nested group layers using Python. I am able to create group layers and add them to a webmap on Enterprise Portal 11.2. I wrote the code several different ways and haven't been able to get nested group layers to work. The code below works for group layer, but when I add the line that would add the nested layer it doesn't work. I get an this error: AttributeError: 'GroupLayer' object has no attribute 'properties'. 

This is the simplest version of the code. Any help is appreciated.

MaryHooper_1-1766509597651.png

 

 

 

 

0 Kudos
1 Reply
Brian_McLeer
Frequent Contributor

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

 

0 Kudos