@math.gl/geohash
Version:
Math for the GeoHash DGGS (Discrete Global Grid System)
64 lines (55 loc) • 1.54 kB
JavaScript
const BASE32_CODES = '0123456789bcdefghjkmnpqrstuvwxyz';
const BASE32_CODES_DICT = {};
for (let i = 0; i < BASE32_CODES.length; i++) {
BASE32_CODES_DICT[BASE32_CODES.charAt(i)] = i;
}
const MIN_LAT = -90;
const MAX_LAT = 90;
const MIN_LON = -180;
const MAX_LON = 180;
export function getGeohashLngLat(geohash) {
const [s, w, n, e] = getGeohashBounds(geohash);
return [(e + w) / 2, (n + s) / 2];
}
export function getGeohashBoundary(geohash) {
const [s, w, n, e] = getGeohashBounds(geohash);
return [[e, n], [e, s], [w, s], [w, n], [e, n]];
}
export function getGeohashBoundaryFlat(geohash) {
const [s, w, n, e] = getGeohashBounds(geohash);
return [e, n, e, s, w, s, w, n, e, n];
}
export function getGeohashBounds(geohash) {
let isLon = true;
let maxLat = MAX_LAT;
let minLat = MIN_LAT;
let maxLon = MAX_LON;
let minLon = MIN_LON;
let mid;
let hashValue = 0;
for (let i = 0, l = geohash.length; i < l; i++) {
const code = geohash[i].toLowerCase();
hashValue = BASE32_CODES_DICT[code];
for (let bits = 4; bits >= 0; bits--) {
const bit = hashValue >> bits & 1;
if (isLon) {
mid = (maxLon + minLon) / 2;
if (bit === 1) {
minLon = mid;
} else {
maxLon = mid;
}
} else {
mid = (maxLat + minLat) / 2;
if (bit === 1) {
minLat = mid;
} else {
maxLat = mid;
}
}
isLon = !isLon;
}
}
return [minLat, minLon, maxLat, maxLon];
}
//# sourceMappingURL=geohash.js.map