Latitude and longitude to quantized mesh coordinates

I’ve been trying to display quantized mesh tiles into a 3d Java application. I managed to read the mesh data and display it correctly. Now I want to, given latitude and longitude, load tiles around this position but it appears that .terrain files do not follow a standard TMS disposition since I can’t seem to load them.

Is there some kind of pre-made formula for converting geo coordinates to terrain tiles x,y?

Hi @Sgambe33,
It’s possible I’m not understanding your question fully, but I the “tiling scheme” used by layer.json / quantized-mesh-1.0 is described near the top of this page:

It should match TMS. One subtlety is that not every tile will exist, though. There’s availability information in the layer.json as well as in individual tiles (every ten levels, I believe).

I eventually managed to get a formula capable of converting latitude and longitude to a correct terrain tile:

public static PointF worldToTerrainTilePos(double lat, double lon, int zoom) {
        // Calculate the number of tiles across the map, n, using 2^zoom
        double nY = Math.pow(2.0, zoom);
        double nX = Math.pow(2.0, zoom + 1);

        // Multiply x and y by n. Round results down to give tilex and tiley.
        PointF coord = new PointF();
        coord.x = (int) (nX * ((lon + 180) / 360));
        coord.y = (int) (nY * ((lat + 90) / 180));
        return coord;
    }

But I must say, terrain tiles do not follow the TMS that many other map providers do like Openstreetmap or Google. Now I’ll have to find a way to correctly overlay the tile image

terrain tiles do not follow the TMS that many other map providers do like Openstreetmap or Google

I think you’re referring to the Web Mercator projection, rather than the TMS tile layout. Web Mercator is not suitable for globe terrain because it’s only defined to about +/- 85 degrees latitude. There would be a 5 degree hole at the North and South poles. Cesium World Terrain instead uses a geographic projection, which is also a valid option for TMS but not the projection used by 2D map providers like OSM and Google.

Is there a simple way to then overlay the TMS imagery over the terrain that does not follow the same projection?

Cesium for Unity can overlay Web Mercator imagery on Geographic terrain, and vice-versa. It doesn’t currently support any projections other than those two, though.