Skip to content

Mongo: Fix reversing polygon coordinates #4609

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 5 commits into from
Mar 10, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 5 additions & 2 deletions spec/MongoTransform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,16 @@ describe('parseObjectToMongoObjectForCreate', () => {
});

it('polygon', (done) => {
const input = {location: { type: 'Polygon', coordinates: [[[45, -45],[45, -45]]]}};
const lat = -45;
const lng = 45;
// Mongo stores polygon in WGS84 lng/lat
const input = {location: { type: 'Polygon', coordinates: [[[lng, lat],[lng, lat]]]}};
Copy link
Contributor

Choose a reason for hiding this comment

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

this is against the documentation for the GeoPoints,m where the list of corrdinates should be provided as lat,lng pairs, not lng,lat pairs.

http://parseplatform.org/Parse-SDK-JS/api/v1.11.1/Parse.GeoPoint.html

If the storage need to be reveresed, that's another issue but should not for something un natural for the user.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wrote it this way to highlight that the input and output are going to be reversed.

The current GeoPoint transform tests are slightly confusing at a glance.

Shouldn't this be testing mongoObjectToParseObject input -> lng/lat output -> lat/lng?

Copy link
Contributor

Choose a reason for hiding this comment

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

OK I misread. Do you have a test that ensures the input / output orders are preserved? ie the mongo transformation is properly applied and reversed?

const output = transform.mongoObjectToParseObject(null, input, {
fields: { location: { type: 'Polygon' }},
});
expect(typeof output.location).toEqual('object');
expect(output.location).toEqual(
{__type: 'Polygon', coordinates: [[45, -45],[45, -45]]}
{__type: 'Polygon', coordinates: [[lat, lng],[lat, lng]]}
);
done();
});
Expand Down
58 changes: 58 additions & 0 deletions spec/ParsePolygon.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,64 @@ describe('Parse.Polygon testing', () => {
}, done.fail);
});

it('polygonContain query no reverse input (Regression test for #4608)', (done) => {
const points1 = [[.25,0],[.25,1.25],[.75,1.25],[.75,0]];
const points2 = [[0,0],[0,2],[2,2],[2,0]];
const points3 = [[10,10],[10,15],[15,15],[15,10],[10,10]];
const polygon1 = new Parse.Polygon(points1);
const polygon2 = new Parse.Polygon(points2);
const polygon3 = new Parse.Polygon(points3);
const obj1 = new TestObject({location: polygon1});
const obj2 = new TestObject({location: polygon2});
const obj3 = new TestObject({location: polygon3});
Parse.Object.saveAll([obj1, obj2, obj3]).then(() => {
const where = {
location: {
$geoIntersects: {
$point: { __type: 'GeoPoint', latitude: 0.5, longitude:1.0 }
}
}
};
return rp.post({
url: Parse.serverURL + '/classes/TestObject',
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('polygonContain query real data (Regression test for #4608)', (done) => {
const detroit = [[42.631655189280224,-83.78406753121705],[42.633047793854814,-83.75333640366955],[42.61625254348911,-83.75149921669944],[42.61526926650296,-83.78161794858735],[42.631655189280224,-83.78406753121705]];
const polygon = new Parse.Polygon(detroit);
const obj = new TestObject({location: polygon});
obj.save().then(() => {
const where = {
location: {
$geoIntersects: {
$point: { __type: 'GeoPoint', latitude: 42.624599, longitude:-83.770162 }
}
}
};
return rp.post({
url: Parse.serverURL + '/classes/TestObject',
json: { where, '_method': 'GET' },
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Javascript-Key': Parse.javaScriptKey
}
});
}).then((resp) => {
expect(resp.results.length).toBe(1);
done();
}, done.fail);
});

it('polygonContain invalid input', (done) => {
const points = [[0,0],[0,1],[1,1],[1,0]];
const polygon = new Parse.Polygon(points);
Expand Down
13 changes: 11 additions & 2 deletions src/Adapters/Storage/Mongo/MongoTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -1261,9 +1261,13 @@ var GeoPointCoder = {

var PolygonCoder = {
databaseToJSON(object) {
// Convert lng/lat -> lat/lng
const coords = object.coordinates[0].map((coord) => {
return [coord[1], coord[0]];
});
return {
__type: 'Polygon',
coordinates: object['coordinates'][0]
coordinates: coords
}
},

Expand All @@ -1283,7 +1287,8 @@ var PolygonCoder = {
},

JSONToDatabase(json) {
const coords = json.coordinates;
let coords = json.coordinates;
// Add first point to the end to close polygon
if (coords[0][0] !== coords[coords.length - 1][0] ||
coords[0][1] !== coords[coords.length - 1][1]) {
coords.push(coords[0]);
Expand All @@ -1306,6 +1311,10 @@ var PolygonCoder = {
'GeoJSON: Loop must have at least 3 different vertices'
);
}
// Convert lat/long -> long/lat
coords = coords.map((coord) => {
return [coord[1], coord[0]];
});
return { type: 'Polygon', coordinates: [coords] };
},

Expand Down