Created
May 7, 2025 09:33
-
-
Save stepankuzmin/6e3fccf4c7914972073d8e8e2900e1bb to your computer and use it in GitHub Desktop.
Get tile bounds from XYZ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function lngFromMercatorX(x: number): number { | |
return x * 360 - 180; | |
} | |
export function latFromMercatorY(y: number): number { | |
const y2 = 180 - y * 360; | |
return 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90; | |
} | |
function getBounds(tileID: CanonicalTileID): [number, number, number, number] { | |
const worldSize = Math.pow(2, tileID.z); | |
const x1 = tileID.x / worldSize; | |
const x2 = (tileID.x + 1) / worldSize; | |
const y1 = tileID.y / worldSize; | |
const y2 = (tileID.y + 1) / worldSize; | |
// left, bottom, right, top | |
return [ | |
lngFromMercatorX(x1), | |
latFromMercatorY(y2), | |
lngFromMercatorX(x2), | |
latFromMercatorY(y1) | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment