Skip to content

Added WithinPolygon to Query #438

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

Merged
merged 6 commits into from
Jun 20, 2017
Merged
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 .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/
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