I am currently working on a Panel app where a first step is to ask the user to select a region on a map. I am able to use geoview’s Polygons to show this map, and if I add the tool “Tap”, I am able to select one or more polygons in the map. However, I am unable to retrieve this information! It seems that the tap selection works at the bokeh level and I haven’t been able to find where to get the info in holoviews…
Minimal working example as I do it in my notebook:
import geopandas as gpd
import geoviews as gv
import holoviews as hv
gv.extension('bokeh')
hv.extension('bokeh')
countries = gpd.read_file('https://github.com/johan/world.geo.json/raw/master/countries.geo.json')
polys = gv.Polygons(countries, vdims=['name'])
polys.opts(
tools=['hover', 'tap'], width=600,
)
This allows you to select 1 or more countries, but how do you get the info?
(Minor question : Here the colors are magically generated from the name attribute. Is there a way to put a single color for all shapes?)
Thanks!
In current HoloViews (1.12.7) you can already get the selected indices like this:
polys = gv.Polygons(countries, vdims=['name'])
sel = hv.streams.Selection1D(source=polys)
polys.opts(
tools=['hover', 'tap'], width=600,
)
Then select a few polygons and access the selection index like this:
sel.index
Starting in the next release of HoloViews (1.13.0) and GeoViews (1.7) you’ll then also apply that selection directly on the Polygons element:
polys.iloc[sel.index]
You might also be happy to hear that for Bokeh 2.0 (due next week) I implemented box_select
for polygons.
(Minor question : Here the colors are magically generated from the name attribute. Is there a way to put a single color for all shapes?)
This is a fairly unfortunate decision, which will be addressed in the near future. For now you have to set the color_index=None
and then set the desired color:
polys.opts(
color_index=None, color='red', tools=['hover', 'tap'], width=600,
)
1 Like
Thanks a lot! I expected a simple solution like that, I just couldn’t find it, nice!