Atlaslog / Field Notes / Tiles
Field note · Tiles
TMS tiles render upside down because the Y axis counts from the bottom
Quick answer
Every tile loads and the mosaic is seamless, but the whole map is flipped vertically: at z18 Beijing lands in the southern hemisphere while east-west orientation is correct. The directory uses TMS numbering while the client requests XYZ.
Symptom
- At z18 Beijing is stitched into the southern hemisphere and the Taihang range runs inverted, while left-right orientation stays correct
- The Beijing tile is y=105000 in XYZ but y=157143 in the TMS directory, and the two values sum to 262143
- The same tile set displays correctly in QGIS when opened as a TMS layer
Checklist
- Open one tile of known location and back-compute its lat/lng to see whether north and south are swapped
- Read the top row's y value: close to 2^z indicates TMS numbering
- Check the service documentation; GeoServer GWC exposes both `/gwc/service/tms/1.0.0` and XYZ
- Request `y=0` and `y=2^z-1` manually and see which one is the south pole
- Apply `y_tms = 2^z - 1 - y_xyz` and reload
Fix
Use Leaflet's `{-y}` placeholder (`L.tileLayer('/tiles/{z}/{x}/{-y}.png')`), or in OpenLayers flip inside `tileUrlFunction`: `const n = Math.pow(2, z); return '/tiles/' + z + '/' + x + '/' + (n - 1 - y) + '.png'`.
Code
const z = 18, n = Math.pow(2, z); // 262144
const yXyz = 105000;
const yTms = n - 1 - yXyz; // 157143L.tileLayer('/tiles/{z}/{x}/{-y}.png', {
tileSize: 256,
minZoom: 3,
maxZoom: 18,
tms: false
}).addTo(map);
L.tileLayer('/tiles/{z}/{x}/{y}.png', { tileSize: 256 }).addTo(map);
Watch out
- Combining `tms: true` with `{-y}`, which flips twice and restores the inverted map
- Hardcoding one level's `2^z - 1` constant instead of computing the flip per zoom
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Atlaslog (2026). TMS tiles render upside down because the Y axis counts from the bottom. https://www.jingweizhi.com/en/cookbook/tms-y-flip.html (accessed 2026-11-03).