# XYZ Tile to LL conversion help needed

**URL:** <https://community.cesium.com/t/xyz-tile-to-ll-conversion-help-needed/8073>\
**Category:** CesiumJS\
**Created:** [March 7, 2019, 1:56am UTC](https://community.cesium.com/t/xyz-tile-to-ll-conversion-help-needed/8073 "2019-03-07T01:56:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alan\_Rose](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/alan_rose/32/2293_2.png) [@Alan\_Rose](https://community.cesium.com/u/Alan_Rose)\
**Post date:** [March 7, 2019, 1:56am UTC](https://community.cesium.com/t/xyz-tile-to-ll-conversion-help-needed/8073/1 "2019-03-07T01:56:25Z")

</div>

**1. A concise explanation of the problem you’re experiencing.**

**2. A minimal code example. If you’ve found a bug, this helps us reproduce and repair it.**

**3. Context. Why do you need to do this? We might know a better way to accomplish your goal.**

**4. The Cesium version you’re using, your operating system and browser.**

I want to convert an XYZ tile to a bounding box of Lat Long positions and found a website with examples

[https://wiki.openstreetmap.org/wiki/Slippy\_map\_tilenames#Lon..2Flat.\_to\_tile\_numbers\_2](https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Lon..2Flat._to_tile_numbers_2)

and created the code below from this.

This code correctly outputs (-180,-85.05) - (0,85.05) for tile Z0, X,0, Y0.

However when I looked at the South Island of New Zealand in debug mode the tile coordinate says L5X62Y23 or (X62,Y23,Z5)

Approx North -39, South -44 and West 168, East 174

If I then run that XYZ coordinate through the code below it returns the correct longitude but latitude is approx 20 degrees south (North -61, South -66)

What have I done wrong?

Default cesiumContainer used

public static BoundingBox GetLatLonBoundsForCesiumTile(int zoom, int x, int y)

{

BoundingBox bb = new BoundingBox();

bb.North = tile2lat(y, zoom);

bb.South = tile2lat(y + 1, zoom);

bb.West = tile2lon(x, zoom + 1);

bb.East = tile2lon(x + 1, zoom + 1);

return bb;

}

public static double tile2lon(int x, int zoom)

{

return x / Math.Pow(2, zoom) \* 360.0 - 180;

}

public static double tile2lat(int y, int zoom)

{

double n = Math.PI - (2.0 \* Math.PI \* y) / Math.Pow(2.0, zoom);

return (180.0 / Math.PI) \* (Math.Atan(Math.Sinh(n)));

}

---

<div class="post-metadata">

**Author:** ![omar](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/omar/32/1999_2.png) [@omar](https://community.cesium.com/u/omar)\
**Post date:** [March 8, 2019, 3:14pm UTC](https://community.cesium.com/t/xyz-tile-to-ll-conversion-help-needed/8073/2 "2019-03-08T15:14:59Z")

</div>

My best guess without knowing more information is there might be some projection you’re not account for. If so, I would expect the offset from the correct answer to be different depending on where you take the measurement, how close to the poles etc.

In the Cesium source code, you can see here is where a tile is given x, y and level, and it’s converted to a rectangle:

[https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Scene/QuadtreeTile.js#L55](https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Scene/QuadtreeTile.js#L55)

This conversion depends on what tiling scheme is used. This is the geographic tiling scheme:

[https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Core/GeographicTilingScheme.js#L168](https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Core/GeographicTilingScheme.js#L168)

And the web mercator one:

[https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Core/WebMercatorTilingScheme.js#L194](https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Core/WebMercatorTilingScheme.js#L194)

---

<div class="post-metadata">

**Author:** ![Alan\_Rose](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/alan_rose/32/2293_2.png) [@Alan\_Rose](https://community.cesium.com/u/Alan_Rose)\
**Post date:** [March 9, 2019, 3:12am UTC](https://community.cesium.com/t/xyz-tile-to-ll-conversion-help-needed/8073/3 "2019-03-09T03:12:14Z")

</div>

Thanks Omar. I think I have made a rookie mistake and used a Geographic calculation instead of Web Mercator. Thanks for the links.

---

<div class="post-metadata">

**Author:** ![Alan\_Rose](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/alan_rose/32/2293_2.png) [@Alan\_Rose](https://community.cesium.com/u/Alan_Rose)\
**Post date:** [March 11, 2019, 2:47am UTC](https://community.cesium.com/t/xyz-tile-to-ll-conversion-help-needed/8073/4 "2019-03-11T02:47:32Z")

</div>

Still having trouble understanding whats going on here.  
Am I right in thinking for Imagery tiles the default TilingScheme is Web Mercator and for Terrain it is Geographic

Debugging in Chrome it appears to suggest WebMercator has 2x2 tiles at zero level (not what I see on screen however), and for Geographic tiling it is 1x2 tiles at level 0

Is there a piece of code I can use in sandcastle that will correctly show me the correctly the lon lat rectangle coordinates for a given terrain tile X/Y/Z entered

---

<div class="post-metadata">

**Author:** ![Alan\_Rose](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/alan_rose/32/2293_2.png) [@Alan\_Rose](https://community.cesium.com/u/Alan_Rose)\
**Post date:** [March 11, 2019, 6:47am UTC](https://community.cesium.com/t/xyz-tile-to-ll-conversion-help-needed/8073/5 "2019-03-11T06:47:35Z")

</div>

Ignore code request. I found my code error and have since followed the cesium code example and now have it working
