atriusmaps-node-sdk
Version:
This project provides an API to Atrius Personal Wayfinder maps within a Node environment. See the README.md for more information
56 lines (46 loc) • 1.71 kB
JavaScript
;
/*
This utility module contains a set of functions dealing with "Geodesy" - measurements of
the earth, its surface and related geometry.
For a more cohesive set of functions, see https://github.com/chrisveness/geodesy
*/
// degrees to radians
const toRadians = d => (d * Math.PI) / 180;
/**
* Haversine formula for finding distance between two lat/lng points.
* from https://www.movable-type.co.uk/scripts/latlong.html
* @param {float} lat1 latitutde of point 1
* @param {float} lng1 longitude of point 1
* @param {float} lat2 latitutde of point 2
* @param {float} lng2 longitude of point 2
* @returns number (in meters) between p1 and p2
*/
function distance(lat1, lng1, lat2, lng2) {
const R = 6371000; // approx. earth radius (meters)
const φ1 = toRadians(lat1);
const φ2 = toRadians(lat2);
const Δφ = toRadians(lat2 - lat1);
const Δλ = toRadians(lng2 - lng1);
const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) + Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}
function bearingToDirection(deg, T) {
const names = [
T('wayfinder:north'), // 0
T('wayfinder:northeast'), // 45
T('wayfinder:east'), // 90
T('wayfinder:southeast'), // 135
T('wayfinder:south'), // 180 / -180
T('wayfinder:southwest'), // -135
T('wayfinder:west'), // -90
T('wayfinder:northwest'), // -45
];
// normalize -180..180
const d = ((deg + 180) % 360) - 180;
const index = Math.round(d / 45);
return names[(index + 8) % 8];
}
exports.bearingToDirection = bearingToDirection;
exports.distance = distance;
exports.toRadians = toRadians;