greatcircledistcalculator
Version:
This is to calculate great circledistance
91 lines (90 loc) • 4.01 kB
JavaScript
;
var InvalidLatitude = new Error("Invalid Latitude, Latitude should be in between -90 and 90!");
var InvalidLongitude = new Error("Invalid Longitude, Longitude should be in between -180 and 180!");
var GreatCircleDistance = /** @class */ (function () {
function GreatCircleDistance(radius) {
if (radius === void 0) { radius = 6371; }
this.fromLocation = { latitude: 0.0, longitude: 0.0 };
/**
* - Enter radius in kilometers, default is Earth's radius
*/
this.radius = radius;
}
GreatCircleDistance.prototype.setFromLocation = function (latitude, longitude) {
if (latitude === void 0) { latitude = 28.5272803; }
if (longitude === void 0) { longitude = 77.0688994; }
/**
* - This function will be set fromLocation's coordinates in Radian
* - Function will throw InvalidLatitude or InvalidLongitude exception in case of
* invalid value of latitude or longitude
* - Default latitude and longitude are of New Delhi, India
*/
if (!this.isValidLatitude(latitude)) {
throw InvalidLatitude;
}
if (!this.isValidLongitude(longitude)) {
throw InvalidLongitude;
}
this.fromLocation.latitude = this.degreeToRadian(latitude);
this.fromLocation.longitude = this.degreeToRadian(longitude);
};
GreatCircleDistance.prototype.isValidLatitude = function (latitude) {
/**
* This function will return true if, Latitude is valid, otherwise it will return false
*/
if (latitude > -90 && latitude < 90) {
return true;
}
return false;
};
GreatCircleDistance.prototype.isValidLongitude = function (longitude) {
/**
* This function will return true if, Longitude is valid, otherwise it will return false
*/
if (longitude > -180 && longitude < 180) {
return true;
}
return false;
};
GreatCircleDistance.prototype.degreeToRadian = function (coordinate) {
/**
* - This function will convert coordinate in degree to radian
*/
return (coordinate * Math.PI) / 180;
};
GreatCircleDistance.prototype.getDistanceToLocationInKM = function (latitude, longitude) {
/**
* - This function will return the distance in Kilometer
* - Formula of Great Circle Distance: d = rcos-1[cos a cos b cos(x-y) + sin a sin b]
* - Function will through InvalidLatitude or InvalidLogitude exception in case of
* invalid value of latitude or longitude
* - Default latitude and longitude are of New Delhi, India
*/
if (latitude === void 0) { latitude = 28.5272803; }
if (longitude === void 0) { longitude = 77.0688994; }
if (!this.isValidLatitude(latitude)) {
throw InvalidLatitude;
}
if (!this.isValidLongitude(longitude)) {
throw InvalidLongitude;
}
var latitudeInRadian = this.degreeToRadian(latitude);
var longitudeInRadian = this.degreeToRadian(longitude);
var centralAngle = this.getCentralAngle(latitudeInRadian, longitudeInRadian);
var distance = this.radius * centralAngle;
return Math.round(distance * 100) / 100;
};
GreatCircleDistance.prototype.getCentralAngle = function (lat1, long1) {
/**
* - This function will return the central angle
*/
var diffLongitudes = Math.abs(this.fromLocation.longitude - long1);
var centralAngle = Math.acos(Math.sin(lat1) * Math.sin(this.fromLocation.latitude) +
Math.cos(lat1) *
Math.cos(this.fromLocation.latitude) *
Math.cos(diffLongitudes));
return centralAngle;
};
return GreatCircleDistance;
}());
module.exports = GreatCircleDistance;