Surface and Scatter3D do not overlap as they should


I am trying to plot a GPX track on top of a DEM surface using plotly:

This is my code:

fig.add_surface(z=alt, x=lat, y=lon, colorscale='Earth', reversescale=True, lighting=lighting_effects, lightposition=dict(x=df['Longitude'].min(), y=df['Latitude'].min(), z=ele_max))

                 mode='lines', line=dict(width=8))
fig.update_layout(width=width, height=height, 
				scene=dict(xaxis = dict(title='lat', showbackground=False), yaxis = dict(title='lon', showbackground=False), zaxis = dict(title='alt', range=[ele_min, ele_max], showbackground=False)))

The problem is that the coordinates of the track (in red) are not at all on the DEM surface, in spite of the fact that the altitude data is extracted from the same geotiff file in both cases.

It looks like a problem of scaling, but I cannot figure out what is wrong.

I am guessing that the issue comes from here:

lin_x = np.linspace(df['Longitude'].min(), df['Longitude'].max(), dem_array.shape[1])
lin_y = np.linspace(df['Latitude'].min(), df['Latitude'].max(), dem_array.shape[0])

lon, lat = np.meshgrid(lin_x, lin_y)

Ok, the following works!

dem = gdal.Open(source_file_dem)
gt  = dem.GetGeoTransform()
dem_array = dem.ReadAsArray()

xres = gt[1]
yres = gt[5]

X = np.arange(gt[0], gt[0] + dem_array.shape[1]*xres, xres)
Y = np.arange(gt[3], gt[3] + dem_array.shape[0]*yres, yres)

lon, lat = np.meshgrid(X, Y)
alt = dem_array

This is what I was trying to achieve:

Plotly is truly an amazing tool. Thanks!

1 Like