UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

134 lines 6.15 kB
"use strict"; var GeographicalPosition_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.GeographicalPosition = void 0; const tslib_1 = require("tslib"); const AngleUnit_1 = require("../../utils/unit/AngleUnit"); const LengthUnit_1 = require("../../utils/unit/LengthUnit"); const decorators_1 = require("../decorators"); const Absolute3DPosition_1 = require("./Absolute3DPosition"); const utils_1 = require("../../utils"); /** * Geographical WGS 84 position stored as an 3D vector in ISO 6709. * @category Position */ let GeographicalPosition = GeographicalPosition_1 = class GeographicalPosition extends Absolute3DPosition_1.Absolute3DPosition { constructor(lat, lng, amsl, gcs = utils_1.GCS.WGS84) { super(); this.fromVector(new utils_1.Vector3(lng, lat, amsl), gcs); } /** * Geographical Latitude * @returns {number} Latitude */ get latitude() { return this.y; } set latitude(lat) { this.y = lat; } /** * Geographical Longitude * @returns {number} Longitude */ get longitude() { return this.x; } set longitude(lng) { this.x = lng; } /** * Altitude above mean sea level * @returns {number} Altitude */ get altitude() { return this.z; } set altitude(amsl) { this.z = amsl; } /** * Get the distance from this location to a destination * @param {GeographicalPosition} destination Destination location * @returns {number} Distance between this point and destination */ distanceTo(destination) { return super.distanceTo(destination, utils_1.HAVERSINE); } /** * Get the bearing in degrees from this location to a destination * @param {GeographicalPosition} destination Destination location * @returns {number} Bearing in degrees from this position to destination */ bearing(destination) { return AngleUnit_1.AngleUnit.RADIAN.convert(this.angleTo(destination), AngleUnit_1.AngleUnit.DEGREE); } /** * Get the bearing in radians from this location to a destination * @param {GeographicalPosition} destination Destination location * @returns {number} Bearing in radians from this position to destination */ angleTo(destination) { const lonRadA = AngleUnit_1.AngleUnit.DEGREE.convert(this.longitude, AngleUnit_1.AngleUnit.RADIAN); const latRadA = AngleUnit_1.AngleUnit.DEGREE.convert(this.latitude, AngleUnit_1.AngleUnit.RADIAN); const lonRadB = AngleUnit_1.AngleUnit.DEGREE.convert(destination.longitude, AngleUnit_1.AngleUnit.RADIAN); const latRadB = AngleUnit_1.AngleUnit.DEGREE.convert(destination.latitude, AngleUnit_1.AngleUnit.RADIAN); const y = Math.sin(lonRadB - lonRadA) * Math.cos(latRadB); const x = Math.cos(latRadA) * Math.sin(latRadB) - Math.sin(latRadA) * Math.cos(latRadB) * Math.cos(lonRadB - lonRadA); return Math.atan2(y, x); } destination(bearing, distance, bearingUnit = AngleUnit_1.AngleUnit.DEGREE, distanceUnit = LengthUnit_1.LengthUnit.METER) { distance = distanceUnit.convert(distance, LengthUnit_1.LengthUnit.METER); const brng = bearingUnit.convert(bearing, AngleUnit_1.AngleUnit.RADIAN); const lonRadA = bearingUnit.convert(this.longitude, AngleUnit_1.AngleUnit.RADIAN); const latRadA = bearingUnit.convert(this.latitude, AngleUnit_1.AngleUnit.RADIAN); const latX = Math.asin(Math.sin(latRadA) * Math.cos(distance / utils_1.GCS.EARTH_RADIUS_MEAN) + Math.cos(latRadA) * Math.sin(distance / utils_1.GCS.EARTH_RADIUS_MEAN) * Math.cos(brng)); const lonX = lonRadA + Math.atan2(Math.sin(brng) * Math.sin(distance / utils_1.GCS.EARTH_RADIUS_MEAN) * Math.cos(latRadA), Math.cos(distance / utils_1.GCS.EARTH_RADIUS_MEAN) - Math.sin(latRadA) * Math.sin(latX)); const location = new GeographicalPosition_1(); location.latitude = AngleUnit_1.AngleUnit.RADIAN.convert(latX, AngleUnit_1.AngleUnit.DEGREE); location.longitude = AngleUnit_1.AngleUnit.RADIAN.convert(lonX, AngleUnit_1.AngleUnit.DEGREE); location.altitude = this.altitude; location.unit = this.unit; return location; } fromVector(vector, unit = utils_1.GCS.WGS84) { let converted; if (unit instanceof LengthUnit_1.LengthUnit) { converted = utils_1.GCS.ECEF.convert(new utils_1.Vector3(unit.convert(vector.x, LengthUnit_1.LengthUnit.METER), unit.convert(vector.y, LengthUnit_1.LengthUnit.METER), unit.convert(vector.z, LengthUnit_1.LengthUnit.METER)), utils_1.GCS.WGS84); } else if (unit instanceof utils_1.GCS) { converted = unit !== utils_1.GCS.WGS84 ? unit.convert(vector, utils_1.GCS.WGS84) : vector; } this.x = converted.x; this.y = converted.y; this.z = converted.z; return this; } toVector3(unit = utils_1.GCS.WGS84) { if (unit instanceof utils_1.GCS) { return utils_1.GCS.WGS84.convert(new utils_1.Vector3(this.x, this.y, this.z), unit); } else if (unit instanceof LengthUnit_1.LengthUnit) { return utils_1.GCS.WGS84.convert(new utils_1.Vector3(LengthUnit_1.LengthUnit.METER.convert(this.x, unit), LengthUnit_1.LengthUnit.METER.convert(this.y, unit), LengthUnit_1.LengthUnit.METER.convert(this.z, unit)), utils_1.GCS.ECEF); } } /** * Clone the position * @returns {GeographicalPosition} Cloned geographical position */ clone() { const position = super.clone(); position.latitude = this.latitude; position.longitude = this.longitude; position.z = this.altitude; return position; } }; exports.GeographicalPosition = GeographicalPosition; exports.GeographicalPosition = GeographicalPosition = GeographicalPosition_1 = tslib_1.__decorate([ (0, decorators_1.SerializableObject)(), tslib_1.__metadata("design:paramtypes", [Number, Number, Number, utils_1.GCS]) ], GeographicalPosition); //# sourceMappingURL=GeographicalPosition.js.map