- 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
Code:
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
else:
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.
Happy scripting!!
Guest Spotfire blogger residing in Whitefish, MT. Working for SM Energy’s Advanced Analytics and Emerging Technology team!
I am getting an error at line at 5. The error is : unexpected token “”
Please help.
Screenshot please.
Sorry I am new to this but I am not sure what you meant in step 15
I got the same error: File “, line 5
if l.Title ==“Swedish”:
^
SyntaxError: unexpected token ‘“’
I was trying to insert the screenshot but I could not find the upload option
FYI: I have followed everything but was not sure what to change in line 5 for the code so I left with nothing changed.
you need to change the quotes, when you copy from the browser you get the stylistic quotes(”), not the coding quotes (“)
Now I know the correct terminology for this….stylistic versus coding quotes. Thanks!
Hey , Can you help me how to change the Interactive Layer in map. For eg. if the Map has 3 layers and we want to have 2 layers active while hide one layer. And we can decide which of the two active layer be the interactive layer. Can you help on the Iron python script for the same.
Thanks Julie! worked great and just what i needed. Going to combine this script with some fancy buttons here: https://community.tibco.com/wiki/tibco-spotfire-tips-and-tricks-buttons
thanks again!!
thank you for this!