s2-tools
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
143 lines • 4.46 kB
JavaScript
import { EARTH_RADIUS } from '../../space/planets';
import { angle } from '../s2/point';
import { getDistance } from '../ll';
import { degToRad, radToDeg } from '..';
/**
* convert an angle in degrees to an angle in radians
* @param angle - input angle in degrees
* @returns - angle in radians
*/
export function fromDegrees(angle) {
return degToRad(angle);
}
/**
* convert an angle in radians to an angle in degrees
* @param angle - input angle in radians
* @returns - angle in degrees
*/
export function toDegrees(angle) {
return radToDeg(angle);
}
/**
* build an angle in E5 format.
* @param e5_ - input angle in degrees
* @returns - e5 angle in radians
*/
export function toE5(e5_) {
return fromDegrees(e5_ * 1e-5);
}
/**
* build an angle in E6 format.
* @param e6_ - input angle in degrees
* @returns - e6 angle in radians
*/
export function toE6(e6_) {
return fromDegrees(e6_ * 1e-6);
}
/**
* build an angle in E7 format.
* @param e7_ - input angle in degrees
* @returns - e7 angle in radians
*/
export function toE7(e7_) {
return fromDegrees(e7_ * 1e-7);
}
/**
* Return the angle between two points, which is also equal to the distance
* between these points on the unit sphere. The points do not need to be
* normalized. This function has a maximum error of 3.25 * DBL_EPSILON (or
* 2.5 * DBL_EPSILON for angles up to 1 radian). If either point is
* zero-length (e.g. an uninitialized S2Point), or almost zero-length, the
* resulting angle will be zero.
* @param a - The first point
* @param b - The second point
* @returns - The angle between the two points in radians
*/
export function fromS2Points(a, b) {
return angle(a, b);
}
/**
* Like the constructor above, but return the angle (i.e., distance) between
* two S2LatLng points. This function has about 15 digits of accuracy for
* small distances but only about 8 digits of accuracy as the distance
* approaches 180 degrees (i.e., nearly-antipodal points).
* @param a - The first lon-lat pair
* @param b - The second lon-lat pair
* @returns - The angle between the two points in radians
*/
export function fromLonLat(a, b) {
return getDistance(a, b);
}
/**
* convert an angle in radians to an angle in meters
* @param angle - input angle in radians
* @param radius - radius of the planet (defaults to Earth's radius)
* @returns - angle in meters
*/
export function toMeters(angle, radius = EARTH_RADIUS) {
return angle * radius;
}
/**
* convert an angle in meters to an angle in radians
* @param angle - angle in meters
* @param radius - radius of the planet (defaults to Earth's radius)
* @returns - angle in radians
*/
export function fromMeters(angle, radius = EARTH_RADIUS) {
return angle / radius;
}
/**
* convert an angle in radians to an angle in kilometers
* @param angle - input angle in radians
* @param radius - radius of the planet (defaults to Earth's radius)
* @returns - angle in meters
*/
export function toKM(angle, radius = EARTH_RADIUS) {
return (angle * radius) / 1_000;
}
/**
* convert an angle in kilometers to an angle in radians
* @param angle - angle in kilometers
* @param radius - radius of the planet (defaults to Earth's radius)
* @returns - angle in radians
*/
export function fromKM(angle, radius = EARTH_RADIUS) {
return (angle * 1_000) / radius;
}
// Note that the E5, E6, and E7 conversion involve two multiplications rather
// than one. This is mainly for backwards compatibility (changing this would
// break many tests), but it does have the nice side effect that conversions
// between Degrees, E6, and E7 are exact when the arguments are integers.
/**
* Build an angle in E5 format.
* @param angle - input angle in radians
* @returns - an e5 angle in degrees
*/
export function e5(angle) {
return toDegrees(angle) * 1e5;
}
/**
* Build an angle in E6 format.
* @param angle - input angle in radians
* @returns - an e6 angle in degrees
*/
export function e6(angle) {
return toDegrees(angle) * 1e6;
}
/**
* Build an angle in E7 format.
* @param angle - input angle in radians
* @returns - an e7 angle in degrees
*/
export function e7(angle) {
return toDegrees(angle) * 1e7;
}
/**
* Normalize this angle to the range (-180, 180] degrees.
* @param angle - input angle in radians
* @returns - normalized angle in radians
*/
export function normalize(angle) {
return angle % (2 * Math.PI);
}
//# sourceMappingURL=angle.js.map