hunter-news-engines
Version:
Contains engine defition for Hunter News Social Network
177 lines • 8.48 kB
JavaScript
///<reference path="../lib/ngeohash.d.ts"/>
///<reference path="../lib/geolib.d.ts"/>
;
var hunter_news_exceptions_1 = require("hunter-news-exceptions");
var GeoLib = require("geolib");
var GeoHash = require("ngeohash");
var LatLongEngine = (function () {
function LatLongEngine() {
}
/** Valida que la latitud sea válida. Lanza un error en caso de no ser válido */
LatLongEngine.prototype.validateLatitude = function (latitude) {
if (latitude == null || isNaN(latitude))
throw new hunter_news_exceptions_1.CommonException('La componente "Latitud" del punto es inválida');
if (latitude > 90 || latitude < -90)
throw new hunter_news_exceptions_1.CommonException('La componente "Latitud" está fuera del rango permitido. Esta debe estar entre 90 y -90. El valor ingresado es:' + latitude);
};
/** Valida que la longitud sea válida. Lanza un error en caso de no ser válido */
LatLongEngine.prototype.validateLongitude = function (longitude) {
if (longitude == null || isNaN(longitude))
throw new hunter_news_exceptions_1.CommonException('La componente "Longitud" del punto es inválida');
if (longitude > 180 || longitude < -180)
throw new hunter_news_exceptions_1.CommonException('La componente "Longitud" está fuera del rango permitido. Esta debe estar entre 180 y -180. El valor ingresado es:' + longitude);
};
/** Valida que el punto sea válido. Lanza un error en caso de no ser válido */
LatLongEngine.prototype.validateDot = function (dot) {
if (dot == null)
throw new hunter_news_exceptions_1.CommonException('El punto es nulo. Debe proporcionar un objeto válido.');
this.validateLatitude(dot.latitude);
this.validateLongitude(dot.longitude);
};
/** Contiene los rangos de distancias y la precisión del hash que debiera utilizarse */
LatLongEngine.prototype.getDistanceRange = function () {
var ranges = [
{ since: 0, until: 5000, value: 7 },
{ since: 5000, until: 15000, value: 6 },
{ since: 15000, until: 50000, value: 5 },
{ since: 50000, until: 200000, value: 4 },
{ since: 200000, until: 600000, value: 3 },
{ since: 600000, until: 1200000, value: 2 },
{ since: 1200000, until: 9999999, value: 1 }
];
return ranges;
};
/** Obtiene el vértice mínimo de un cuadrilatero */
LatLongEngine.prototype.getMinDot = function (dot1, dot2, dot3, dot4) {
var dots = [];
dots.push(dot1);
dots.push(dot2);
dots.push(dot3);
dots.push(dot3);
var latitudes = dots.map(function (dto) { return dto.latitude; }).filter(function (x, i, a) { return a.indexOf(x) == i; });
var longitudes = dots.map(function (dto) { return dto.longitude; }).filter(function (x, i, a) { return a.indexOf(x) == i; });
var minLat;
var minLon;
//Se establecen las latitudes máximas y mínimas
if (latitudes[0] > latitudes[1])
minLat = latitudes[1];
else
minLat = latitudes[0];
//Se establecen las longitudes máximas y mínimas
if (longitudes[0] > longitudes[1])
minLon = longitudes[1];
else
minLon = longitudes[0];
return { latitude: minLat, longitude: minLon };
};
/** Obtiene el vertice superior de un cuadrilatero */
LatLongEngine.prototype.getMaxDot = function (dot1, dot2, dot3, dot4) {
var dots = [];
dots.push(dot1);
dots.push(dot2);
dots.push(dot3);
dots.push(dot3);
var latitudes = dots.map(function (dto) { return dto.latitude; }).filter(function (x, i, a) { return a.indexOf(x) == i; });
var longitudes = dots.map(function (dto) { return dto.longitude; }).filter(function (x, i, a) { return a.indexOf(x) == i; });
var maxLat;
var maxLon;
//Se establecen las latitudes máximas y mínimas
if (latitudes[0] < latitudes[1])
maxLat = latitudes[1];
else
maxLat = latitudes[0];
//Se establecen las longitudes máximas y mínimas
if (longitudes[0] < longitudes[1])
maxLon = longitudes[1];
else
maxLon = longitudes[0];
return { latitude: maxLat, longitude: maxLon };
};
/** Obtiene la precisión del hash */
LatLongEngine.prototype.getHashPrecision = function (start, end) {
try {
var distance_1 = GeoLib.getDistance(start, end, 0, 1);
if (distance_1 < 0)
distance_1 = distance_1 * -1;
var range = this.getDistanceRange();
var hashPrecision_1;
range.forEach(function (item) {
if (item.since <= distance_1 && item.until > distance_1) {
hashPrecision_1 = item.value;
return true;
}
});
return hashPrecision_1;
}
catch (err) {
throw new hunter_news_exceptions_1.CommonException('Se produjo un error inesperado al calcular la precisión del hash.', err);
}
};
/** calcula el GeoHash de un punto dado */
LatLongEngine.prototype.makeGeoHash = function (point) {
try {
//Se valida que el punto sea válido
this.validateDot(point);
//Se retorna el GeoHash del punto
return GeoHash.encode(point.latitude, point.longitude);
}
catch (err) {
throw new hunter_news_exceptions_1.CommonException('Se produjo un error inesperado al calcular el Key de un punto geolocalizado.', err);
}
};
/** Calcula el punto dado a partir de un GeoHash */
LatLongEngine.prototype.makeDot = function (hash) {
try {
var dot = GeoHash.decode(hash);
return dot;
}
catch (err) {
throw new hunter_news_exceptions_1.CommonException('Se produjo un error inesperado al obtener el punto a partir del hash.', err);
}
};
/** Calcula los GeoHash para la zona visible */
LatLongEngine.prototype.getHashFromVisibleArea = function (dot1, dot2, dot3, dot4) {
try {
this.validateDot(dot1);
this.validateDot(dot2);
this.validateDot(dot3);
this.validateDot(dot4);
var minDot = this.getMinDot(dot1, dot2, dot3, dot4);
var maxDot = this.getMaxDot(dot1, dot2, dot3, dot4);
//Largo de la cadena para generar el hash
var range = this.getHashPrecision(minDot, maxDot);
//Se retornan los puntos visibles
return GeoHash.bboxes(minDot.latitude, minDot.longitude, maxDot.latitude, maxDot.longitude, range);
}
catch (err) {
throw new hunter_news_exceptions_1.CommonException('Se produjo un error inesperado al momento de calcular los GeoHash de la zona visible.', err);
}
};
/** Obtiene la precisión del hash */
LatLongEngine.prototype.getHashPrecisionByDots = function (dot1, dot2, dot3, dot4) {
try {
this.validateDot(dot1);
this.validateDot(dot2);
this.validateDot(dot3);
this.validateDot(dot4);
var minDot = this.getMinDot(dot1, dot2, dot3, dot4);
var maxDot = this.getMaxDot(dot1, dot2, dot3, dot4);
return this.getHashPrecision(minDot, maxDot);
}
catch (err) {
var message = 'Se produjo un error inesperado al calcular la precisión del hash.';
message = message + ' dot1=' + JSON.stringify(dot1);
message = message + ' dot2=' + JSON.stringify(dot2);
message = message + ' dot3=' + JSON.stringify(dot3);
message = message + ' dot4=' + JSON.stringify(dot4);
throw new hunter_news_exceptions_1.CommonException(message, err);
}
};
/** Calcula la distancia entre dos puntos */
LatLongEngine.prototype.calculateDistance = function (start, end) {
return GeoLib.getDistance(start, end, 0, 1);
};
return LatLongEngine;
}());
exports.LatLongEngine = LatLongEngine;
//# sourceMappingURL=latlong.engine.js.map