-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
209 lines (181 loc) · 4.93 KB
/
index.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
/* jshint node:true */
// Provides both an encode function (lat,lon) and decode function(hash) to move between
// geographic locations and geohashes
//
// Imported from [github](https://raw.github.com/aisaacs/geohash/master/GeoHash.js)
// (C) 2012 AISAACS
var DICT = '0123456789bcdefghjkmnpqrstuvwxyz';
module.exports.distance = function(start,end) {
var dLat = Math.abs(end.lat - start.lat) * Math.PI/180;
var dlng = Math.abs(end.lng - start.lng) * Math.PI/180;
var lat1 = end.lat * Math.PI/180;
var lat2 = start.lat * Math.PI/180;
var d1 = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dlng/2) * Math.sin(dlng/2) * Math.cos(lat1) * Math.cos(lat2);
return (2 * Math.atan2(Math.sqrt(d1), Math.sqrt(1-d1))) * 6371*0.621371;
};
module.exports.area = function(fn,precision) {
var res = [];
function visit(bounds,flag) {
var mid;
flag = flag || fn(bounds);
if (!flag) return;
bounds.bit++;
if (bounds.bit && bounds.bit % 5 === 0) {
bounds.hash += DICT[bounds.ch];
bounds.ch = 0;
if (flag == 2 || bounds.hash.length >= precision)
return res.push(bounds);
}
if (bounds.bit % 2 === 0) {
mid = (bounds.left + bounds.right) / 2;
visit({
hash : bounds.hash,
bit : bounds.bit,
ch : (bounds.ch << 1) +1,
left : mid,
right : bounds.right,
top : bounds.top,
bottom : bounds.bottom
});
visit({
hash : bounds.hash,
bit : bounds.bit,
ch : (bounds.ch << 1),
left : bounds.left,
right : mid,
top : bounds.top,
bottom : bounds.bottom
});
} else {
mid = (bounds.top+bounds.bottom) / 2;
visit({
hash : bounds.hash,
bit : bounds.bit,
ch : (bounds.ch << 1) +1,
left : bounds.left,
right : bounds.right,
top : bounds.top,
bottom : mid
});
visit({
hash : bounds.hash,
bit : bounds.bit,
ch : (bounds.ch << 1),
left : bounds.left,
right : bounds.right,
top : mid,
bottom : bounds.bottom
});
}
}
visit({
ch : 0,
bit : -1,
hash : '',
bottom: -90.0,
top: 90.0,
left: -180,
right: 180
});
return res;
};
function checkRect(box) {
// We check if rectangles are not intersecting and return the negative
// See [Stackoverflow](http://stackoverflow.com/questions/16005136)
return function(d) {
if (d.top < box.top && d.bottom > box.bottom && d.left > box.left && d.right < box.right) return 2;
var left = d.right < box.left,
right = d.left > box.right,
top = d.bottom > box.top,
bottom = d.top < box.bottom;
return !(left || right || top || bottom);
};
}
module.exports.rect = function(lat,lng,dist,precision) {
// Default distance is 5 miles
dist = dist || 5;
// Default Precision is 5
precision = precision || 5;
// 1° of latitude ~= 69 miles and 1° of longitude ~= cos(latitude)*69
// See [Mysql Distance, page 12](http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL)
var latDist = dist/69,
lngDist = dist / Math.abs(Math.cos(lat * Math.PI / 180)*69);
// See [RASC Calgary](http://calgary.rasc.ca/latlong.htm) for more detailed formulas
var box = {
top : lat + latDist,
bottom: lat - latDist,
left : lng - lngDist,
right : lng + lngDist
};
return module.exports.area(checkRect(box),precision);
};
module.exports.encode = function(lat, lon, precision) {
if (!precision) precision = 12;
var res = '',mid;
var bounds = {
bottom: -90.0,
top: 90.0,
left: -180,
right: 180
};
var ch = 0;
var bit = 0;
while (res.length < precision){
if (bit % 2 ===0) {
mid = (bounds.left + bounds.right) / 2;
if (lon > mid) {
ch |= 16 >> (bit % 5);
bounds.left = mid;
} else {
bounds.right = mid;
}
} else {
mid = (bounds.top + bounds.bottom) / 2;
if (lat > mid) {
ch |= 16 >> (bit % 5);
bounds.bottom = mid;
} else {
bounds.top = mid;
}
}
bit ++;
if (bit % 5 === 0) {
res += DICT[ch];
ch = 0;
}
}
return res;
};
module.exports.decode = function(hash) {
var bounds = {
bottom: -90.0,
top: 90.0,
left: -180,
right: 180
};
var bit = 0,mid;
for (var i = 0, l = hash.length; i < l; i++){
var ch = DICT.indexOf(hash[i]);
for (var j = 0; j < 5; j++){
if (bit % 2 === 0) {
mid = (bounds.left + bounds.right) / 2;
if (ch & (16 >> (bit % 5))){
bounds.left = mid;
} else {
bounds.right = mid;
}
} else {
mid = (bounds.top + bounds.bottom) / 2;
if (ch & (16 >> (bit % 5))){
bounds.bottom = mid;
} else {
bounds.top = mid;
}
}
bit ++;
}
}
bounds.lat = (bounds.top + bounds.bottom) / 2;
bounds.lng = (bounds.left + bounds.right) / 2;
return bounds;
};