Request .terrain from REST API

Hello!

I am trying to make a mesh visualizer in Unity by requesting the .terrain tiles from the REST API but I fail to understand how to request the one i need to show.

I have no issue getting the layer.json file, but I fail to understand how the “available” field works or if I have to use it at all with for the request.

The tree structure is supposedly in tms format (so sorted by zoom then x then y file) as stated in the layer.json file, and the question I guess would be : How to use the latitude/longitude to get a specific tile ?

I hope you can help me on this, here is what I had in the beginning :

public Vector2 WorldToTilePos(double lon, double lat, int zoom)
	{
		Vector2 p = new Vector2();
		p.x = (float)((lon + 180.0) / 360.0 * (1 << zoom));
		p.y = (float)((1.0 - Math.Log(Math.Tan(lat * Math.PI / 180.0) +
			1.0 / Math.Cos(lat * Math.PI / 180.0)) / Math.PI) / 2.0 * (1 << zoom));

		return p;
	}

Thanks

Hi @casparhenrik,

Thank you very much for reaching out to our community! @agallegos do you have any suggestions here?

-Sam

Well it seems the formula is correct since I get the good xyz from google as a tms. But when I try on the cesium viewer they are not the same tile at all. Isn’t it the good projection then ?

I understand that it has a 2x1 tile ratio so for the x multiplying by 2 each time seems understandable. But I still have no idea about the y, do I need to divide it at some point to still get a square?

And an other question, can I request the EPSG:3857 (spherical web mercator) projection for the .terrain instead? I would guess not and I’d rather stay with the more recent projection.
I am so close but I can’t seem to figure out how to get this Y

I finally managed to solve my issue with this formula I found I can’t remember where :

public static Vector2Int LatLonToTile(double lon, double lat, 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.
		Vector2Int coord = new Vector2Int();
		coord.x = (int)(nX * ((lon + 180) / 360));
		coord.y = (int)(nY * ((lat + 90) / 180));
		return coord;
}

Hi @casparhenrik,

Thank you for the follow-up. I am glad to see that your issue was resolved.

-Sam

Hello again,

Is it possible though to request the EPSG:3857 (spherical web mercator) projection for the cesium .terrain from the cesium REST api? Or is it tied to the asset?

How do you apply in your cesium viewer the bing maps spherical projection onto the elipsoidal projection of the ceisum terrain ?

Hi @casparhenrik,

These are not straightforward questions, but fortunately we supply open-source, permissively-licensed code in both JavaScript and C++ that addresses these problems, along with many more that you will run into in the process of bringing terrain and 3D Tiles to Unity.

In short, .terrain files are supplied in one projection, usually EPSG:4326. Imagery layers / raster overlays draped on this terrain may be in other projections, in which case we map the appropriate textures to the geometry at runtime.

In CesiumJS, the mapping happens here:

In cesium-native, the mapping happens here:

I’d highly recommend building your Unity implementation on cesium-native, rather than starting from scratch.

Kevin

Thank you for your response, I’ll have a look into Cesium native then

Hello again,

Sorry for the unending series of questions but I couldn’t find the answer i was seeking, so I’ll try a more direct one :

How to project a wsg84 spherical layer like bing maps on a wsg84 ellipsoid terrain from cesium with the TMS scheme?
The spherical projection bounding box goes from -85 85 in latitude whereas the ellipsoid goes from -90 90,
do you just add an offset to it when applied on the terrain?
I earlier thought that since the ellipsoid projection has 2 root tiles when the spherical has 1 only, I could just take the next zoom in the spherical projection (so 2x2) and put the 2 left ones on the 1st ellipsoid terrain tile and the 2 right ones on the 2nd terrain tile.

Right now I managed to have a good zoom system and getting the adjacent terrain and imagerie tiles, but the imagerie is displayed far to up north on the latitude (longitude is fine), so I have the norway imageri tiles displayed on the french terrain tile approximatly.

I think my formulas are fine, and the requested tiles too, but if you had any insight on this I’d be greateful

Thanks again

Here are the Alps and Pyrenees terrain but with norway imagerie