Jump to content

Is there a method to group layers in the MapChart in Spotfire?

Dagfinn Veiberg

Recommended Posts

I have a large number of layers (30-40) in my Spotfire MapCharts

I asked Chat GPT to propose a Java script to put layers into groups. This will provide higher efficiency when working with the layers, specifically if I am able to turn on/off entire groups.

However, the suggested script did not run due to errors. What is wrong with the script?

The script is below:

var app = Spotfire.Dxp.Application.getApplication();

// Get the map chart object

var mapChart = app.getActiveDocument().getActiveVisualization();

// Get the map layers collection

var mapLayers = mapChart.MapLayers;

// Create a new group for the map layers

var layerGroup = mapLayers.addGroup("Group 1");

// Add some map layers to the group




Link to comment
Share on other sites

Hi Dagfinn,

Did you try to debug the script? What error messages are you not able to resolve? I am not saying we can help you debug a script written by ChatGPT but curious what type of results you got, where it fails. And also what specific question you asked Chat GPT.

And can you provide a bit more context around the use-case and the groupings? We don't often see users needing 30-40 layers in a map. And we can maybe share some ideas for you to experiment with. What have you tried already if anything?


Heleen Snelting

Link to comment
Share on other sites

My suggestion for a workaround: I could not find a method in the Spotfire API to create groups or add layers to groups. But we could work with a helper table, called e.g. 'groups' in which you would have two columns: the layer name ('layer') and the group it is assigned to ('group').

You could then add a selection box to a Text Area. If you want to enable multiple groups, I would set it as a List Box (multiple select) mapped to a document property called selectedGroups. This will be created automatically of type StringList.

Create a button to use this IronPython script to enable the layers corresponding to the selected group(s): the variable 'mymap' is an input variable of type Visualization that you will point to your map chart.

In the script I explicitly deselect all layers that are in the groups table but are not selected (rather than deselecting all other layers, to avoid disabling base layers like the MapLayer).

# Copyright © 2023. TIBCO Software Inc. Licensed under TIBCO BSD-style license. from Spotfire.Dxp.Application.Visuals.Maps import *from Spotfire.Dxp.Data import *  ### Find selected groupsselected_groups = Document.Properties['selectedGroups'] ### Read grouping lookup tablegroup_column='group'layer_column='layer'group_table=Document.Data.Tables['groups']rowCount = group_table.RowCount rowsToInclude = IndexSet(rowCount,True)cursor1 = DataValueCursor.CreateFormatted(group_table.Columns[group_column])cursor2 = DataValueCursor.CreateFormatted(group_table.Columns[layer_column]) # locate layers corresponding to selected groupsselected_layers=list()deselected_layers=list()for row in group_table.GetRows(rowsToInclude,cursor1,cursor2): rowIndex = row.Index val1 = cursor1.CurrentValue val2 = cursor2.CurrentValue if val1 in selected_groups: selected_layers.append(val2) else: deselected_layers.append(val2) mymap = mymap.As[MapChart]()layers = mymap.Layersfor ll in layers: if ll.Title in selected_layers: ll.Enabled = True elif ll.Title in deselected_layers: ll.Enabled = False
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...