Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var GeoPoint = require('geopoint'),
* `.longitude(inRadians)`: Return the point's longitude. By default, the longitude is in degrees, unless `inRadians` is `true`
* `.distanceTo(point, inKilometers)`: Calculate the distance to another `GeoPoint` instance. By default, the distance is calculated in miles, unless `inKilometers` is `true`
* `.boundingCoordinates(distance, radius, inKilometers)`: Calculates the bounding coordinates of `distance` from the point and returns an array with the SW and NE points of the bounding box . If `radius` is not provided, the radius of the Earth will be used. The distance is calculated in miles unless `inKilometers` is `true`
* `.isInBoundingBox(boundingBox)`: Returns boolean value if the point's latitude and longitude are within the `boundingBox` passed. Bounding box is an array with the SW and NE points of the bounding box as returned by `.boundingCoordinates` method.

## Static Methods

Expand Down
25 changes: 25 additions & 0 deletions geopoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,31 @@
}
return [new GeoPoint(minLat, minLon, true), new GeoPoint(maxLat, maxLon, true)];
};

/**
* Check if point is in bounding box
*
* @param {Array} boundingBox array containing SW and NE points of bounding box
* @return {Boolean} distance between points
*/
GeoPoint.prototype.isInBoundingBox = function(boundingBox) {
if (!(boundingBox &&
Array.isArray(boundingBox) &&
boundingBox.length === 2 &&
boundingBox[0] instanceof GeoPoint &&
boundingBox[1] instanceof GeoPoint)) {
throw new Error('Invalid boundingBox');
}
var boundingBoxSWGeoPoint = boundingBox[0],
boundingBoxNEGeoPoint = boundingBox[1];

if (this.longitude() >= boundingBoxSWGeoPoint.longitude() &&
this.longitude() <= boundingBoxNEGeoPoint.longitude() &&
this.latitude() >= boundingBoxSWGeoPoint.latitude() &&
this.latitude() <= boundingBoxNEGeoPoint.latitude())
return true;
return false;
};

/**
* Convert degrees to radians
Expand Down
46 changes: 46 additions & 0 deletions test/test.isinbounding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var assert = require('assert'),
GeoPoint = require('../'),
LAT_DEG = 40.689604,
LON_DEG = -74.04455,
SW_LAT_KM = 40.50973996113307,
SW_LON_KM = -74.28175887602288,
SW_DIST_KM = 28.30334049313065,
NE_LAT_KM = 40.86946803886694,
NE_LON_KM = -73.80734112397712,
NE_DIST_KM = 28.2651684254543,
BOUNDINGBOX = [new GeoPoint(SW_LAT_KM, SW_LON_KM), new GeoPoint(NE_LAT_KM, NE_LON_KM)];

describe('.isInBoundingBox(boundingBox)', function() {

it('should throw an error if boundingBox is not valid', function() {
var point = new GeoPoint(LAT_DEG, LON_DEG);
['foo', 0 / 0, void 0, -1, 0, [0, 0], [[0, 0], [0, 0]]].forEach(function(value) {
var error;
try {
point.isInBoundingBox(value);
} catch (e) {
error = e;
}
assert.ok(error instanceof Error);
assert.equal(error.message, 'Invalid boundingBox');
});
});

it('should return a boolean', function() {
var point = new GeoPoint(LAT_DEG, LON_DEG),
isInBoundingBox = point.isInBoundingBox(BOUNDINGBOX);
assert.equal(typeof isInBoundingBox, 'boolean');
assert.ok(isInBoundingBox === true || isInBoundingBox === false);
});

it('should check for proper working', function() {
var point = new GeoPoint(LAT_DEG, LON_DEG),
isInBoundingBox = point.isInBoundingBox(BOUNDINGBOX);
assert.equal(typeof isInBoundingBox, 'boolean');
assert.ok(isInBoundingBox === true || isInBoundingBox === false);
assert.equal(isInBoundingBox, true);
});



});