Skip to content

withinPolygon support for Polygon object #4067

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 10 commits into from May 22, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
166 changes: 166 additions & 0 deletions spec/ParseGeoPoint.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,116 @@ describe('Parse.GeoPoint testing', () => {
}, done.fail);
});

it('supports withinPolygon Polygon object', (done) => {
const inbound = new Parse.GeoPoint(1.5, 1.5);
const onbound = new Parse.GeoPoint(10, 10);
const outbound = new Parse.GeoPoint(20, 20);
const obj1 = new Parse.Object('Polygon', {location: inbound});
const obj2 = new Parse.Object('Polygon', {location: onbound});
const obj3 = new Parse.Object('Polygon', {location: outbound});
const polygon = {
__type: 'Polygon',
coordinates: [
[0, 0],
[10, 0],
[10, 10],
[0, 10],
[0, 0]
]
}
Parse.Object.saveAll([obj1, obj2, obj3]).then(() => {
const where = {
location: {
$geoWithin: {
$polygon: polygon
}
}
};
return rp.post({
url: Parse.serverURL + '/classes/Polygon',
json: { where, '_method': 'GET' },
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Javascript-Key': Parse.javaScriptKey
}
});
}).then((resp) => {
expect(resp.results.length).toBe(2);
done();
}, done.fail);
});

it('invalid Polygon object withinPolygon', (done) => {
const point = new Parse.GeoPoint(1.5, 1.5);
const obj = new Parse.Object('Polygon', {location: point});
const polygon = {
__type: 'Polygon',
coordinates: [
[0, 0],
[10, 0],
]
}
obj.save().then(() => {
const where = {
location: {
$geoWithin: {
$polygon: polygon
}
}
};
return rp.post({
url: Parse.serverURL + '/classes/Polygon',
json: { where, '_method': 'GET' },
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Javascript-Key': Parse.javaScriptKey
}
});
}).then((resp) => {
fail(`no request should succeed: ${JSON.stringify(resp)}`);
done();
}).catch((err) => {
expect(err.error.code).toEqual(1);
Copy link
Member

@dplewis dplewis Sep 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error should be 107 or Parse.Error.INVALID_JSON

done();
});
});

it('out of bounds Polygon object withinPolygon', (done) => {
const point = new Parse.GeoPoint(1.5, 1.5);
const obj = new Parse.Object('Polygon', {location: point});
const polygon = {
__type: 'Polygon',
coordinates: [
[0, 0],
[181, 0],
[0, 10]
]
}
obj.save().then(() => {
const where = {
location: {
$geoWithin: {
$polygon: polygon
}
}
};
return rp.post({
url: Parse.serverURL + '/classes/Polygon',
json: { where, '_method': 'GET' },
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Javascript-Key': Parse.javaScriptKey
}
});
}).then((resp) => {
fail(`no request should succeed: ${JSON.stringify(resp)}`);
done();
}).catch((err) => {
expect(err.error.code).toEqual(1);
done();
});
});

it('invalid input withinPolygon', (done) => {
const point = new Parse.GeoPoint(1.5, 1.5);
const obj = new Parse.Object('Polygon', {location: point});
Expand Down Expand Up @@ -628,4 +738,60 @@ describe('Parse.GeoPoint testing', () => {
done();
});
});

