2013-06-24 23:38:50 +00:00
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
/*!
|
2013-07-23 18:16:43 +00:00
|
|
|
* Get a near filter from a given where object. For connector use only.
|
2013-06-24 23:38:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
exports.nearFilter = function nearFilter(where) {
|
|
|
|
var result = false;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
if (where && typeof where === 'object') {
|
2013-06-24 23:38:50 +00:00
|
|
|
Object.keys(where).forEach(function (key) {
|
|
|
|
var ex = where[key];
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
if (ex && ex.near) {
|
2013-06-24 23:38:50 +00:00
|
|
|
result = {
|
|
|
|
near: ex.near,
|
|
|
|
maxDistance: ex.maxDistance,
|
|
|
|
key: key
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2013-06-24 23:38:50 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Filter a set of objects using the given `nearFilter`.
|
|
|
|
*/
|
|
|
|
|
2013-06-24 22:21:59 +00:00
|
|
|
exports.filter = function (arr, filter) {
|
|
|
|
var origin = filter.near;
|
|
|
|
var max = filter.maxDistance > 0 ? filter.maxDistance : false;
|
|
|
|
var key = filter.key;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2013-06-24 22:21:59 +00:00
|
|
|
// create distance index
|
|
|
|
var distances = {};
|
|
|
|
var result = [];
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2013-06-24 22:21:59 +00:00
|
|
|
arr.forEach(function (obj) {
|
|
|
|
var loc = obj[key];
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2013-06-24 22:21:59 +00:00
|
|
|
// filter out objects without locations
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!loc) return;
|
|
|
|
|
|
|
|
if (!(loc instanceof GeoPoint)) {
|
2013-06-26 03:31:00 +00:00
|
|
|
loc = GeoPoint(loc);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
if (typeof loc.lat !== 'number') return;
|
|
|
|
if (typeof loc.lng !== 'number') return;
|
|
|
|
|
2013-06-24 23:38:50 +00:00
|
|
|
var d = GeoPoint.distanceBetween(origin, loc);
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
if (max && d > max) {
|
2013-06-24 22:21:59 +00:00
|
|
|
// dont add
|
|
|
|
} else {
|
|
|
|
distances[obj.id] = d;
|
|
|
|
result.push(obj);
|
|
|
|
}
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2013-06-24 22:21:59 +00:00
|
|
|
return result.sort(function (objA, objB) {
|
|
|
|
var a = objB[key];
|
|
|
|
var b = objB[key];
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
if (a && b) {
|
2013-06-24 22:21:59 +00:00
|
|
|
var da = distances[objA.id];
|
|
|
|
var db = distances[objB.id];
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
if (db === da) return 0;
|
2013-06-24 23:38:50 +00:00
|
|
|
return da > db ? 1 : -1;
|
2013-06-24 22:21:59 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-06-24 23:38:50 +00:00
|
|
|
exports.GeoPoint = GeoPoint;
|
|
|
|
|
2014-03-12 23:28:46 +00:00
|
|
|
/**
|
|
|
|
* The GeoPoint object represents a physical location.
|
|
|
|
*
|
|
|
|
* For example:
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* var here = new GeoPoint({lat: 10.32424, lng: 5.84978});
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Embed a latitude / longitude point in a model.
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* var CoffeeShop = loopback.createModel('coffee-shop', {
|
|
|
|
* location: 'GeoPoint'
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* You can query LoopBack models with a GeoPoint property and an attached data source using geo-spatial filters and
|
|
|
|
* sorting. For example, the following code finds the three nearest coffee shops.
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* CoffeeShop.attachTo(oracle);
|
|
|
|
* var here = new GeoPoint({lat: 10.32424, lng: 5.84978});
|
|
|
|
* CoffeeShop.find( {where: {location: {near: here}}, limit:3}, function(err, nearbyShops) {
|
|
|
|
* console.info(nearbyShops); // [CoffeeShop, ...]
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
* @class GeoPoint
|
|
|
|
* @param {Object} latlong Object with two Number properties: lat and long.
|
|
|
|
* @prop {Number} lat
|
|
|
|
* @prop {Number} lng
|
|
|
|
*/
|
|
|
|
|
2013-06-24 23:38:50 +00:00
|
|
|
function GeoPoint(data) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!(this instanceof GeoPoint)) {
|
2013-06-24 23:38:50 +00:00
|
|
|
return new GeoPoint(data);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
if (typeof data === 'string') {
|
2013-06-26 03:31:00 +00:00
|
|
|
data = data.split(/,\s*/);
|
|
|
|
assert(data.length === 2, 'must provide a string "lng,lat" creating a GeoPoint with a string');
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
if (Array.isArray(data)) {
|
2013-06-26 03:31:00 +00:00
|
|
|
data = {
|
|
|
|
lng: Number(data[0]),
|
|
|
|
lat: Number(data[1])
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
data.lng = Number(data.lng);
|
|
|
|
data.lat = Number(data.lat);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2013-06-24 23:38:50 +00:00
|
|
|
assert(typeof data === 'object', 'must provide a lat and lng object when creating a GeoPoint');
|
2013-12-14 17:49:11 +00:00
|
|
|
assert(typeof data.lat === 'number' && !isNaN(data.lat), 'lat must be a number when creating a GeoPoint');
|
|
|
|
assert(typeof data.lng === 'number' && !isNaN(data.lng), 'lng must be a number when creating a GeoPoint');
|
2013-06-24 23:38:50 +00:00
|
|
|
assert(data.lng <= 180, 'lng must be <= 180');
|
|
|
|
assert(data.lng >= -180, 'lng must be >= -180');
|
|
|
|
assert(data.lat <= 90, 'lat must be <= 90');
|
|
|
|
assert(data.lat >= -90, 'lat must be >= -90');
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2014-05-14 23:34:24 +00:00
|
|
|
/**
|
|
|
|
* @property {Number} lat The latitude point in degrees. Range: -90 to 90.
|
|
|
|
*/
|
2013-06-24 23:38:50 +00:00
|
|
|
this.lat = data.lat;
|
2014-05-14 23:34:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Number} lng The longitude point in degrees. Range: -90 to 90.
|
|
|
|
*/
|
2013-06-24 23:38:50 +00:00
|
|
|
this.lng = data.lng;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* Determine the spherical distance between two GeoPoints.
|
|
|
|
* Specify units of measurement with the 'type' property in options object. Type can be:
|
|
|
|
* - `miles` (default)
|
|
|
|
* - `radians`
|
|
|
|
* - `kilometers`
|
|
|
|
* - `meters`
|
|
|
|
* - `miles`
|
|
|
|
* - `feet`
|
|
|
|
* - `degrees`
|
|
|
|
* @param {GeoPoint} pointA Point A
|
|
|
|
* @param {GeoPoint} pointB Point B
|
|
|
|
* @param {Object} options Options object; has one key, 'type', to specify the units of measurment (see above). Default is miles.
|
2013-06-24 23:38:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
GeoPoint.distanceBetween = function distanceBetween(a, b, options) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!(a instanceof GeoPoint)) {
|
2013-06-26 03:31:00 +00:00
|
|
|
a = GeoPoint(a);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!(b instanceof GeoPoint)) {
|
2013-06-26 03:31:00 +00:00
|
|
|
b = GeoPoint(b);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2013-06-24 23:38:50 +00:00
|
|
|
var x1 = a.lat;
|
|
|
|
var y1 = a.lng;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2013-06-24 23:38:50 +00:00
|
|
|
var x2 = b.lat;
|
|
|
|
var y2 = b.lng;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2013-06-24 23:38:50 +00:00
|
|
|
return geoDistance(x1, y1, x2, y2, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the spherical distance to the given point.
|
2014-03-12 23:28:46 +00:00
|
|
|
* Example:
|
|
|
|
* ```js
|
|
|
|
* var here = new GeoPoint({lat: 10, lng: 10});
|
|
|
|
* var there = new GeoPoint({lat: 5, lng: 5});
|
|
|
|
* GeoPoint.distanceBetween(here, there, {type: 'miles'}) // 438
|
|
|
|
* ```
|
|
|
|
* @param {Object} point GeoPoint object to which to measure distance.
|
|
|
|
* @param {Object} options Use type key to specify units of measurment (default is miles).
|
2013-06-24 23:38:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
GeoPoint.prototype.distanceTo = function (point, options) {
|
|
|
|
return GeoPoint.distanceBetween(this, point, options);
|
|
|
|
}
|
|
|
|
|
2013-06-26 03:31:00 +00:00
|
|
|
/**
|
|
|
|
* Simple serialization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
GeoPoint.prototype.toString = function () {
|
|
|
|
return this.lng + ',' + this.lat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* @property {Number} PI - Ratio of a circle's circumference to its diameter.
|
|
|
|
* @property {Number} DEG2RAD - Factor to convert degrees to radians.
|
|
|
|
* @property {Number} RAD2DEG - Factor to convert radians to degrees.
|
|
|
|
* @property {Object} EARTH_RADIUS - Radius of the earth.
|
|
|
|
*/
|
2013-06-26 03:31:00 +00:00
|
|
|
|
2013-06-24 23:38:50 +00:00
|
|
|
// ratio of a circle's circumference to its diameter
|
|
|
|
var PI = 3.1415926535897932384626433832795;
|
|
|
|
|
2014-03-12 23:28:46 +00:00
|
|
|
// factor to convert degrees to radians
|
2014-01-24 17:09:53 +00:00
|
|
|
var DEG2RAD = 0.01745329252;
|
2013-06-24 23:38:50 +00:00
|
|
|
|
2014-03-12 23:28:46 +00:00
|
|
|
// factor to convert radians degrees to degrees
|
2013-06-24 23:38:50 +00:00
|
|
|
var RAD2DEG = 57.29577951308;
|
|
|
|
|
|
|
|
// radius of the earth
|
|
|
|
var EARTH_RADIUS = {
|
|
|
|
kilometers: 6370.99056,
|
|
|
|
meters: 6370990.56,
|
|
|
|
miles: 3958.75,
|
|
|
|
feet: 20902200,
|
|
|
|
radians: 1,
|
|
|
|
degrees: RAD2DEG
|
|
|
|
};
|
|
|
|
|
|
|
|
function geoDistance(x1, y1, x2, y2, options) {
|
|
|
|
// Convert to radians
|
|
|
|
x1 = x1 * DEG2RAD;
|
|
|
|
y1 = y1 * DEG2RAD;
|
|
|
|
x2 = x2 * DEG2RAD;
|
|
|
|
y2 = y2 * DEG2RAD;
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
var a = Math.pow(Math.sin(( y2 - y1 ) / 2.0), 2);
|
|
|
|
var b = Math.pow(Math.sin(( x2 - x1 ) / 2.0), 2);
|
|
|
|
var c = Math.sqrt(a + Math.cos(y2) * Math.cos(y1) * b);
|
2013-06-24 23:38:50 +00:00
|
|
|
|
|
|
|
var type = (options && options.type) || 'miles';
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
return 2 * Math.asin(c) * EARTH_RADIUS[type];
|
2013-06-26 03:31:00 +00:00
|
|
|
}
|
|
|
|
|