Skip to content

Commit

Permalink
Added WithinPolygon to Query (parse-community#438)
Browse files Browse the repository at this point in the history
* Added withinPolygon to query

* test

* more test and documentation

* reused objects for test

* clean up test
  • Loading branch information
dplewis authored and flovilmart committed Jun 20, 2017
1 parent 8126508 commit 1b26adc
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ lib
logs
node_modules
test_output
integration/test_logs
*~
.DS_Store
.idea/
Expand Down
2 changes: 1 addition & 1 deletion integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"dependencies": {
"express": "^4.13.4",
"mocha": "^2.4.5",
"parse-server": "2.4.2"
"parse-server": "^2.4.2"
},
"scripts": {
"test": "mocha --reporter dot -t 5000"
Expand Down
57 changes: 57 additions & 0 deletions integration/test/ParseGeoPointTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,61 @@ describe('Geo Point', () => {
done();
});
});

it('supports withinPolygon open path', (done) => {
const points = [
new Parse.GeoPoint(37.85, -122.33),
new Parse.GeoPoint(37.85, -122.90),
new Parse.GeoPoint(37.68, -122.90),
new Parse.GeoPoint(37.68, -122.33)
];
const query = new Parse.Query(TestPoint);
query.withinPolygon('location', points);
return query.find().then((results) => {
assert.equal(results.length, 1);
done();
});
});

it('supports withinPolygon closed path', (done) => {
const points = [
new Parse.GeoPoint(38.52, -121.50),
new Parse.GeoPoint(37.75, -157.93),
new Parse.GeoPoint(37.578072, -121.379914),
new Parse.GeoPoint(38.52, -121.50)
];
const query = new Parse.Query(TestPoint);
query.withinPolygon('location', points);
return query.find().then((results) => {
assert.equal(results.length, 2);
done();
});
});

it('non array withinPolygon', (done) => {
const query = new Parse.Query(TestPoint);
query.withinPolygon('location', 1234);
return query.find().fail((err) => {
assert.equal(err.code, Parse.Error.INVALID_JSON);
done();
});
});

it('invalid array withinPolygon', (done) => {
const query = new Parse.Query(TestPoint);
query.withinPolygon('location', [1234]);
return query.find().fail((err) => {
assert.equal(err.code, Parse.Error.INVALID_JSON);
done();
});
});

it('minimum 3 points withinPolygon', (done) => {
const query = new Parse.Query(TestPoint);
query.withinPolygon('location', []);
return query.find().fail((err) => {
assert.equal(err.code, Parse.Error.INTERNAL_SERVER_ERROR);
done();
});
});
});
20 changes: 18 additions & 2 deletions src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function handleSelectResult(data: any, select: Array<string>){
data[field] = undefined
} else if (hasSubObjectSelect) {
// this field references a sub-object,
// so we need to walk down the path components
// so we need to walk down the path components
let pathComponents = field.split(".");
var obj = data;
var serverMask = serverDataMask;
Expand Down Expand Up @@ -358,7 +358,7 @@ export default class ParseQuery {
if (select) {
handleSelectResult(data, select);
}

return ParseObject.fromJSON(data, !select);
});
})._thenRunCallbacks(options);
Expand Down Expand Up @@ -928,6 +928,22 @@ export default class ParseQuery {
return this;
}

/**
* Adds a constraint to the query that requires a particular key's
* coordinates be contained within and on the bounds of a given polygon.
* Supports closed and open (last point is connected to first) paths
*
* Polygon must have at least 3 points
*
* @method withinPolygon
* @param {String} key The key to be constrained.
* @param {Array} array of geopoints
* @return {Parse.Query} Returns the query, so you can chain this call.
*/
withinPolygon(key: string, points: Array): ParseQuery {
return this._addCondition(key, '$geoWithin', { '$polygon': points });
}

/** Query Orderings **/

/**
Expand Down

0 comments on commit 1b26adc

Please sign in to comment.