it('support $geoIntersects queries', (done) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it does. Duplicate removed.

const polygon = {
__type: 'Polygon',
coordinates: [
[0, 0],
[10, 0],
[10, 10],
[0, 10],
[0, 0]
]
}
const obj = new Parse.Object('Polygon', { polygon });
obj.save().then(() => {
const pointInsidePolygon = new Parse.GeoPoint(5, 5);
const where = {
polygon: {
$geoIntersects: {
$point: pointInsidePolygon
}
}
};
return rp.post({
url: Parse.serverURL + '/classes/Polygon',
json: { where, '_method': 'GET' },
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Javascript-Key': Parse.javaScriptKey
}
});
}).then((resp) => {
equal(resp.results.length, 1);
const pointOutsidePolygon = new Parse.GeoPoint(20, 20);
const where = {
polygon: {
$geoIntersects: {
$point: pointOutsidePolygon
}
}
};
return rp.post({
url: Parse.serverURL + '/classes/Polygon',
json: { where, '_method': 'GET' },
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Javascript-Key': Parse.javaScriptKey
}
});
}).then((resp) => {
equal(resp.results.length, 0);
done();
}).catch((err) => {
console.log(err)
fail(err);
});
});
});
33 changes: 24 additions & 9 deletions src/Adapters/Storage/Mongo/MongoTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,19 +670,34 @@ function transformConstraint(constraint, inArray) {

case '$geoWithin': {
const polygon = constraint[key]['$polygon'];
if (!(polygon instanceof Array)) {
throw new Parse.Error(
Parse.Error.INVALID_JSON,
'bad $geoWithin value; $polygon should contain at least 3 GeoPoints'
);
}
if (polygon.length < 3) {
let points
if (typeof polygon === 'object' && polygon.__type === 'Polygon') {
if (!polygon.coordinates && polygon.coordinates.length < 3) {
throw new Parse.Error(
Parse.Error.INVALID_JSON,
'bad $geoWithin value; Polygon.coordinates should contain at least 3 lon/lat pairs'
);
}
points = polygon.coordinates
} else if (polygon instanceof Array) {
if (polygon.length < 3) {
throw new Parse.Error(
Parse.Error.INVALID_JSON,
'bad $geoWithin value; $polygon should contain at least 3 GeoPoints'
);
}
points = polygon;
} else {
throw new Parse.Error(
Parse.Error.INVALID_JSON,
'bad $geoWithin value; $polygon should contain at least 3 GeoPoints'
'bad $geoWithin value; $polygon should be Polygon object or Array of Parse.GeoPoint\'s'
);
}
const points = polygon.map((point) => {
points = points.map((point) => {
if (point instanceof Array && point.length === 2) {
Parse.GeoPoint._validate(point[1], point[0]);
return point;
}
if (!GeoPointCoder.isValidJSON(point)) {
throw new Parse.Error(Parse.Error.INVALID_JSON, 'bad $geoWithin value');
} else {
Expand Down
33 changes: 24 additions & 9 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,19 +413,34 @@ const buildWhereClause = ({ schema, query, index }) => {

if (fieldValue.$geoWithin && fieldValue.$geoWithin.$polygon) {
const polygon = fieldValue.$geoWithin.$polygon;
if (!(polygon instanceof Array)) {
throw new Parse.Error(
Parse.Error.INVALID_JSON,
'bad $geoWithin value; $polygon should contain at least 3 GeoPoints'
);
}
if (polygon.length < 3) {
let points
if (typeof polygon === 'object' && polygon.__type === 'Polygon') {
if (!polygon.coordinates && polygon.coordinates.length < 3) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!polygon.coordinates || polygon.coordinates.length < 3

Using OR here instead of AND passes test. You might want to do the same with mongo and check your logic

throw new Parse.Error(
Parse.Error.INVALID_JSON,
'bad $geoWithin value; Polygon.coordinates should contain at least 3 lon/lat pairs'
);
}
points = polygon.coordinates;
} else if ((polygon instanceof Array)) {
if (polygon.length < 3) {
throw new Parse.Error(
Parse.Error.INVALID_JSON,
'bad $geoWithin value; $polygon should contain at least 3 GeoPoints'
);
}
points = polygon;
} else {
throw new Parse.Error(
Parse.Error.INVALID_JSON,
'bad $geoWithin value; $polygon should contain at least 3 GeoPoints'
'bad $geoWithin value; $polygon should be Polygon object or Array of Parse.GeoPoint\'s'
);
}
const points = polygon.map((point) => {
points = points.map((point) => {
if (point instanceof Array && point.length === 2) {
Parse.GeoPoint._validate(point[1], point[0]);
return `(${point[0]}, ${point[1]})`;
}
if (typeof point !== 'object' || point.__type !== 'GeoPoint') {
throw new Parse.Error(Parse.Error.INVALID_JSON, 'bad $geoWithin value');
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/vendor/mongodbUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
hostEnd = i;
break;
case 64: // '@'
// At this point, either we have an explicit point where the
// auth portion cannot go past, or the last @ char is the decider.
// At this point, either we have an explicit point where the
// auth portion cannot go past, or the last @ char is the decider.
atSign = i;
nonHost = -1;
break;
Expand Down