-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeohash.js
398 lines (344 loc) · 12.5 KB
/
geohash.js
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
/*
* Copyright ©️ 2019 GaltProject Society Construction and Terraforming Company
* (Founded by [Nikolai Popeka](https://github.com/npopeka)
*
* Copyright ©️ 2019 Galt•Core Blockchain Company
* (Founded by [Nikolai Popeka](https://github.com/npopeka) by
* [Basic Agreement](ipfs/QmaCiXUmSrP16Gz8Jdzq6AJESY1EAANmmwha15uR3c1bsS)).
*/
/**
* Pure geohash operations based on symbols relations in geohash concept, without using any libs.
*/
module.exports = class Geohash {
static get neighbourData() {
return {
n: ['p0r21436x8zb9dcf5h7kjnmqesgutwvy', 'bc01fg45238967deuvhjyznpkmstqrwx'],
s: ['14365h7k9dcfesgujnmqp0r2twvyx8zb', '238967debc01fg45kmstqrwxuvhjyznp'],
e: ['bc01fg45238967deuvhjyznpkmstqrwx', 'p0r21436x8zb9dcf5h7kjnmqesgutwvy'],
w: ['238967debc01fg45kmstqrwxuvhjyznp', '14365h7k9dcfesgujnmqp0r2twvyx8zb'],
};
}
static get borderData() {
return {
n: ['prxz', 'bcfguvyz'],
s: ['028b', '0145hjnp'],
e: ['bcfguvyz', 'prxz'],
w: ['0145hjnp', '028b'],
};
}
static get base32() {
return '0123456789bcdefghjkmnpqrstuvwxyz';
}
/**
* Get neighbour geohash by given direction.
*
* @param geohash - Cell to which adjacent cell is required.
* @param direction - Direction from geohash (n/s/e/w).
* @returns {string} Geohash of neighbour.
*/
static neighbourByDirection(geohash, direction) {
const lastCh = geohash.slice(-1); // last character of hash
let parent = Geohash.getParent(geohash);
const type = geohash.length % 2;
// check for edge-cases which don't share common prefix
if (this.borderData[direction][type].indexOf(lastCh) !== -1 && parent !== '') {
parent = Geohash.neighbourByDirection(parent, direction);
}
// append letter for direction to parent
return parent + Geohash.base32.charAt(this.neighbourData[direction][type].indexOf(lastCh));
}
/**
* Returns all 8 neighbours geohashes by specified geohash.
*
* @param {string} geohash - Geohash neighbours are required of.
* @returns [n,ne,e,se,s,sw,w,nw]
* @throws Invalid geohash.
*/
static allNeighbours(geohash) {
return [
Geohash.neighbourByDirection(geohash, 'n'),
Geohash.neighbourByDirection(Geohash.neighbourByDirection(geohash, 'n'), 'e'),
Geohash.neighbourByDirection(geohash, 'e'),
Geohash.neighbourByDirection(Geohash.neighbourByDirection(geohash, 's'), 'e'),
Geohash.neighbourByDirection(geohash, 's'),
Geohash.neighbourByDirection(Geohash.neighbourByDirection(geohash, 's'), 'w'),
Geohash.neighbourByDirection(geohash, 'w'),
Geohash.neighbourByDirection(Geohash.neighbourByDirection(geohash, 'n'), 'w'),
];
}
/**
* Get direction between geohash and neighbour
*
* @param geohash
* @param neighbour
* @returns {*}
*/
static directionOfNeighbour(geohash, neighbour) {
if (geohash.length > neighbour.length) {
geohash = geohash.slice(0, neighbour.length);
} else if (neighbour.length > geohash.length) {
neighbour = neighbour.slice(0, geohash.length);
}
const neighbours = Geohash.allNeighbours(geohash);
if (neighbours.indexOf(neighbour) === -1) {
return null;
}
return ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw'][neighbours.indexOf(neighbour)];
}
static isNeighbourOnSameLevel(geohash, neighbour, direction) {
if (direction.length === 1) {
return neighbour === Geohash.neighbourByDirection(geohash, direction);
} else {
return neighbour === Geohash.neighbourByDirection(Geohash.neighbourByDirection(geohash, direction[0]), direction[1]);
}
}
/**
* Get neighbour on diff precision levels, for example: "u40" and "u410" with direction "e".
* WARNING: Supports only 1 precision diff in levels.
*
* @param geohash
* @param neighbour
* @param direction
* @returns {boolean}
*/
static isNeighbourOnDiffLevel(geohash, neighbour, direction) {
const lengthDiff = geohash.length - neighbour.length;
if (Math.abs(lengthDiff) > 1) {
return false;
}
let highLevelGeohash;
let lowLevelGeohash;
if (lengthDiff < 0) {
highLevelGeohash = geohash;
lowLevelGeohash = neighbour;
} else {
lowLevelGeohash = geohash;
highLevelGeohash = neighbour;
}
const lowLevelGeohashParent = Geohash.getParent(lowLevelGeohash);
let isParentsNeighbours;
if (lengthDiff < 0) {
isParentsNeighbours = Geohash.isNeighbourOnSameLevel(highLevelGeohash, lowLevelGeohashParent, direction);
} else {
isParentsNeighbours = Geohash.isNeighbourOnSameLevel(lowLevelGeohashParent, highLevelGeohash, direction);
}
if (!isParentsNeighbours) {
return false;
}
const lowLevelGeohashBorder = Geohash.getGeohashBorder(lowLevelGeohash);
if (!lowLevelGeohashBorder) {
return false;
}
const lowLevelGeohashDirection = Geohash.getGeohashBorder(lowLevelGeohash);
if (lengthDiff > 0) {
direction = Geohash.oppositeDirection(direction);
}
if (direction === 'e' && Geohash.isBorderWest(lowLevelGeohashDirection)) {
return true;
}
if (direction === 'w' && Geohash.isBorderEast(lowLevelGeohashDirection)) {
return true;
}
if (direction === 'n' && Geohash.isBorderSouth(lowLevelGeohashDirection)) {
return true;
}
if (direction === 's' && Geohash.isBorderNorth(lowLevelGeohashDirection)) {
return true;
}
return false;
}
static getGeohashBorder(geohash) {
const type = geohash.length % 2;
const lastChar = geohash.slice(geohash.length - 1);
const isNBorder = Geohash.isBorder('n', type, lastChar);
const isSBorder = Geohash.isBorder('s', type, lastChar);
const isEBorder = Geohash.isBorder('e', type, lastChar);
const isWBorder = Geohash.isBorder('w', type, lastChar);
if (isSBorder && isWBorder) {
return 'sw';
}
if (isNBorder && isWBorder) {
return 'nw';
}
if (isSBorder && isEBorder) {
return 'se';
}
if (isNBorder && isEBorder) {
return 'ne';
}
if (isNBorder) {
return 'n';
}
if (isSBorder) {
return 's';
}
if (isEBorder) {
return 'e';
}
if (isWBorder) {
return 'w';
}
return null;
}
static isBorder(direction, type, lastChar) {
return this.borderData[direction][type].indexOf(lastChar) !== -1;
}
static oppositeDirection(direction) {
if (direction === 'n') {
return 's';
}
if (direction === 's') {
return 'n';
}
if (direction === 'w') {
return 'e';
}
if (direction === 'e') {
return 'w';
}
return null;
}
static isBorderEast(border) {
return border === 'e' || border.slice(-1) === 'e';
}
static isBorderWest(border) {
return border === 'w' || border.slice(-1) === 'w';
}
static isBorderNorth(border) {
return border === 'n' || border.slice(0, -1) === 'n';
}
static isBorderSouth(border) {
return border === 's' || border.slice(0, -1) === 's';
}
static getParent(geohash) {
return geohash.slice(0, -1); // hash without last character
}
/**
* Get full list of children geohashes by parent geohash
*
* @param parentGeohash
* @returns {Array}
*/
static getChildren(parentGeohash) {
let indexOfChildSymbol = 0;
const childrenGeohashes = [];
while (indexOfChildSymbol < 32) {
childrenGeohashes.push(parentGeohash + Geohash.base32[indexOfChildSymbol]);
indexOfChildSymbol++;
}
return childrenGeohashes;
}
/**
* Get children list by one-side direction.
* For example: "gfp" have ["gfp0", "gfp1", "gfp4", "gfp5", "gfph", "gfpj", "gfpn", "gfpp"] children on "w" direction
* @param geohash
* @param direction
* @returns {*}
*/
static getChildrenByDirection(geohash, direction) {
if (direction.length !== 1) {
return null;
}
const type = (geohash.length + 1) % 2;
const firstDirectionSymbols = Geohash.borderData[direction][type].split('');
return firstDirectionSymbols.map((symbol) => {
return geohash + symbol;
});
}
/**
* Get child by two-sides direction. For example: "gfp" have "gfp0" child on "sw" direction
* @param geohash
* @param direction
* @param precision
* @returns {*}
*/
static getChildByDirection(geohash, direction, precision = null) {
if (direction.length !== 2 || !geohash) {
return null;
}
if(precision && (geohash.length >= precision || precision > 12)) {
return null;
}
const firstDirection = direction[0];
const secondDirection = direction[1];
const type = (geohash.length + 1) % 2;
const firstDirectionSymbols = Geohash.borderData[firstDirection][type].split('');
const secondDirectionSymbols = Geohash.borderData[secondDirection][type].split('');
let resultSymbol;
firstDirectionSymbols.some((fdSymbol) => {
const intersection = secondDirectionSymbols.indexOf(fdSymbol) !== -1;
if(intersection) {
resultSymbol = fdSymbol;
}
return intersection;
});
if(!resultSymbol) {
return null;
}
const resultGeohash = geohash + resultSymbol;
if(precision && resultGeohash.length < precision) {
return Geohash.getChildByDirection(resultGeohash, direction, precision)
} else {
return resultGeohash;
}
}
/**
* Get list of possible parents for merge from children geohashes.
* If all 32 children of parent exists - parent will be in list.
*
* @param geohashesList
* @returns {Array}
*/
static getFullParentsFromGeohashList(geohashesList) {
const parentsForMerge = [];
const parentsToChildren = {};
geohashesList.forEach((geohash, index) => {
const parent = Geohash.getParent(geohash);
if (!parentsToChildren[parent]) {
parentsToChildren[parent] = [];
}
parentsToChildren[parent].push(geohash);
if (parentsToChildren[parent].length === 32) {
parentsForMerge.push(parent);
}
});
return parentsForMerge;
}
/**
* Get first neighbour with direction from possibleNeighbours list
*
* @param geohash
* @param possibleNeighbours
* @param directionRequired
* @returns {{geohash: *, direction: *}}
*/
static getNeighbourWithDirection(geohash, possibleNeighbours, directionRequired) {
let resultGeohash;
let resultDirection;
possibleNeighbours
.some(possibleNeighbour => {
if (geohash === possibleNeighbours) {
return false;
}
const direction = Geohash.directionOfNeighbour(geohash, possibleNeighbour);
if (!direction || (directionRequired && direction !== directionRequired)) {
return false;
}
let isNeighbour;
if (geohash.length === possibleNeighbour.length) {
isNeighbour = Geohash.isNeighbourOnSameLevel(geohash, possibleNeighbour, direction);
} else {
isNeighbour = Geohash.isNeighbourOnDiffLevel(geohash, possibleNeighbour, direction);
}
if (isNeighbour) {
resultGeohash = possibleNeighbour;
resultDirection = direction;
}
return isNeighbour;
});
return {
geohash: resultGeohash,
direction: resultDirection
};
}
};