/
utils.ts
468 lines (419 loc) · 16.2 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import { GeoFireTypes } from './GeoFireTypes';
// Default geohash length
export const GEOHASH_PRECISION = 10;
// Characters used in location geohashes
export const BASE32 = '0123456789bcdefghjkmnpqrstuvwxyz';
// The meridional circumference of the earth in meters
export const EARTH_MERI_CIRCUMFERENCE = 40007860;
// Length of a degree latitude at the equator
export const METERS_PER_DEGREE_LATITUDE = 110574;
// Number of bits per geohash character
export const BITS_PER_CHAR = 5;
// Maximum length of a geohash in bits
export const MAXIMUM_BITS_PRECISION = 22 * BITS_PER_CHAR;
// Equatorial radius of the earth in meters
export const EARTH_EQ_RADIUS = 6378137.0;
// The following value assumes a polar radius of
// const EARTH_POL_RADIUS = 6356752.3;
// The formulate to calculate E2 is
// E2 == (EARTH_EQ_RADIUS^2-EARTH_POL_RADIUS^2)/(EARTH_EQ_RADIUS^2)
// The exact value is used here to avoid rounding errors
export const E2 = 0.00669447819799;
// Cutoff for rounding errors on double calculations
export const EPSILON = 1e-12;
function log2(x: number): number {
return Math.log(x) / Math.log(2);
}
/**
* Validates the inputted key and throws an error if it is invalid.
*
* @param key The key to be verified.
*/
export function validateKey(key: string): void {
let error: string;
if (typeof key !== 'string') {
error = 'key must be a string';
} else if (key.length === 0) {
error = 'key cannot be the empty string';
} else if (1 + GEOHASH_PRECISION + key.length > 755) {
// Firebase can only stored child paths up to 768 characters
// The child path for this key is at the least: 'i/<geohash>key'
error = 'key is too long to be stored in Firebase';
} else if (/[\[\].#$\/\u0000-\u001F\u007F]/.test(key)) {
// Firebase does not allow node keys to contain the following characters
error = 'key cannot contain any of the following characters: . # $ ] [ /';
}
if (typeof error !== 'undefined') {
throw new Error('Invalid GeoFire key \'' + key + '\': ' + error);
}
}
/**
* Validates the inputted location and throws an error if it is invalid.
*
* @param location The [latitude, longitude] pair to be verified.
*/
export function validateLocation(location: number[]): void {
let error: string;
if (!Array.isArray(location)) {
error = 'location must be an array';
} else if (location.length !== 2) {
error = 'expected array of length 2, got length ' + location.length;
} else {
const latitude = location[0];
const longitude = location[1];
if (typeof latitude !== 'number' || isNaN(latitude)) {
error = 'latitude must be a number';
} else if (latitude < -90 || latitude > 90) {
error = 'latitude must be within the range [-90, 90]';
} else if (typeof longitude !== 'number' || isNaN(longitude)) {
error = 'longitude must be a number';
} else if (longitude < -180 || longitude > 180) {
error = 'longitude must be within the range [-180, 180]';
}
}
if (typeof error !== 'undefined') {
throw new Error('Invalid GeoFire location \'' + location + '\': ' + error);
}
}
/**
* Validates the inputted geohash and throws an error if it is invalid.
*
* @param geohash The geohash to be validated.
*/
export function validateGeohash(geohash: string): void {
let error;
if (typeof geohash !== 'string') {
error = 'geohash must be a string';
} else if (geohash.length === 0) {
error = 'geohash cannot be the empty string';
} else {
for (const letter of geohash) {
if (BASE32.indexOf(letter) === -1) {
error = 'geohash cannot contain \'' + letter + '\'';
}
}
}
if (typeof error !== 'undefined') {
throw new Error('Invalid GeoFire geohash \'' + geohash + '\': ' + error);
}
}
/**
* Validates the inputted query criteria and throws an error if it is invalid.
*
* @param newQueryCriteria The criteria which specifies the query's center and/or radius.
* @param requireCenterAndRadius The criteria which center and radius required.
*/
export function validateCriteria(newQueryCriteria: GeoFireTypes.QueryCriteria, requireCenterAndRadius = false): void {
if (typeof newQueryCriteria !== 'object') {
throw new Error('query criteria must be an object');
} else if (typeof newQueryCriteria.center === 'undefined' && typeof newQueryCriteria.radius === 'undefined') {
throw new Error('radius and/or center must be specified');
} else if (requireCenterAndRadius && (typeof newQueryCriteria.center === 'undefined' || typeof newQueryCriteria.radius === 'undefined')) {
throw new Error('query criteria for a new query must contain both a center and a radius');
}
// Throw an error if there are any extraneous attributes
const keys: string[] = Object.keys(newQueryCriteria);
for (const key of keys) {
if (key !== 'center' && key !== 'radius') {
throw new Error('Unexpected attribute \'' + key + '\' found in query criteria');
}
}
// Validate the 'center' attribute
if (typeof newQueryCriteria.center !== 'undefined') {
validateLocation(newQueryCriteria.center);
}
// Validate the 'radius' attribute
if (typeof newQueryCriteria.radius !== 'undefined') {
if (typeof newQueryCriteria.radius !== 'number' || isNaN(newQueryCriteria.radius)) {
throw new Error('radius must be a number');
} else if (newQueryCriteria.radius < 0) {
throw new Error('radius must be greater than or equal to 0');
}
}
}
/**
* Converts degrees to radians.
*
* @param degrees The number of degrees to be converted to radians.
* @returns The number of radians equal to the inputted number of degrees.
*/
export function degreesToRadians(degrees: number): number {
if (typeof degrees !== 'number' || isNaN(degrees)) {
throw new Error('Error: degrees must be a number');
}
return (degrees * Math.PI / 180);
}
/**
* Generates a geohash of the specified precision/string length from the [latitude, longitude]
* pair, specified as an array.
*
* @param location The [latitude, longitude] pair to encode into a geohash.
* @param precision The length of the geohash to create. If no precision is specified, the
* global default is used.
* @returns The geohash of the inputted location.
*/
export function encodeGeohash(location: number[], precision: number = GEOHASH_PRECISION): string {
validateLocation(location);
if (typeof precision !== 'undefined') {
if (typeof precision !== 'number' || isNaN(precision)) {
throw new Error('precision must be a number');
} else if (precision <= 0) {
throw new Error('precision must be greater than 0');
} else if (precision > 22) {
throw new Error('precision cannot be greater than 22');
} else if (Math.round(precision) !== precision) {
throw new Error('precision must be an integer');
}
}
const latitudeRange = {
min: -90,
max: 90
};
const longitudeRange = {
min: -180,
max: 180
};
let hash = '';
let hashVal = 0;
let bits = 0;
let even: number | boolean = 1;
while (hash.length < precision) {
const val = even ? location[1] : location[0];
const range = even ? longitudeRange : latitudeRange;
const mid = (range.min + range.max) / 2;
if (val > mid) {
hashVal = (hashVal << 1) + 1;
range.min = mid;
} else {
hashVal = (hashVal << 1) + 0;
range.max = mid;
}
even = !even;
if (bits < 4) {
bits++;
} else {
bits = 0;
hash += BASE32[hashVal];
hashVal = 0;
}
}
return hash;
}
/**
* Calculates the number of degrees a given distance is at a given latitude.
*
* @param distance The distance to convert.
* @param latitude The latitude at which to calculate.
* @returns The number of degrees the distance corresponds to.
*/
export function metersToLongitudeDegrees(distance: number, latitude: number): number {
const radians = degreesToRadians(latitude);
const num = Math.cos(radians) * EARTH_EQ_RADIUS * Math.PI / 180;
const denom = 1 / Math.sqrt(1 - E2 * Math.sin(radians) * Math.sin(radians));
const deltaDeg = num * denom;
if (deltaDeg < EPSILON) {
return distance > 0 ? 360 : 0;
}
else {
return Math.min(360, distance / deltaDeg);
}
}
/**
* Calculates the bits necessary to reach a given resolution, in meters, for the longitude at a
* given latitude.
*
* @param resolution The desired resolution.
* @param latitude The latitude used in the conversion.
* @return The bits necessary to reach a given resolution, in meters.
*/
export function longitudeBitsForResolution(resolution: number, latitude: number): number {
const degs = metersToLongitudeDegrees(resolution, latitude);
return (Math.abs(degs) > 0.000001) ? Math.max(1, log2(360 / degs)) : 1;
}
/**
* Calculates the bits necessary to reach a given resolution, in meters, for the latitude.
*
* @param resolution The bits necessary to reach a given resolution, in meters.
* @returns Bits necessary to reach a given resolution, in meters, for the latitude.
*/
export function latitudeBitsForResolution(resolution: number): number {
return Math.min(log2(EARTH_MERI_CIRCUMFERENCE / 2 / resolution), MAXIMUM_BITS_PRECISION);
}
/**
* Wraps the longitude to [-180,180].
*
* @param longitude The longitude to wrap.
* @returns longitude The resulting longitude.
*/
export function wrapLongitude(longitude: number): number {
if (longitude <= 180 && longitude >= -180) {
return longitude;
}
const adjusted = longitude + 180;
if (adjusted > 0) {
return (adjusted % 360) - 180;
}
else {
return 180 - (-adjusted % 360);
}
}
/**
* Calculates the maximum number of bits of a geohash to get a bounding box that is larger than a
* given size at the given coordinate.
*
* @param coordinate The coordinate as a [latitude, longitude] pair.
* @param size The size of the bounding box.
* @returns The number of bits necessary for the geohash.
*/
export function boundingBoxBits(coordinate: number[], size: number): number {
const latDeltaDegrees = size / METERS_PER_DEGREE_LATITUDE;
const latitudeNorth = Math.min(90, coordinate[0] + latDeltaDegrees);
const latitudeSouth = Math.max(-90, coordinate[0] - latDeltaDegrees);
const bitsLat = Math.floor(latitudeBitsForResolution(size)) * 2;
const bitsLongNorth = Math.floor(longitudeBitsForResolution(size, latitudeNorth)) * 2 - 1;
const bitsLongSouth = Math.floor(longitudeBitsForResolution(size, latitudeSouth)) * 2 - 1;
return Math.min(bitsLat, bitsLongNorth, bitsLongSouth, MAXIMUM_BITS_PRECISION);
}
/**
* Calculates eight points on the bounding box and the center of a given circle. At least one
* geohash of these nine coordinates, truncated to a precision of at most radius, are guaranteed
* to be prefixes of any geohash that lies within the circle.
*
* @param center The center given as [latitude, longitude].
* @param radius The radius of the circle.
* @returns The eight bounding box points.
*/
export function boundingBoxCoordinates(center: number[], radius: number): number[][] {
const latDegrees = radius / METERS_PER_DEGREE_LATITUDE;
const latitudeNorth = Math.min(90, center[0] + latDegrees);
const latitudeSouth = Math.max(-90, center[0] - latDegrees);
const longDegsNorth = metersToLongitudeDegrees(radius, latitudeNorth);
const longDegsSouth = metersToLongitudeDegrees(radius, latitudeSouth);
const longDegs = Math.max(longDegsNorth, longDegsSouth);
return [
[center[0], center[1]],
[center[0], wrapLongitude(center[1] - longDegs)],
[center[0], wrapLongitude(center[1] + longDegs)],
[latitudeNorth, center[1]],
[latitudeNorth, wrapLongitude(center[1] - longDegs)],
[latitudeNorth, wrapLongitude(center[1] + longDegs)],
[latitudeSouth, center[1]],
[latitudeSouth, wrapLongitude(center[1] - longDegs)],
[latitudeSouth, wrapLongitude(center[1] + longDegs)]
];
}
/**
* Calculates the bounding box query for a geohash with x bits precision.
*
* @param geohash The geohash whose bounding box query to generate.
* @param bits The number of bits of precision.
* @returns A [start, end] pair of geohashes.
*/
export function geohashQuery(geohash: string, bits: number): string[] {
validateGeohash(geohash);
const precision = Math.ceil(bits / BITS_PER_CHAR);
if (geohash.length < precision) {
return [geohash, geohash + '~'];
}
geohash = geohash.substring(0, precision);
const base = geohash.substring(0, geohash.length - 1);
const lastValue = BASE32.indexOf(geohash.charAt(geohash.length - 1));
const significantBits = bits - (base.length * BITS_PER_CHAR);
const unusedBits = (BITS_PER_CHAR - significantBits);
// delete unused bits
const startValue = (lastValue >> unusedBits) << unusedBits;
const endValue = startValue + (1 << unusedBits);
if (endValue > 31) {
return [base + BASE32[startValue], base + '~'];
} else {
return [base + BASE32[startValue], base + BASE32[endValue]];
}
}
/**
* Calculates a set of queries to fully contain a given circle. A query is a [start, end] pair
* where any geohash is guaranteed to be lexiographically larger then start and smaller than end.
*
* @param center The center given as [latitude, longitude] pair.
* @param radius The radius of the circle.
* @return An array of geohashes containing a [start, end] pair.
*/
export function geohashQueries(center: number[], radius: number): string[][] {
validateLocation(center);
const queryBits = Math.max(1, boundingBoxBits(center, radius));
const geohashPrecision = Math.ceil(queryBits / BITS_PER_CHAR);
const coordinates = boundingBoxCoordinates(center, radius);
const queries = coordinates.map((coordinate) => {
return geohashQuery(encodeGeohash(coordinate, geohashPrecision), queryBits);
});
// remove duplicates
return queries.filter((query, index) => {
return !queries.some((other, otherIndex) => {
return index > otherIndex && query[0] === other[0] && query[1] === other[1];
});
});
}
/**
* Encodes a location and geohash as a GeoFire object.
*
* @param location The location as [latitude, longitude] pair.
* @param geohash The geohash of the location.
* @returns The location encoded as GeoFire object.
*/
export function encodeGeoFireObject(location: number[], geohash: string): GeoFireTypes.Document {
validateLocation(location);
validateGeohash(geohash);
return { '.priority': geohash, 'g': geohash, 'l': location };
}
/**
* Decodes the location given as GeoFire object. Returns null if decoding fails.
*
* @param geoFireObj The location encoded as GeoFire object.
* @returns The location as [latitude, longitude] pair or null if decoding fails.
*/
export function decodeGeoFireObject(geoFireObj: GeoFireTypes.Document): number[] {
if (geoFireObj && 'l' in geoFireObj && Array.isArray(geoFireObj.l) && geoFireObj.l.length === 2) {
return geoFireObj.l;
} else {
throw new Error('Unexpected location object encountered: ' + JSON.stringify(geoFireObj));
}
}
/**
* Returns the key of a Firebase snapshot across SDK versions.
*
* @param A Firebase snapshot.
* @returns The Firebase snapshot's key.
*/
export function geoFireGetKey(snapshot: GeoFireTypes.firebase.DataSnapshot): string {
let key;
if (typeof snapshot.key === 'string' || snapshot.key === null) {
key = snapshot.key;
} else if (typeof snapshot.key === 'function') {
// @ts-ignore
key = snapshot.key();
} else {
// @ts-ignore
key = snapshot.name();
}
return key;
}
/**
* Method which calculates the distance, in kilometers, between two locations,
* via the Haversine formula. Note that this is approximate due to the fact that the
* Earth's radius varies between 6356.752 km and 6378.137 km.
*
* @param location1 The [latitude, longitude] pair of the first location.
* @param location2 The [latitude, longitude] pair of the second location.
* @returns The distance, in kilometers, between the inputted locations.
*/
export function distance(location1: number[], location2: number[]): number {
validateLocation(location1);
validateLocation(location2);
const radius = 6371; // Earth's radius in kilometers
const latDelta = degreesToRadians(location2[0] - location1[0]);
const lonDelta = degreesToRadians(location2[1] - location1[1]);
const a = (Math.sin(latDelta / 2) * Math.sin(latDelta / 2)) +
(Math.cos(degreesToRadians(location1[0])) * Math.cos(degreesToRadians(location2[0])) *
Math.sin(lonDelta / 2) * Math.sin(lonDelta / 2));
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return radius * c;
}