w3reality / three-geo

3D geographic visualization library
MIT License
1.09k stars 183 forks source link

Q: How do you calculate terrain height relative to the position units in three.js, which are also dependant on the zoom factor? #58

Open sonicviz opened 1 year ago

sonicviz commented 1 year ago

Q: How do you calculate terrain height relative to the position units in three.js, which are also dependant on the zoom factor?

For example, if I want to position an object 1m above sea level (which according to https://docs.mapbox.com/data/tilesets/guides/access-elevation-data/#mapbox-terrain-rgb sea level is 0) how do I translate that into a relative mesh position,z value that will translate it a unit of 1m (or 10m or whatever) relative to the scale of the mesh terrain elevation?

Also. how does that position calculation change based on the zoom factor, which can be from 11 to 17?

https://docs.mapbox.com/help/glossary/zoom-level/ doesn't give me much of a solution either.

Thanks!

j-devel commented 1 year ago

how does that position calculation change based on the zoom factor, which can be from 11 to 17?

The zoom parameter in the API is used to specify the "resolution" of the terrain object, and it has nothing to do with the position/rotation/scale of the object rendered by Three.js.

To illustrate this more clearly, I have updated the heightmap example so it can accept some of varying zoom values.

It would be helpful to compare these cases:

https://w3reality.github.io/three-geo/examples/heightmaps/index.html?zoom=11 https://w3reality.github.io/three-geo/examples/heightmaps/index.html?zoom=12 https://w3reality.github.io/three-geo/examples/heightmaps/index.html?zoom=13

where each rendered terrain is essentially the result of

const origin = [46.5763, 7.9904];
const radius = 5.0;
const terrain = await tgeo.getTerrainRgb(origin, radius, zoom)

if I want to position an object 1m above sea level

I would suggest following the projection example for that.

Specifically, how the z value of 3970m above sea level is mapped into WebGL space through multiplication by unitsPerMeter.

sonicviz commented 1 year ago

OK, I'll give that a shot. Thanks.

sonicviz commented 1 year ago

Looks like that works, thanks.

How accurate is that elevation calculation? Within some rough % range? I gather there will be some variance based on the quality of the dem and other factors?

"We’ve mapped these to 0.1 meter height increments, which gives us the vertical precision necessary for cartographic and 3D applications." ref: https://blog.mapbox.com/global-elevation-data-6689f1d0ba65

So up to 0.1m accuracy? (that mapbox post is from 2015, not sure if it's increased since then)