65 lines
2.4 KiB
JavaScript
65 lines
2.4 KiB
JavaScript
/*
|
|
Copyright (c) 2023-forever Douglas Malnati. All rights reserved.
|
|
|
|
See the /faq/tos page for details.
|
|
|
|
(If this generated header is stamped on a file which is a 3rd party file or under a different license or copyright, then ignore this copyright statement and use that file's terms.)
|
|
*/
|
|
|
|
// Adapted from https://github.com/mwgg/GreatCircle
|
|
|
|
export let GreatCircle = {
|
|
|
|
validateRadius: function(unit) {
|
|
let r = {'M': 6371009, 'KM': 6371.009, 'MI': 3958.761, 'NM': 3440.070, 'YD': 6967420, 'FT': 20902260};
|
|
if ( unit in r ) return r[unit];
|
|
else return unit;
|
|
},
|
|
|
|
distance: function(lat1, lon1, lat2, lon2, unit) {
|
|
if ( unit === undefined ) unit = 'KM';
|
|
let r = this.validateRadius(unit);
|
|
lat1 *= Math.PI / 180;
|
|
lon1 *= Math.PI / 180;
|
|
lat2 *= Math.PI / 180;
|
|
lon2 *= Math.PI / 180;
|
|
let lonDelta = lon2 - lon1;
|
|
let a = Math.pow(Math.cos(lat2) * Math.sin(lonDelta) , 2) + Math.pow(Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lonDelta) , 2);
|
|
let b = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lonDelta);
|
|
let angle = Math.atan2(Math.sqrt(a) , b);
|
|
|
|
return angle * r;
|
|
},
|
|
|
|
bearing: function(lat1, lon1, lat2, lon2) {
|
|
lat1 *= Math.PI / 180;
|
|
lon1 *= Math.PI / 180;
|
|
lat2 *= Math.PI / 180;
|
|
lon2 *= Math.PI / 180;
|
|
let lonDelta = lon2 - lon1;
|
|
let y = Math.sin(lonDelta) * Math.cos(lat2);
|
|
let x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lonDelta);
|
|
let brng = Math.atan2(y, x);
|
|
brng = brng * (180 / Math.PI);
|
|
|
|
if ( brng < 0 ) { brng += 360; }
|
|
|
|
return brng;
|
|
},
|
|
|
|
destination: function(lat1, lon1, brng, dt, unit) {
|
|
if ( unit === undefined ) unit = 'KM';
|
|
let r = this.validateRadius(unit);
|
|
lat1 *= Math.PI / 180;
|
|
lon1 *= Math.PI / 180;
|
|
let lat3 = Math.asin(Math.sin(lat1) * Math.cos(dt / r) + Math.cos(lat1) * Math.sin(dt / r) * Math.cos( brng * Math.PI / 180 ));
|
|
let lon3 = lon1 + Math.atan2(Math.sin( brng * Math.PI / 180 ) * Math.sin(dt / r) * Math.cos(lat1) , Math.cos(dt / r) - Math.sin(lat1) * Math.sin(lat3));
|
|
|
|
return {
|
|
'LAT': lat3 * 180 / Math.PI,
|
|
'LON': lon3 * 180 / Math.PI
|
|
};
|
|
}
|
|
|
|
};
|