- Is managing your map layers a bit of a hassle?
- Would you like to be able to turn layers on and off with the click of a button?
I had a request from a user last week to be able to turn map layers on and off with the click of a button. I knew this would require a bit of IronPython, so I turned to one of my Ruths.ai colleagues, Lucas Wood, who was able to supply a few simple lines of code to make this work. You can download a example DXP here: Toggle Script Here are the steps needed to apply this solution.
- In the map chart, right click on the Map > Properties
- Go to the Layers menu
- Select the desired layer
- Click the Settings button
- Go to the General menu and name the layer something that is distinctive but simple, such as ‘Swedish’
- Click Close to exit
- Add a text area to the page
- Right click and select ‘Edit Text Area’
- Click the Add Action Control button
- Name the control as desired, such as “Toggle Sweden Off/On”
- Click the Script button the left hand side of the dialog
- Click New
- Name the script, such as ‘toggle script’
- Copy and paste the code below
- Edit the script so that the name of the script is specified in the 5th line of code within quotation marks
- Add a Script parameter called “viz”.
- Set the ‘Type:’ drop down to ‘Visualization’
- Select the map from the ‘Select visualization’ drop down
- Click Ok to exit
- Run the script to make sure there are no errors
- Click OK twice to exit
- The button should now work to toggle the feature layer on and off
from Spotfire.Dxp.Application.Visuals import *
v = viz.As[VisualContent]()
for l in v.Layers:
if l.Title == “Swedish”:
if l.Enabled == True:
l.Enabled = False
l.Enabled = True
I also want to note two more things before wrapping up.
- This script should work for any layer — feature, marker, WMS, etc — because the layer type is not specified, only the layer name.
- The same script will work to turn off multiple layers, just copy and paste, and specify each layer you want to turn on and off with one click of the button.
Guest Spotfire blogger residing in Whitefish, MT. Working for SM Energy’s Advanced Analytics and Emerging Technology team!