From 05917b6c501b61284e619ecc6fb61cba2c314517 Mon Sep 17 00:00:00 2001 From: Veri Ferdiansyah Date: Thu, 23 Mar 2017 11:41:40 +0700 Subject: [PATCH 1/3] Fix GeoPoint issues on PostgreSQL (#3285 & #3659) --- src/Adapters/Storage/Postgres/PostgresStorageAdapter.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index e35744a738..8313dfa0e6 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -325,8 +325,8 @@ const buildWhereClause = ({ schema, query, index }) => { const point = fieldValue.$nearSphere; const distance = fieldValue.$maxDistance; const distanceInKM = distance * 6371 * 1000; - patterns.push(`ST_distance_sphere($${index}:name::geometry, POINT($${index + 1}, $${index + 2})::geometry) <= $${index + 3}`); - sorts.push(`ST_distance_sphere($${index}:name::geometry, POINT($${index + 1}, $${index + 2})::geometry) ASC`) + patterns.push(`ST_distance_sphere($${index}:name::geometry, POINT($${index + 2}, $${index + 1})::geometry) <= $${index + 3}`); + sorts.push(`ST_distance_sphere($${index}:name::geometry, POINT($${index + 2}, $${index + 1})::geometry) ASC`) values.push(fieldName, point.longitude, point.latitude, distanceInKM); index += 4; } @@ -1071,8 +1071,8 @@ export class PostgresStorageAdapter { } if (object[fieldName] && schema.fields[fieldName].type === 'GeoPoint') { object[fieldName] = { - latitude: object[fieldName].y, - longitude: object[fieldName].x + latitude: object[fieldName].x, + longitude: object[fieldName].y } } if (object[fieldName] && schema.fields[fieldName].type === 'File') { From 651d78521e971fd0c18a9ebeec0db2e078f5c9ff Mon Sep 17 00:00:00 2001 From: Veri Ferdiansyah Date: Mon, 27 Mar 2017 08:32:05 +0700 Subject: [PATCH 2/3] Fix for failed tests --- .../Storage/Postgres/PostgresStorageAdapter.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 8313dfa0e6..b40a0819b7 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -325,21 +325,21 @@ const buildWhereClause = ({ schema, query, index }) => { const point = fieldValue.$nearSphere; const distance = fieldValue.$maxDistance; const distanceInKM = distance * 6371 * 1000; - patterns.push(`ST_distance_sphere($${index}:name::geometry, POINT($${index + 2}, $${index + 1})::geometry) <= $${index + 3}`); - sorts.push(`ST_distance_sphere($${index}:name::geometry, POINT($${index + 2}, $${index + 1})::geometry) ASC`) - values.push(fieldName, point.longitude, point.latitude, distanceInKM); + patterns.push(`ST_distance_sphere($${index}:name::geometry, POINT($${index + 1}, $${index + 2})::geometry) <= $${index + 3}`); + sorts.push(`ST_distance_sphere($${index}:name::geometry, POINT($${index + 1}, $${index + 2})::geometry) ASC`) + values.push(fieldName, point.latitude, point.longitude, distanceInKM); index += 4; } if (fieldValue.$within && fieldValue.$within.$box) { const box = fieldValue.$within.$box; - const left = box[0].longitude; const bottom = box[0].latitude; - const right = box[1].longitude; + const left = box[0].longitude; const top = box[1].latitude; + const right = box[1].longitude; patterns.push(`$${index}:name::point <@ $${index + 1}::box`); - values.push(fieldName, `((${left}, ${bottom}), (${right}, ${top}))`); + values.push(fieldName, `((${bottom}, ${left}), (${top}, ${right}))`); index += 2; } @@ -761,7 +761,7 @@ export class PostgresStorageAdapter { }); const geoPointsInjects = Object.keys(geoPoints).map((key) => { const value = geoPoints[key]; - valuesArray.push(value.longitude, value.latitude); + valuesArray.push(value.latitude, value.longitude); const l = valuesArray.length + columnsArray.length; return `POINT($${l}, $${l + 1})`; }); From 859fe26096b6ba5dff02e5fa17b9e0ec2f7f310e Mon Sep 17 00:00:00 2001 From: Veri Ferdiansyah Date: Fri, 7 Apr 2017 21:04:49 +0700 Subject: [PATCH 3/3] Add a near location test case --- spec/ParseGeoPoint.spec.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spec/ParseGeoPoint.spec.js b/spec/ParseGeoPoint.spec.js index 1dd7710ede..bb23011ecb 100644 --- a/spec/ParseGeoPoint.spec.js +++ b/spec/ParseGeoPoint.spec.js @@ -176,6 +176,21 @@ describe('Parse.GeoPoint testing', () => { Parse.Object.saveAll([sacramento, sf, honolulu], callback); }; + it('returns nearest location', (done) => { + makeSomeGeoPoints(function() { + var sfo = new Parse.GeoPoint(37.6189722, -122.3748889); + var query = new Parse.Query(TestObject); + query.near('location', sfo); + query.find({ + success: function(results) { + equal(results[0].get('name'), 'San Francisco'); + equal(results[1].get('name'), 'Sacramento'); + done(); + } + }); + }); + }); + it('geo max distance in km everywhere', (done) => { makeSomeGeoPoints(function() { var sfo = new Parse.GeoPoint(37.6189722, -122.3748889);