-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (71 loc) · 2.27 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
//http://en.wikipedia.org/wiki/Haversine_formula
//http://www.movable-type.co.uk/scripts/latlong.html
var point = require('turf-point');
/**
* Takes a {@link Point} and calculates the location of a destination point given a distance in degrees, radians, miles, or kilometers; and bearing in degrees. This uses the [Haversine formula](http://en.wikipedia.org/wiki/Haversine_formula) to account for global curvature.
*
* @module turf/destination
* @category measurement
* @param {Feature<Point>} start starting point
* @param {Number} distance distance from the starting point
* @param {Number} bearing ranging from -180 to 180
* @param {String} units miles, kilometers, degrees, or radians
* @returns {Feature<Point>} destination point
* @example
* var point = {
* "type": "Feature",
* "properties": {
* "marker-color": "#0f0"
* },
* "geometry": {
* "type": "Point",
* "coordinates": [-75.343, 39.984]
* }
* };
* var distance = 50;
* var bearing = 90;
* var units = 'miles';
*
* var destination = turf.destination(point, distance, bearing, units);
* destination.properties['marker-color'] = '#f00';
*
* var result = {
* "type": "FeatureCollection",
* "features": [point, destination]
* };
*
* //=result
*/
module.exports = function (point1, distance, bearing, units) {
var coordinates1 = point1.geometry.coordinates;
var longitude1 = toRad(coordinates1[0]);
var latitude1 = toRad(coordinates1[1]);
var bearing_rad = toRad(bearing);
var R = 0;
switch (units) {
case 'miles':
R = 3960;
break;
case 'kilometers':
case 'kilometres':
R = 6373;
break;
case 'degrees':
R = 57.2957795;
break;
case 'radians':
R = 1;
break;
}
var latitude2 = Math.asin(Math.sin(latitude1) * Math.cos(distance / R) +
Math.cos(latitude1) * Math.sin(distance / R) * Math.cos(bearing_rad));
var longitude2 = longitude1 + Math.atan2(Math.sin(bearing_rad) * Math.sin(distance / R) * Math.cos(latitude1),
Math.cos(distance / R) - Math.sin(latitude1) * Math.sin(latitude2));
return point([toDeg(longitude2), toDeg(latitude2)]);
};
function toRad(degree) {
return degree * Math.PI / 180;
}
function toDeg(rad) {
return rad * 180 / Math.PI;
}