Skip to content

Commit

Permalink
Merge pull request #18 from Turistforeningen/feat/max-min-near
Browse files Browse the repository at this point in the history
Add support for max & min $near queries
  • Loading branch information
Starefossen committed Mar 28, 2016
2 parents c93d585 + 3dbebe6 commit 64d3005
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ useful when building an API and accepting various user specificed queries.
|-----------|---------------|--------------|
| bbox | `?bbox=0,1,2,3` | `{ geojson: { $geoWithin: { $geometry: { … } } } }` |
| near | `?near=0,1` | `{ geojson: { $near: { $geometry: { … } } } }` |
| near (max distance) | `?near=0,1,2` | `{ geojson: { $near: { …, $maxDistance: 2 } } }` |
| near (max & min distance) | `?near=0,1,2,3` | `{ geojson: { $near: { …, $minDistance: 3 } } }` |

* Custom query functions
* `after` (date)
Expand Down
21 changes: 21 additions & 0 deletions examples/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ describe('Example App', function() {
.end(done);
});

it('returns places near point with max distance', function(done) {
app.get(url + '?near=6.13037,61.00607,7000')
.expect(200)
.expect(function(res) {
assert.equal(res.body.length, 2);
assert.equal(res.body[0].name, 'Solrenningen');
assert.equal(res.body[1].name, 'Åsedalen');
})
.end(done);
});

it('returns places near point with max and min distance', function(done) {
app.get(url + '?near=6.13037,61.00607,7000,1000')
.expect(200)
.expect(function(res) {
assert.equal(res.body.length, 1);
assert.equal(res.body[0].name, 'Åsedalen');
})
.end(done);
});

it('returns places inside bbox', function(done) {
var bbox = [
'5.5419158935546875',
Expand Down
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ module.exports.prototype.customNear = function(field) {
return function(query, point) {
point = point.split(',');

if (point.length === 2) {
if (point.length >= 2) {
point[0] = parseFloat(point[0], 10);
point[1] = parseFloat(point[1], 10);

if (!isNaN(point.reduce(function(a,b){return a+b;}))) {
var max = parseInt(point[2], 10);
var min = parseInt(point[3], 10);

query[field] = {
$near: {
$geometry: {
Expand All @@ -82,6 +85,14 @@ module.exports.prototype.customNear = function(field) {
},
},
};

if (!isNaN(max)) {
query[field].$near.$maxDistance = parseInt(point[2], 10);

if (!isNaN(min)) {
query[field].$near.$minDistance = parseInt(point[3], 10);
}
}
}
}
};
Expand Down

0 comments on commit 64d3005

Please sign in to comment.