Programmatically select Polygon

Hi,

How can I programmatically select a Polygon from a gv.Polygons in a callback ?

I have a Button, that, when clicked, will trigger a callback that increases the selection index.

To be precise, I don’t want to retrieve a Polygon’s data after it is selected by the user on the plot.
I want to “trigger” a Tap selection event programmatically on a specific index.

Previously I did like this but it is quite heavy so I hope there is (with recent versions) a better way to do this:

ftRenderer = plot.handles['glyph_renderer']
def changeSelected(attr, old, new):
    global ftRenderer
    if ftRenderer:
        newSource = copy.copy(ftRenderer.data_source)
        newSource.selected.indices = new
        ftRenderer.data_source = newSource
        ftRenderer.view.source = newSource

Tried
renderer.get_plot(ft).selected = [idxSelect]
without success

In recent versions a fair number of elements (Polygons included) support a selected plot option, which should be sufficient for your purposes.

I tried with selected but nothing happens on the plot

Here is a part of the code I’m using :

ft = gv.Polygons(footprints_data, vdims=['startdate', 'color'], crs=proj).opts(
line_width=2, selected=[0])

mapPlot = pn.panel((gf.coastline * gf.land * gf.ocean.opts(projection=proj, width=1100, height=800, padding=0.1,
                                                           axiswise=True, title="Cyclone track and acquisitions",
                                                           tools=['tap'],
                                                           global_extent=True) * ft )

nextButton = pn.widgets.Button(name="Next")
idxSelect = 0

def next(event):
    global idxSelect
    idxSelect += 1
    ft.opts(selected=[idxSelect])

nextButton.on_click(next)

I’d suggest brushing up on how dynamic plotting works in HoloViews by reading the Live Data user guide and maybe some following on from that. Here’s one way of doing what you are trying (with some random data since I don’t have your footprints_data):

nextButton = pn.widgets.Button(name="Next")

poly = gv.Polygons([hv.Ellipse(i*5, i*5, i) for i in range(10)]).opts(
    line_width=2)

@pn.depends(nextButton.param.clicks) 
def select_poly(clicks):
    return poly.opts(selected=[clicks])

dynamic_poly = hv.DynamicMap(select_poly)

ocean = gf.ocean.opts(width=1100, height=800, padding=0.1,
                      axiswise=True, title="Cyclone track and acquisitions",
                      tools=['tap'], global_extent=True)

pn.Row(gf.coastline * gf.land * ocean * dynamic_poly, nextButton)

1 Like