add comments to decode_geo_hash lost by refactor to typescript (#43789) (#43818)

This commit is contained in:
Nathan Reese 2019-08-22 16:06:37 -06:00 committed by GitHub
parent 7d6e2498f5
commit bb9890b931
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 15 deletions

View file

@ -17,14 +17,27 @@
* under the License.
*/
import expect from '@kbn/expect';
import { geohashColumns } from '../decode_geo_hash';
import { geohashColumns, decodeGeoHash } from '../decode_geo_hash';
describe('decode_geo_hash', function () {
it('geohashColumns', function () {
expect(geohashColumns(1)).to.equal(8);
expect(geohashColumns(2)).to.equal(8 * 4);
expect(geohashColumns(3)).to.equal(8 * 4 * 8);
expect(geohashColumns(4)).to.equal(8 * 4 * 8 * 4);
test('geohashColumns', function () {
expect(geohashColumns(1)).toBe(8);
expect(geohashColumns(2)).toBe(8 * 4);
expect(geohashColumns(3)).toBe(8 * 4 * 8);
expect(geohashColumns(4)).toBe(8 * 4 * 8 * 4);
});
test('decodeGeoHash', function () {
expect(decodeGeoHash('drm3btev3e86')).toEqual({
latitude: [
41.119999922811985,
41.12000009045005,
41.12000000663102,
],
longitude: [
-71.34000029414892,
-71.3399999588728,
-71.34000012651086,
],
});
});

View file

@ -17,12 +17,17 @@
* under the License.
*/
interface Coordinates {
interface DecodedGeoHash {
latitude: number[];
longitude: number[];
}
export function decodeGeoHash(geohash: number[]): Coordinates {
/**
* Decodes geohash to object containing
* top-left and bottom-right corners of
* rectangle and center point.
*/
export function decodeGeoHash(geohash: string): DecodedGeoHash {
const BITS: number[] = [16, 8, 4, 2, 1];
const BASE32: string = '0123456789bcdefghjkmnpqrstuvwxyz';
let isEven: boolean = true;
@ -34,9 +39,8 @@ export function decodeGeoHash(geohash: number[]): Coordinates {
lon[1] = 180.0;
let latErr: number = 90.0;
let lonErr: number = 180.0;
for (const geohashEl of geohash) {
const c: string = geohashEl.toString();
const cd: number = BASE32.indexOf(c);
[...geohash].forEach((nextChar: string) => {
const cd: number = BASE32.indexOf(nextChar);
for (let j = 0; j < 5; j++) {
const mask: number = BITS[j];
if (isEven) {
@ -48,13 +52,13 @@ export function decodeGeoHash(geohash: number[]): Coordinates {
}
isEven = !isEven;
}
}
});
lat[2] = (lat[0] + lat[1]) / 2;
lon[2] = (lon[0] + lon[1]) / 2;
return {
latitude: lat,
longitude: lon,
} as Coordinates;
} as DecodedGeoHash;
}
function refineInterval(interval: number[], cd: number, mask: number) {