-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
69 lines (63 loc) · 1.62 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
//http://en.wikipedia.org/wiki/Haversine_formula
//http://www.movable-type.co.uk/scripts/latlong.html
/**
* Takes two {@link Point|points} and finds the geographic bearing between them.
*
* @module turf/bearing
* @category measurement
* @param {Feature<Point>} start starting Point
* @param {Feature<Point>} end ending Point
* @category measurement
* @returns {Number} bearing in decimal degrees
* @example
* var point1 = {
* "type": "Feature",
* "properties": {
* "marker-color": '#f00'
* },
* "geometry": {
* "type": "Point",
* "coordinates": [-75.343, 39.984]
* }
* };
* var point2 = {
* "type": "Feature",
* "properties": {
* "marker-color": '#0f0'
* },
* "geometry": {
* "type": "Point",
* "coordinates": [-75.534, 39.123]
* }
* };
*
* var points = {
* "type": "FeatureCollection",
* "features": [point1, point2]
* };
*
* //=points
*
* var bearing = turf.bearing(point1, point2);
*
* //=bearing
*/
module.exports = function (point1, point2) {
var coordinates1 = point1.geometry.coordinates;
var coordinates2 = point2.geometry.coordinates;
var lon1 = toRad(coordinates1[0]);
var lon2 = toRad(coordinates2[0]);
var lat1 = toRad(coordinates1[1]);
var lat2 = toRad(coordinates2[1]);
var a = Math.sin(lon2 - lon1) * Math.cos(lat2);
var b = Math.cos(lat1) * Math.sin(lat2) -
Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1);
var bearing = toDeg(Math.atan2(a, b));
return bearing;
};
function toRad(degree) {
return degree * Math.PI / 180;
}
function toDeg(radian) {
return radian * 180 / Math.PI;
}