Here is a working example of clicking on a bokeh map and having a callback. Currently though the points are not georeferenced and appear to be scaled down and appear in the meddle of the map. Ideally I would not only show text but be able to add a hvplot as a Pop up or at least on the side of the map. Can anyone help add the projection and if they can help me with the Tap to show hvplot (any data would do) it would be great!!!
import pandas as pd
import holoviews as hv
from holoviews import opts
from holoviews.streams import Tap
import geoviews.tile_sources as gts
import geoviews as gv
import cartopy.crs as ccrs
import numpy as np
# Suppress warnings related to non-parameter attributes
hv.config.warn_options_call = False
hv.extension('bokeh')
# Create a sample DataFrame with latitude and longitude columns
data = pd.DataFrame({'lat': [51.5074, 48.8566, 40.7128],
'lon': [-0.1278, 2.3522, -74.0060],
'city': ['London', 'Paris', 'New York']})
# Function to handle the marker click event
def marker_info(x, y):
if np.isnan(x) or np.isnan(y):
return hv.Text(0,0,f'Click on a station', halign='left', valign='bottom')
else:
data['distance'] = ((data['lat'] - y) ** 2 + (data['lon'] - x) ** 2) ** 0.5
selected_point = data.loc[data['distance'].idxmin()]
info = f"Latitude: {selected_point['lat']}, Longitude: {selected_point['lon']}, City: {selected_point['city']}"
return hv.Text(x,y,info, halign='left', valign='bottom')
# Create a DynamicMap that responds to the click event on the markers
tap_stream = Tap(transient=True, x=np.nan, y=np.nan)
marker_dmap = hv.DynamicMap(marker_info, streams=[tap_stream])
# Create a map with markers from the DataFrame and add the DynamicMap with the marker information
markers = hv.Points(data, kdims=['lat', 'lon'], vdims=['city'], crs=ccrs.PlateCarree()).opts(
size=10,
color='red',
tools=['tap']
)
# Set the CRS for the tile source to Plate Carrée projection
tile_source = gv.tile_sources.OSM().clone(crs=ccrs.PlateCarree())
map_with_markers = (tile_source * markers * marker_dmap).opts(
opts.Points(active_tools=['tap']),
opts.WMTS(width=800, height=800, xlim=(-180, 180), ylim=(-90, 90))
)
# Display the map
map_with_markers