forked from kmoe/slack-pokebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo.js
68 lines (58 loc) · 1.57 KB
/
geo.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
'use strict';
const geolib = require('geolib');
const geocoder = require('geocoder');
function getDistance(a, b) {
return geolib.getDistance(a, b);
}
function reverseGeoCode(location, callback) {
geocoder.reverseGeocode(location.latitude, location.longitude, (err, data) => {
if (data && data.results && data.results[0]) {
callback(` (${data.results[0].formatted_address})`);
} else {
callback('');
}
});
}
function radians(deg) {
return (deg * Math.PI) / 180;
}
function degrees(rad) {
return (rad * 180) / Math.PI;
}
function toRadians(a) {
return {
latitude: radians(a.latitude),
longitiude: radians(a.longitude),
};
}
function getBearing(start, target) {
const s = toRadians(start);
const d = toRadians(target);
let dLong = d.longitiude - s.longitiude;
const inner =
Math.tan((d.latitude / 2.0) + (Math.PI / 4.0)) /
Math.tan((s.latitude / 2.0) + (Math.PI / 4.0));
const dPhi = Math.log(inner);
if (Math.abs(dLong) > Math.PI) {
if (dLong > 0.0) {
dLong = -((2 * Math.PI) - dLong);
} else {
dLong = ((2 * Math.PI) + dLong);
}
}
const bearing = degrees(Math.atan2(dLong, dPhi));
return bearing;
}
const cardinals = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'];
function cardinalBearing(deg) {
const degreesPerItem = 360 / cardinals.length;
const option = (deg + (degreesPerItem * 0.5)) / degreesPerItem;
const index = Math.floor(option + cardinals.length) % cardinals.length;
return cardinals[index];
}
module.exports = {
getDistance,
reverseGeoCode,
getBearing,
cardinalBearing,
};