# Request .terrain from REST API

**URL:** <https://community.cesium.com/t/request-terrain-from-rest-api/18245>\
**Category:** Cesium ion\
**Created:** [April 20, 2022, 11:52am UTC](https://community.cesium.com/t/request-terrain-from-rest-api/18245 "2022-04-20T11:52:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![casparhenrik](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/casparhenrik/32/3476_2.png) [@casparhenrik](https://community.cesium.com/u/casparhenrik)\
**Post date:** [April 20, 2022, 11:52am UTC](https://community.cesium.com/t/request-terrain-from-rest-api/18245/1 "2022-04-20T11:52:09Z")

</div>

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 :

```auto
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

---

<div class="post-metadata">

**Author:** ![sam.rothstein](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/sam.rothstein/32/6584_2.png) [@sam.rothstein](https://community.cesium.com/u/sam.rothstein)\
**Post date:** [April 20, 2022, 2:45pm UTC](https://community.cesium.com/t/request-terrain-from-rest-api/18245/2 "2022-04-20T14:45:27Z")

</div>

Hi @casparhenrik,

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

-Sam

---

<div class="post-metadata">

**Author:** ![casparhenrik](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/casparhenrik/32/3476_2.png) [@casparhenrik](https://community.cesium.com/u/casparhenrik)\
**Post date:** [April 20, 2022, 10:28pm UTC](https://community.cesium.com/t/request-terrain-from-rest-api/18245/3 "2022-04-20T22:28:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casparhenrik](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/casparhenrik/32/3476_2.png) [@casparhenrik](https://community.cesium.com/u/casparhenrik)\
**Post date:** [April 21, 2022, 1:05pm UTC](https://community.cesium.com/t/request-terrain-from-rest-api/18245/4 "2022-04-21T13:05:25Z")

</div>

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

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![sam.rothstein](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/sam.rothstein/32/6584_2.png) [@sam.rothstein](https://community.cesium.com/u/sam.rothstein)\
**Post date:** [April 21, 2022, 2:25pm UTC](https://community.cesium.com/t/request-terrain-from-rest-api/18245/5 "2022-04-21T14:25:46Z")

</div>

Hi @casparhenrik,

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

-Sam

---

<div class="post-metadata">

**Author:** ![casparhenrik](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/casparhenrik/32/3476_2.png) [@casparhenrik](https://community.cesium.com/u/casparhenrik)\
**Post date:** [April 21, 2022, 11:03pm UTC](https://community.cesium.com/t/request-terrain-from-rest-api/18245/6 "2022-04-21T23:03:29Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Kevin\_Ring](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/kevin_ring/32/2003_2.png) [@Kevin\_Ring](https://community.cesium.com/u/Kevin_Ring)\
**Post date:** [April 26, 2022, 11:44pm UTC](https://community.cesium.com/t/request-terrain-from-rest-api/18245/8 "2022-04-26T23:44:08Z")

</div>

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:

> <https://github.com/CesiumGS/cesium/blob/main/Source/Scene/ImageryLayer.js#L518>

In cesium-native, the mapping happens here:

> <https://github.com/CesiumGS/cesium-native/blob/main/Cesium3DTilesSelection/src/QuadtreeRasterOverlayTileProvider.cpp#L443>

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

Kevin

---

<div class="post-metadata">

**Author:** ![casparhenrik](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/casparhenrik/32/3476_2.png) [@casparhenrik](https://community.cesium.com/u/casparhenrik)\
**Post date:** [April 27, 2022, 1:21pm UTC](https://community.cesium.com/t/request-terrain-from-rest-api/18245/9 "2022-04-27T13:21:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casparhenrik](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/casparhenrik/32/3476_2.png) [@casparhenrik](https://community.cesium.com/u/casparhenrik)\
**Post date:** [April 29, 2022, 4:46pm UTC](https://community.cesium.com/t/request-terrain-from-rest-api/18245/10 "2022-04-29T16:46:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casparhenrik](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/casparhenrik/32/3476_2.png) [@casparhenrik](https://community.cesium.com/u/casparhenrik)\
**Post date:** [April 29, 2022, 4:49pm UTC](https://community.cesium.com/t/request-terrain-from-rest-api/18245/11 "2022-04-29T16:49:33Z")

</div>

![Capture.PNG](https://global.discourse-cdn.com/cesium/original/2X/f/fae54498279ccf2f6cfd977b24084b7d7703b4a1.jpeg)

Here are the Alps and Pyrenees terrain but with norway imagerie

---

<div class="post-metadata">

**Author:** ![daktor](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/daktor/32/3476_2.png) [@daktor](https://community.cesium.com/u/daktor)\
**Post date:** [October 21, 2024, 5:50pm UTC](https://community.cesium.com/t/request-terrain-from-rest-api/18245/12 "2024-10-21T17:50:17Z")

</div>

Sorry to resurrect an old thread, but my questions are on the same topic.

The equation here does seem to work with REST requests, but does not give the same values as TMS. Is this equation the correct equation to find a tile for terrain REST requests? Are terrain datasets using TMS tiling?
