Working with multiple patches taken from a GeoDataFrame, I am visualizing a county map of the USA where for each county I have a Climate zone defined as follows:
cZone
Name
geometry
cZoneColorIndex
0
3C
San Francisco
POLYGON ((-122.511983 37.77113, -122.465396 37…
7
1
5A
Suffolk
POLYGON ((-71.19115499999999 42.283059, -71.15…
11
2
5A
Banner
POLYGON ((-104.052825235239 41.69795385306401,…
11
3
4A
Vance
POLYGON ((-78.497783 36.514477, -78.4572778962…
8
4
4A
Sherman
POLYGON ((-102.162463 36.500326, -102.03233901…
8
As the Bokeh Tap Tool bar would only choose a single entity upon click, is there a way to select all similar cZone entities?
e.g from the DF above, when the user would select the polygon representing Vance, Sherman would be added to the selection set and we would be able to manipulate its appearance.
Essentially a ‘tap’ listener and the ability to edit the ‘selection set’.
Core script as follows:
“ILLUSTRATE MAP”
#Read data to json.
json_raw = json.loads(gdf.to_json())
#Convert to String like object.
json_data = json.dumps(json_raw)
#Input GeoJSON source that contains features for plotting.
geosource = GeoJSONDataSource(geojson = json_data)
Define a sequential multi-hue color palette
palette = cividis(15)
#Instantiate LinearColorMapper that linearly maps numbers in a range, into a sequence of colors
color_mapper = LinearColorMapper(palette = palette, low = 1, high = 16)
#set fixed tick locations
ticker = FixedTicker(ticks=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
#tick values corresponding ‘ticker’ above
formatter = FuncTickFormatter (code="""
data = {1: ‘1A’, 2: ‘2A’, 3:‘2B’, 4:‘3A’, 5:‘3B-CA’, 6:‘3B-Other’, 7:‘3C’,
8:‘4A’, 9:‘4B’, 10:‘4C’, 11:‘5A’, 12:‘5B’, 13:‘6A’, 14:‘6B’, 15:‘7’, 16:’ '}
return data[tick]
“”")
#Create color bar.
color_bar = ColorBar(color_mapper=color_mapper, label_standoff=8, width = 10, height = 375,
border_line_color=None,location = (0,0), ticker=ticker, formatter=formatter, major_label_text_align=‘left’)
#Add hover tool
hover = HoverTool(tooltips = [ (‘Climate Zone’,’@cZone’),(‘County’,’@Name’)])
#Add select tool
select_tools = [‘tap’, ‘reset’]
#Create figure object.
p = figure(title = ‘USA Climate Zone Map’, plot_height = 450 , plot_width = 800, toolbar_location = “below”,
tools=select_tools)
p.add_tools(hover)
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
#Add patch renderer to figure.
p.patches(‘xs’,‘ys’, source = geosource, fill_color = {‘field’ :‘cZoneColorIndex’, ‘transform’ : color_mapper},
line_color = ‘black’, line_width = 0.25, fill_alpha = 1)
#Specify figure layout.
p.add_layout(color_bar, ‘right’)
#Display figure inline in Jupyter Notebook.
output_notebook()
Display figure.
show(p)
``
Many thanks