Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for querying within a polygon #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 examples/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const qs = new MongoQS({
custom: {
bbox: 'geojson',
near: 'geojson',
within: 'geojson',
},
});

Expand Down
28 changes: 28 additions & 0 deletions examples/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,34 @@ describe('Example App', () => {
.end(done);
});

it('returns places inside polygon', (done) => {
const polygon = [
5.9490966796875,
61.025705069868856,
5.987548828125,
60.994423108456154,
6.10565185546875,
60.98376689595989,
6.1962890625,
61.016390262027116,
6.115264892578125,
61.04964487995011,
5.969696044921875,
61.0516390483921,
5.9490966796875,
61.025705069868856,
].join(',');

app.get(`${url}?within=${polygon}`)
.expect(200)
.expect((res) => {
assert.equal(res.body.length, 2);
assert.equal(res.body[0].name, 'Solrenningen');
assert.equal(res.body[1].name, 'Norddalshytten');
})
.end(done);
});

it('returns places with any of the following tags', (done) => {
app.get(`${url}?tags[]=Båt&tags[]=Stekeovn`)
.expect(200)
Expand Down
74 changes: 73 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ module.exports = function MongoQS(options) {
this.custom.near = this.customNear(this.custom.near);
}

if (this.custom.within) {
this.custom.within = this.customWithin(this.custom.within);
}

if (this.custom.after) {
this.custom.after = this.customAfter(this.custom.after);
}
Expand Down Expand Up @@ -100,6 +104,74 @@ module.exports.prototype.customNear = field => (query, point) => {
}
};

module.exports.prototype.customWithin = field => (query, pointList) => {
const pointStrArr = pointList.split(',');

//need at least four points to make a polygon
if(pointStrArr.length < 8) {
return {};
}

//ensure even amount of points
if(pointStrArr.length % 2 !== 0) {
return {};
}

//convert every string to a float
var pointNumArr = [];

try {
pointStrArr.forEach(str => {
var num = parseFloat(str, 10);

//if float conversion fails from bad input, return empty
if(isNaN(num)) {
throw new Error('Invalid value passed as polygon coordinate')
}

pointNumArr.push(num);
})
} catch (err) {
return {}
}

//build our coordinates array
var coordinates = [];

try {
for(var i = 0 ; i < pointNumArr.length ; i = i + 2) {
//should never happen from previous check
if(i+1 >= pointNumArr.length) {
throw new Error("Odd number of points given")
}

coordinates.push([pointNumArr[i], pointNumArr[i+1]]);
}
} catch (err) {
return {}
}

// check if last coordinate matches first, if not go ahead an add first into list
var lastIndex = coordinates.length-1;
var firstLon = coordinates[0][0]
var firstLat = coordinates[0][1]
var lastLon = coordinates[lastIndex][0]
var lastLat = coordinates[lastIndex][1]

if (firstLon !== lastLon || firstLat !== lastLat) {
return {}
}

query[field] = {
$geoWithin: {
$geometry : {
type: 'Polygon',
coordinates: [coordinates]
}
}
}
};

function parseDate(value) {
let date = value;

Expand Down Expand Up @@ -192,7 +264,7 @@ module.exports.prototype.parseString = function parseString(string, array) {
break;
default:
break;
}
}
break;
default:
ret.org = org = op + org;
Expand Down
78 changes: 78 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,45 @@ describe('customBBOX()', () => {
});
});

describe('customWithin()', () => {
it('does not return $geoWithin query for invalid polygon', () => {
[
// non matching first last
'1,1,2,2,2,3,1,2,2,2',
// not enough points
'1,1,2,2,2,3',
// odd length
'1,1,2,2,2,3,1',
// invalid value
'1,1,2,2,2,3,1,2,2,a',
].forEach((polygon) => {
mqs.customWithin('gojson')(query, polygon);
assert.deepEqual(query, {});
});
});

it('returns $geoWithin query for valid polygon', () => {
const string = '1,1,2,2,2,3,1,2,1,1';
mqs.customWithin('geojson')(query, string);
assert.deepEqual(query, {
geojson: {
$geoWithin: {
$geometry: {
type: 'Polygon',
coordinates: [[
[1, 1],
[2, 2],
[2, 3],
[1, 2],
[1, 1],
]],
},
},
},
});
});
});

describe('customNear()', () => {
it('does not return $near query for invalid point', () => {
['0123', '0,'].forEach((bbox) => {
Expand Down Expand Up @@ -107,6 +146,45 @@ describe('customNear()', () => {
});
});

describe('customWithin()', () => {
it('does not return $geoWithin query for invalid polygon', () => {
[
// non matching first last
'1,1,2,2,2,3,1,2,2,2',
// not enough points
'1,1,2,2,2,3',
// odd length
'1,1,2,2,2,3,1',
// invalid value
'1,1,2,2,2,3,1,2,2,a',
].forEach((polygon) => {
mqs.customWithin('gojson')(query, polygon);
assert.deepEqual(query, {});
});
});

it('returns $geoWithin query for valid polygon', () => {
const string = '1,1,2,2,2,3,1,2,1,1';
mqs.customWithin('geojson')(query, string);
assert.deepEqual(query, {
geojson: {
$geoWithin: {
$geometry: {
type: 'Polygon',
coordinates: [[
[1, 1],
[2, 2],
[2, 3],
[1, 2],
[1, 1],
]],
},
},
},
});
});
});

describe('customAfter()', () => {
it('does not return after query for invalid date', () => {
['foo', '2015-13-40'].forEach((date) => {
Expand Down