vgridjs
Version:
Vgrid DGGS JS
117 lines (115 loc) • 3.25 kB
JavaScript
;
// dggs/mercantile.ts
var MAX_ZOOM = 32;
var MIN_ZOOM = 0;
function tile(lng, lat, zoom) {
if (zoom < MIN_ZOOM || zoom > MAX_ZOOM) {
throw new Error(`Invalid zoom level: ${zoom}`);
}
if (lng < -180 || lng > 180) {
throw new Error(`Invalid longitude: ${lng}`);
}
if (lat < -90 || lat > 90) {
throw new Error(`Invalid latitude: ${lat}`);
}
const n = Math.pow(2, zoom);
const xtile = Math.floor((lng + 180) / 360 * n);
const ytile = Math.floor((1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * n);
return { x: xtile, y: ytile, z: zoom };
}
function bounds(x, y, z) {
if (z < MIN_ZOOM || z > MAX_ZOOM) {
throw new Error(`Invalid zoom level: ${z}`);
}
const n = Math.pow(2, z);
const west = x / n * 360 - 180;
const east = (x + 1) / n * 360 - 180;
const north = Math.atan(Math.sinh(Math.PI * (1 - 2 * y / n))) * 180 / Math.PI;
const south = Math.atan(Math.sinh(Math.PI * (1 - 2 * (y + 1) / n))) * 180 / Math.PI;
return { west, south, east, north };
}
function quadkey(tile2) {
let quadkey2 = "";
for (let i = tile2.z; i > 0; i--) {
let digit = 0;
const mask = 1 << i - 1;
if ((tile2.x & mask) !== 0) digit += 1;
if ((tile2.y & mask) !== 0) digit += 2;
quadkey2 += digit.toString();
}
return quadkey2;
}
function quadkeyToTile(quadkey2) {
let x = 0;
let y = 0;
const z = quadkey2.length;
for (let i = z; i > 0; i--) {
const mask = 1 << i - 1;
const digit = parseInt(quadkey2[z - i]);
if (digit & 1) x |= mask;
if (digit & 2) y |= mask;
}
return { x, y, z };
}
function parent(tile2) {
if (tile2.z === 0) {
throw new Error("Cannot get parent of zoom level 0");
}
return {
x: Math.floor(tile2.x / 2),
y: Math.floor(tile2.y / 2),
z: tile2.z - 1
};
}
function children(tile2) {
const z = tile2.z + 1;
const x = tile2.x * 2;
const y = tile2.y * 2;
return [
{ x, y, z },
{ x: x + 1, y, z },
{ x, y: y + 1, z },
{ x: x + 1, y: y + 1, z }
];
}
function siblings(tile2) {
const parentTile = parent(tile2);
return children(parentTile).filter(
(t) => t.x !== tile2.x || t.y !== tile2.y || t.z !== tile2.z
);
}
function xyTile(x, y, zoom) {
const n = Math.pow(2, zoom);
const xtile = Math.floor((x + 1) / 2 * n);
const ytile = Math.floor((1 - (y + 1) / 2) * n);
return { x: xtile, y: ytile, z: zoom };
}
function tileToXY(tile2) {
const n = Math.pow(2, tile2.z);
const x = tile2.x / n * 2 - 1;
const y = 1 - tile2.y / n * 2;
return [x, y];
}
function lngLatToXY(lng, lat) {
const x = lng / 180;
const y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / Math.PI;
return [x, y];
}
function xyToLngLat(x, y) {
const lng = x * 180;
const lat = 90 - 360 * Math.atan(Math.exp(-y * Math.PI)) / Math.PI;
return [lng, lat];
}
exports.bounds = bounds;
exports.children = children;
exports.lngLatToXY = lngLatToXY;
exports.parent = parent;
exports.quadkey = quadkey;
exports.quadkeyToTile = quadkeyToTile;
exports.siblings = siblings;
exports.tile = tile;
exports.tileToXY = tileToXY;
exports.xyTile = xyTile;
exports.xyToLngLat = xyToLngLat;
//# sourceMappingURL=mercantile.cjs.map
//# sourceMappingURL=mercantile.cjs.map