# XYZ Tile to LL conversion help needed

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

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)));

}

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:

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

And the web mercator one:

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

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

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

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