I'm currently developing a slippy map. It is perfectly working with tile coordinates that are used in openstreet maps i.e. the zoom/x/y format. I have used the formula from the OSM wiki to map longitude and latitude coordinates to their respective tiles. However I also want to implement a method to use a WMTS Tile Server. The problem is that the coordinate to tile mapping seems to be different here.
Is there a formula to get the WMTS tilenumber from a specific coordinate in longitude/latitude format with a given zoom level?
I am using geoserver to serve WMTS tiles.
I constructed an example to show my problem.
var lat = 50.5524; //Coordinates of Wetzlar, Germany
var lon = 8.5072;
var zoom = 12;
var tileLong = long2tile (lon, zoom + 1); //For some reason I need to increase the zoom level by 1 to get equivalent results in WMTS and OSM
var tileLat = lat2tile(lat, zoom +1);
tileLong -> 4289, this is correct as the OSM and the WMTS tile have the same value
tileLat -> 2758, correct in OSM, incorrect for WMTS as WMTS needs inversing the y-axis
var invertedTileLat = (1 << zoom) - tileLat - 1; //I have also tried zoom+1
invertedTileLat -> 1337, still incorrect for WMTS. The expected result is 897