Trimesh value at given x,y

I am using Trimesh but now would like to obtain the interpolated value of vdims at a particular x,y location without necessarily displaying the Trimesh. I looked through holoviews datashader operations and could not find a way to do so. Can someone help or point me in the right direction?

Hi,
maybe a non working example can push you in the right direction.
In my usecase I visualize data with spatial reference. The X-, Y- coordinates and variable data are wrapped in a pandas dataframe. The interensting part for you might be the tooltips thing. The example is not working because theirs no data.

from bokeh.models import HoverTool
import holoviews as hv
import geoviews as gv
import panel as pn

dict_rasterized_data = {}
dict_tooltips = {}

variable_lst= ['variable_1', 'variable_2', 'variable_3']

First I construct a dictionairy with the datashaded trimesh. Therefore I am able to change between different variables in the aftermath. In a second dict I put the tooltips. If you don’t declare tooltips you are not able to decide what the hover shows.
If you have just one value dimension you can skip the for-loop and ignore the select options. But even then you might want to change the colormap.

for v in variable_lst:
    points = gv.Points(df, crs=crs.epsg(25832), kdims=['X','Y'], vdims = v)
    dict_tooltips][v] = [(v, '@image')]  # this is the part where the tooltips are specified
    d = gv.Dataset(gv.operation.project_points(Points))
    dict_rasterized_data[v] = rasterize(hv.TriMesh((element, d)),
                             aggregator=ds.mean(v), precompute=True, dynamic=True)

select_variable = pn.widgets.RadioButtonGroup(name='Variable', options=variable_lst)

@pn.depends(sel_var = select_variable.param.value, watch=False)
def hovertool(sel_var):
    hover = dict_tooltips][sel_var]
    return hover

@pn.depends(sel_var = select_variable.param.value, watch=False)
def react_view(sel_var):
    view = dict_data[sel_var].opts(cmap='viridis', tools=[hovertool()] width=500, height=500)
    return view

Because of the selections I put it together in a Panel Layout.

pn.Row(react_view, select_variable)

Thanks for the share. I am currently displaying a trimesh with hover using rasterize just like your code.

Now I have an array of points (x,y) and would like to sample the trimesh values at those locations. If I understand your code, I would have to hover over those locations. I would rather just be able to do this without displaying the trimesh or interacting with it visually.

Is there a way to call and get hover values (without displaying) with your setup above?

If you have rasterized it you can index the resulting index element with img[x, y] which will return the value at that location.

Thanks. I came up with this solution. I wonder if it can be improved or there is a better way

trimesh = hv.TriMesh((build_trimesh_simplex(dfe), hv.Points(dfn0,vdims='z')))
x,y,z=tuple(dfn0.iloc[0,:]) # x,y, z values at a node of the trimesh

# borrowed from the datashader code
import datashader as ds
# create a 3x3 pixel image with a range of x,y of +/- 1
cvs=ds.Canvas(plot_width=3,plot_height=3,x_range=(x-1,x+1),y_range=(y-1,y+1))
simplices = trimesh.dframe([0, 1, 2])
verts = trimesh.nodes.dframe([0, 1, 3])
for c, dtype in zip(simplices.columns[:3], simplices.dtypes):
    if dtype.kind != 'i':
        simplices[c] = simplices[c].astype('int')

# calculate xarray
result=cvs.trimesh(verts, simplices, mesh=ds.utils.mesh(verts, simplices), agg=ds.mean('z'))

# check if the value at the x,y is the same as z (node value)
result.sel(x=x,y=y).values==z

I think I missunderstood your question… anyway I’m glad to see that you find a solution.