UNPKG

s2-tools

Version:

A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.

62 lines 1.7 kB
import { S2Point, fromS2Point as fromS2, fromST } from '../geometry'; /** * Convert a BigInt to an Uint64Cell representation * @param id - the 64-bit cell id in BigInt or number form * @returns - an Uint64Cell with the appropriate id and functions */ export function toCell(id) { if (typeof id === 'object') return id; const bigint = BigInt(id); return { low: Number(bigint & 0xffffffffn), high: Number((bigint >> 32n) & 0xffffffffn), }; } /** * Convert a lon/lat to an Uint64Cell representation * @param lon - longitude * @param lat - latitude * @returns - an Uint64Cell with the appropriate id and functions */ export function fromLonLat(lon, lat) { const { toST, fromLonLat } = S2Point; const [face, s, t] = toST(fromLonLat(lon, lat)); return fromFaceST(face, s, t); } /** * Convert a face/s/t to an Uint64Cell representation * @param face - face on the sphere * @param s - x position * @param t - y position * @returns - an Uint64Cell with the appropriate id and functions */ export function fromFaceST(face, s, t) { const id = fromST(face, s, t); return toCell(id); } /** * @param point - a vector point on the sphere * @returns - an Uint64Cell with the appropriate id and functions */ export function fromS2Point(point) { const id = fromS2(point); return toCell(id); } /** * @param a - the first cell * @param b - the second cell * @returns -1 | 0 | 1 */ export function compare(a, b) { if (a.high < b.high) return -1; if (a.high > b.high) return 1; if (a.low < b.low) return -1; if (a.low > b.low) return 1; return 0; } //# sourceMappingURL=uint64.js.map