-
Notifications
You must be signed in to change notification settings - Fork 106
/
util.ts
386 lines (349 loc) · 9.35 KB
/
util.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
export function flip(arr) {
return [arr[1], arr[0]];
}
export function setPrecsion(km: number): number {
switch (true) {
case km <= 0.00477:
return 9;
case km <= 0.0382:
return 8;
case km <= 0.153:
return 7;
case km <= 1.22:
return 6;
case km <= 4.89:
return 5;
case km <= 39.1:
return 4;
case km <= 156:
return 3;
case km <= 1250:
return 2;
default:
return 1;
}
// 1 ≤ 5,000km × 5,000km
// 2 ≤ 1,250km × 625km
// 3 ≤ 156km × 156km
// 4 ≤ 39.1km × 19.5km
// 5 ≤ 4.89km × 4.89km
// 6 ≤ 1.22km × 0.61km
// 7 ≤ 153m × 153m
// 8 ≤ 38.2m × 19.1m
// 9 ≤ 4.77m × 4.77m
}
/////// NGEOHASH ////////
var BASE32_CODES = '0123456789bcdefghjkmnpqrstuvwxyz';
var BASE32_CODES_DICT = {};
for (var i = 0; i < BASE32_CODES.length; i++) {
BASE32_CODES_DICT[BASE32_CODES.charAt(i)] = i;
}
var ENCODE_AUTO = 'auto';
/**
* Significant Figure Hash Length
*
* This is a quick and dirty lookup to figure out how long our hash
* should be in order to guarantee a certain amount of trailing
* significant figures. This was calculated by determining the error:
* 45/2^(n-1) where n is the number of bits for a latitude or
* longitude. Key is # of desired sig figs, value is minimum length of
* the geohash.
* @type Array
*/
// Desired sig figs: 0 1 2 3 4 5 6 7 8 9 10
var SIGFIG_HASH_LENGTH = [0, 5, 7, 8, 11, 12, 13, 15, 16, 17, 18];
/**
* Encode
*
* Create a Geohash out of a latitude and longitude that is
* `numberOfChars` long.
*
* @param {Number|String} latitude
* @param {Number|String} longitude
* @param {Number} numberOfChars
* @returns {String}
*/
export const encode = function(latitude, longitude, numberOfChars) {
if (numberOfChars === ENCODE_AUTO) {
if (typeof latitude === 'number' || typeof longitude === 'number') {
throw new Error('string notation required for auto precision.');
}
var decSigFigsLat = latitude.split('.')[1].length;
var decSigFigsLong = longitude.split('.')[1].length;
var numberOfSigFigs = Math.max(decSigFigsLat, decSigFigsLong);
numberOfChars = SIGFIG_HASH_LENGTH[numberOfSigFigs];
} else if (numberOfChars === undefined) {
numberOfChars = 9;
}
var chars = [],
bits = 0,
bitsTotal = 0,
hash_value = 0,
maxLat = 90,
minLat = -90,
maxLon = 180,
minLon = -180,
mid;
while (chars.length < numberOfChars) {
if (bitsTotal % 2 === 0) {
mid = (maxLon + minLon) / 2;
if (longitude > mid) {
hash_value = (hash_value << 1) + 1;
minLon = mid;
} else {
hash_value = (hash_value << 1) + 0;
maxLon = mid;
}
} else {
mid = (maxLat + minLat) / 2;
if (latitude > mid) {
hash_value = (hash_value << 1) + 1;
minLat = mid;
} else {
hash_value = (hash_value << 1) + 0;
maxLat = mid;
}
}
bits++;
bitsTotal++;
if (bits === 5) {
var code = BASE32_CODES[hash_value];
chars.push(code);
bits = 0;
hash_value = 0;
}
}
return chars.join('');
};
/**
* Encode Integer
*
* Create a Geohash out of a latitude and longitude that is of 'bitDepth'.
*
* @param {Number} latitude
* @param {Number} longitude
* @param {Number} bitDepth
* @returns {Number}
*/
export const encode_int = function(latitude, longitude, bitDepth) {
bitDepth = bitDepth || 52;
var bitsTotal = 0,
maxLat = 90,
minLat = -90,
maxLon = 180,
minLon = -180,
mid,
combinedBits = 0;
while (bitsTotal < bitDepth) {
combinedBits *= 2;
if (bitsTotal % 2 === 0) {
mid = (maxLon + minLon) / 2;
if (longitude > mid) {
combinedBits += 1;
minLon = mid;
} else {
maxLon = mid;
}
} else {
mid = (maxLat + minLat) / 2;
if (latitude > mid) {
combinedBits += 1;
minLat = mid;
} else {
maxLat = mid;
}
}
bitsTotal++;
}
return combinedBits;
};
/**
* Decode Bounding Box
*
* Decode hashString into a bound box matches it. Data returned in a four-element array: [minlat, minlon, maxlat, maxlon]
* @param {String} hash_string
* @returns {Array}
*/
export const decode_bbox = function(hash_string) {
var isLon = true,
maxLat = 90,
minLat = -90,
maxLon = 180,
minLon = -180,
mid;
var hashValue = 0;
for (var i = 0, l = hash_string.length; i < l; i++) {
var code = hash_string[i].toLowerCase();
hashValue = BASE32_CODES_DICT[code];
for (var bits = 4; bits >= 0; bits--) {
var 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];
};
/**
* Decode Bounding Box Integer
*
* Decode hash number into a bound box matches it. Data returned in a four-element array: [minlat, minlon, maxlat, maxlon]
* @param {Number} hashInt
* @param {Number} bitDepth
* @returns {Array}
*/
export const decode_bbox_int = function(hashInt, bitDepth) {
bitDepth = bitDepth || 52;
var maxLat = 90,
minLat = -90,
maxLon = 180,
minLon = -180;
var latBit = 0,
lonBit = 0;
var step = bitDepth / 2;
for (var i = 0; i < step; i++) {
lonBit = get_bit(hashInt, (step - i) * 2 - 1);
latBit = get_bit(hashInt, (step - i) * 2 - 2);
if (latBit === 0) {
maxLat = (maxLat + minLat) / 2;
} else {
minLat = (maxLat + minLat) / 2;
}
if (lonBit === 0) {
maxLon = (maxLon + minLon) / 2;
} else {
minLon = (maxLon + minLon) / 2;
}
}
return [minLat, minLon, maxLat, maxLon];
};
function get_bit(bits, position) {
return (bits / Math.pow(2, position)) & 0x01;
}
/**
* Decode
*
* Decode a hash string into pair of latitude and longitude. A javascript object is returned with keys `latitude`,
* `longitude` and `error`.
* @param {String} hashString
* @returns {Object}
*/
export const decode = function(hashString) {
var bbox = decode_bbox(hashString);
var lat = (bbox[0] + bbox[2]) / 2;
var lon = (bbox[1] + bbox[3]) / 2;
var latErr = bbox[2] - lat;
var lonErr = bbox[3] - lon;
return {
latitude: lat,
longitude: lon,
error: { latitude: latErr, longitude: lonErr }
};
};
/**
* Decode Integer
*
* Decode a hash number into pair of latitude and longitude. A javascript object is returned with keys `latitude`,
* `longitude` and `error`.
* @param {Number} hash_int
* @param {Number} bitDepth
* @returns {Object}
*/
export const decode_int = function(hash_int, bitDepth) {
var bbox = decode_bbox_int(hash_int, bitDepth);
var lat = (bbox[0] + bbox[2]) / 2;
var lon = (bbox[1] + bbox[3]) / 2;
var latErr = bbox[2] - lat;
var lonErr = bbox[3] - lon;
return {
latitude: lat,
longitude: lon,
error: { latitude: latErr, longitude: lonErr }
};
};
/**
* Neighbor
*
* Find neighbor of a geohash string in certain direction. Direction is a two-element array, i.e. [1,0] means north, [-1,-1] means southwest.
* direction [lat, lon], i.e.
* [1,0] - north
* [1,1] - northeast
* ...
* @param {String} hashString
* @param {Array} Direction as a 2D normalized vector.
* @returns {String}
*/
export const neighbor = function(hashString, direction) {
var lonLat = decode(hashString);
var neighborLat = lonLat.latitude + direction[0] * lonLat.error.latitude * 2;
var neighborLon =
lonLat.longitude + direction[1] * lonLat.error.longitude * 2;
return encode(neighborLat, neighborLon, hashString.length);
};
/**
* Neighbor Integer
*
* Find neighbor of a geohash integer in certain direction. Direction is a two-element array, i.e. [1,0] means north, [-1,-1] means southwest.
* direction [lat, lon], i.e.
* [1,0] - north
* [1,1] - northeast
* ...
* @param {String} hash_string
* @returns {Array}
*/
export const neighbor_int = function(hash_int, direction, bitDepth) {
bitDepth = bitDepth || 52;
var lonlat = decode_int(hash_int, bitDepth);
var neighbor_lat = lonlat.latitude + direction[0] * lonlat.error.latitude * 2;
var neighbor_lon =
lonlat.longitude + direction[1] * lonlat.error.longitude * 2;
return encode_int(neighbor_lat, neighbor_lon, bitDepth);
};
/**
* Neighbors
*
* Returns all neighbors' hashstrings clockwise from north around to northwest
* 7 0 1
* 6 x 2
* 5 4 3
* @param {String} hash_string
* @returns {encoded neighborHashList|Array}
*/
export const neighbors = function(hash_string) {
var hashstringLength = hash_string.length;
var lonlat = decode(hash_string);
var lat = lonlat.latitude;
var lon = lonlat.longitude;
var latErr = lonlat.error.latitude * 2;
var lonErr = lonlat.error.longitude * 2;
var neighbor_lat, neighbor_lon;
var neighborHashList = [
encodeNeighbor(1, 0),
encodeNeighbor(1, 1),
encodeNeighbor(0, 1),
encodeNeighbor(-1, 1),
encodeNeighbor(-1, 0),
encodeNeighbor(-1, -1),
encodeNeighbor(0, -1),
encodeNeighbor(1, -1)
];
function encodeNeighbor(neighborLatDir, neighborLonDir) {
neighbor_lat = lat + neighborLatDir * latErr;
neighbor_lon = lon + neighborLonDir * lonErr;
return encode(neighbor_lat, neighbor_lon, hashstringLength);
}
return neighborHashList;
};