// based on https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#ECMAScript_.28JavaScript.2FActionScript.2C_etc..29
// but the floor rounding removed. I did so with the assumption, that the rounded part corresponded to the tile coordinates
var Geohash = require("latlon-geohash@1.1.0");
function lon2tile(lon,zoom) {
return (lon+180)/360*Math.pow(2,zoom);
}
function lat2tile(lat,zoom) {
return (1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 *Math.pow(2,zoom);
}
function tile2long(x,z) {
return (x/Math.pow(2,z)*360-180);
}
function tile2lat(y,z) {
var n=Math.PI-2*Math.PI*y/Math.pow(2,z);
return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));
}
function getTileCoordinates(lat, lon, zoom, extend) {
let tx = lon2tile(lon, zoom),
ty = lat2tile(lat, zoom);
let result = {
x: extend * (tx % 1),
y: extend * (ty % 1),
tileX: Math.floor(tx),
tileY: Math.floor(ty)
};
return result;
}
const extend = 512;
const precision = 3;
const zoom = 2;
const lat = 63.252872;
const lon = 34.123535;
const initGeoHash = Geohash.encode(lat, lon, precision);
console.log(`initial geohash: ${initGeoHash}`);
const {tileX, tileY, x, y} = getTileCoordinates(lat, lon, zoom, extend);
const localTileX = Math.floor(x);
const localTileY = Math.floor(y); // Once we know that this has fallen into the northern geohash, then we can simple +1 to this and it will be correct
console.log(`local tile x: ${localTileX}`);
console.log(`local tile y: ${localTileY}`);
const reLon = tile2long(tileX + localTileX/extend, zoom);
const reLat = tile2lat(tileY + localTileY/extend, zoom);
console.log(`reLon: ${reLon}`);
console.log(`reLat: ${reLat}`);
console.log(`new geohash: ${Geohash.encode(reLat, reLon, precision)}`);
console.log(Geohash.neighbours(initGeoHash)); // from this we can see that it is the northern geohash so we should move localeTileY down (+1)
// in this example we round the coordinates in such a way that the geohash moves from ug0 => ug2