Select to view content in your preferred language

adding multiple layers from webmap in ArcGIS storymap

174
1
02-21-2025 08:14 AM
sumeerkoirala_nowic
New Contributor

"I am currently working on creating an ArcGIS StoryMap and have set up a WebMap with 4 operational layers. Using the Python API, I want to add all four of these layers to the StoryMap. However, the issue I am encountering is that only one layer is visible in the StoryMap. In the manual process, I can add the WebMap and then hide all other layers, keeping the visibility of just the layer I want. How can I achieve this using Python?"


I am using this script;

import json
from arcgis.gis import GIS
from arcgis.apps.storymap import StoryMap, Text, Map
import sys

# WebMap ID (replace with your WebMap ID)
webmap_item_id = "de58075d229a4ff6933e912c0d7c33f3" # Replace with your WebMap ID

# Fetch the WebMap item using the WebMap ID
webmap_item = gis.content.get(webmap_item_id)

if webmap_item:
try:
# Retrieve WebMap JSON to access operational layers and extent
webmap_json = webmap_item.get_data()
operational_layers = webmap_json.get("operationalLayers", [])

# Extract initial extent from WebMap (if available)
initial_extent = webmap_json.get("initialExtent")

# Check if operational layers are found
if not operational_layers:
print(":warning: No operational layers found in the WebMap.")
sys.exit()
else:
print(f"Found {len(operational_layers)} operational layers.")

# Create the StoryMap
story = StoryMap()

# Loop through the operational layers and add each as a separate map
for i, layer in enumerate(operational_layers):
layer_title = layer.get("title", f"Layer {i+1}")

print(f"🗺️ Processing Layer: {layer_title}")

# Configure the layer visibility (only one layer visible at a time)
layers_config = [{"id": l.get("id"), "visible": (l.get("id") == layer.get("id"))}
for l in operational_layers]

# Create the Map object with the configured layers
map_content = Map(webmap_item, layers=layers_config)

# Add a title and the map section to the StoryMap
story.add(Text(f"Section {i+1}: {layer_title}"))
story.add(map_content)

# Set the viewpoint for the map section (use initial_extent or custom scale)
if initial_extent:
map_content.set_viewpoint(extent=initial_extent)
else:
map_content.set_viewpoint(scale=25000) # Default scale

# Save and publish the StoryMap
try:
story_item = story.save("My StoryMap with Multiple Layers", publish=True)
print(f"StoryMap created and published: {story_item.title}")

except Exception as e:
print(f"Error during StoryMap creation or publishing: {e}")
sys.exit()

# Fetch and print the published StoryMap JSON
try:
story_json = story_item.get_data() # Fetch StoryMap data
print("🔍 Published StoryMap JSON:")
print(json.dumps(story_json, indent=4)) # Pretty-print the JSON for easy reading
except Exception as e:
print(f"Error fetching StoryMap JSON: {e}")
sys.exit()

except Exception as e:
print(f"Error during WebMap processing: {e}")
sys.exit()

else:
print(f"WebMap with ID {webmap_item_id} not found.")
sys.exit()



here are screenshots:

sumeerkoirala_nowic_0-1740154444586.png

 

0 Kudos
1 Reply
ThePreatorian
Esri Contributor

@sumeerkoirala_nowic  I think the way to set visibility in the Map class requires you to create an instance and then change the visibility of the layers in the ._map_layers attribute.

Here is a code snippet:

map_content = Map(map_item) 
map_content._map_layers
for layer in map_content._map_layers:
    # Add logic here to determine which layers need to show
    layer['visibility'] = True
new_sm.add(map_content)

#Add map to the StoryMap


I think it would be good if there was an easier method to change the visibilty of layers in the Map class. I have created an issue with the python team to look into improving this part of the StoryMaps module.

0 Kudos