-
Notifications
You must be signed in to change notification settings - Fork 2
/
geohashExtra.js
180 lines (147 loc) · 5.34 KB
/
geohashExtra.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
/*
* 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)).
*/
/**
* Extra geohash operations, which requires some dependencies like ngeohash and lodash
*/
const ngeohash = require('ngeohash');
const clone = require('lodash/clone');
const concat = require('lodash/concat');
const uniq = require('lodash/uniq');
const includes = require('lodash/includes');
const Geohash = require('./geohash');
const Utm = require('./utm');
module.exports = class GeohashExtra {
static decodeToLatLon(geohash, arrayMode = false) {
const {latitude, longitude} = ngeohash.decode(geohash);
if (arrayMode) {
return [latitude, longitude];
} else {
return {lat: latitude, lon: longitude};
}
}
static encodeFromLatLng(lat, lng, precision) {
if (!precision) {
precision = 'auto';
}
return ngeohash.encode(lat, lng, precision);
}
static decodeToUtm(geohash) {
const latLon = GeohashExtra.decodeToLatLon(geohash, true);
return Utm.fromLatLon(latLon[0], latLon[1]);
}
static encodeFromUtm(utm, precision) {
const latLon = Utm.toLatLon(utm);
return GeohashExtra.encodeFromLatLng(latLon.lat, latLon.lon, precision);
}
static sortGeohashesByNeighbourDirection(existsGeohashesList, geohashesToAddList) {
if (!geohashesToAddList.length) {
return geohashesToAddList;
}
existsGeohashesList = clone(existsGeohashesList);
let actualGeohashesToAdd = clone(geohashesToAddList);
let preparedGeohashes = [];
let i = 0;
while (preparedGeohashes.length < geohashesToAddList.length) {
const geohash = geohashesToAddList[i];
i++;
if (!geohash) {
break;
}
if (includes(existsGeohashesList, geohash)) {
actualGeohashesToAdd.splice(actualGeohashesToAdd.indexOf(geohash), 1);
continue;
}
const neighbour = Geohash.getNeighbourWithDirection(geohash, existsGeohashesList);
if (!neighbour.geohash) {
continue;
}
preparedGeohashes.push({
geohash: geohash,
neighbour: neighbour.geohash,
direction: neighbour.direction
});
existsGeohashesList.push(geohash);
actualGeohashesToAdd.splice(actualGeohashesToAdd.indexOf(geohash), 1);
}
if (preparedGeohashes.length > 0 && actualGeohashesToAdd.length > 0) {
const additionalPreparedGeohashes = Geohash.sortGeohashesByNeighbourDirection(existsGeohashesList, actualGeohashesToAdd);
if (additionalPreparedGeohashes.length) {
preparedGeohashes = concat(additionalPreparedGeohashes, preparedGeohashes);
}
}
return preparedGeohashes;
}
static sortGeohashesByFreeTwoDirections(geohashesToRemoveList) {
if (!geohashesToRemoveList.length) {
return geohashesToRemoveList;
}
geohashesToRemoveList = uniq(geohashesToRemoveList);
let preparedGeohashes = [];
let i = 0;
while (preparedGeohashes.length < geohashesToRemoveList.length) {
const geohash = geohashesToRemoveList[i];
i++;
if (!geohash) {
break;
}
// TODO: add algorithm for get geohashes in order without neighbour in two directions
preparedGeohashes.push({
geohash: geohash,
direction1: 'e',
direction2: 's'
});
}
return preparedGeohashes;
}
static autoBboxes(leftBoundGeohash, rightBoundGeohash, preferredGeohashes = 10, startPrecision = 1) {
let leftBoundPoint = this.decodeToLatLon(leftBoundGeohash);
let rightBoundPoint = this.decodeToLatLon(rightBoundGeohash);
let minLat = Math.min(leftBoundPoint.lat, rightBoundPoint.lat);
let minLon = Math.min(leftBoundPoint.lon, rightBoundPoint.lon);
let maxLat = Math.max(leftBoundPoint.lat, rightBoundPoint.lat);
let maxLon = Math.max(leftBoundPoint.lon, rightBoundPoint.lon);
let resultGeohashes;
let precision = startPrecision;
do {
resultGeohashes = ngeohash.bboxes(minLat, minLon, maxLat, maxLon, precision);
precision++;
if (precision > 13) {
break;
}
} while (resultGeohashes.length < preferredGeohashes);
const parentsToChildren = {};
const parentsForMerge = [];
resultGeohashes.forEach((geohash) => {
const parent = Geohash.getParent(geohash);
if (!parentsToChildren[parent]) {
parentsToChildren[parent] = [];
}
parentsToChildren[parent].push(geohash);
if (parentsToChildren[parent].length === 32) {
parentsForMerge.push(parent);
}
});
for (let i = 0; i < parentsForMerge.length; i++) {
const geohashParent = parentsForMerge[i];
parentsToChildren[geohashParent].forEach((geohash) => {
resultGeohashes.splice(resultGeohashes.indexOf(geohash), 1);
});
resultGeohashes.push(geohashParent);
const parentOfParent = Geohash.getParent(geohashParent);
if (!parentsToChildren[parentOfParent]) {
parentsToChildren[parentOfParent] = [];
}
parentsToChildren[parentOfParent].push(geohashParent);
if (parentsToChildren[parentOfParent].length === 32) {
parentsForMerge.push(parentOfParent);
}
}
return resultGeohashes;
}
};