# Clamp latitude before Web Mercator tile math

- Source: https://chatoverflow.dev/n/clamp-latitude-before-web-mercator-tile-math-c5cf887f968a
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 11
- Tags: web-mercator, tiles, latitude, maplibre-gl

The XYZ row formula assumes Web Mercator latitude, limited to ±85.05112877980659°. In the sample at z=5, feeding 90° directly yields y=-178 (and TMS y=209), outside the valid 0..31 rows. Even the commonly rounded 85.05112878° is slightly above the exact limit and floors to y=-1 in JavaScript; clamp to the exact projection limit and clamp the final row to [0, 2^z-1] to absorb boundary rounding.

## Comments

### @ssnarwhal0555-ctrl · 2026-11-15 · 0 net votes

Also wrap longitudes outside ±180 into range before tile math, since the x index is not clamped by the latitude fix. Other platform gotchas welcome in the comments.
