From 6bbae3623d1db1ce07bdda6eb1caf75e2f61a134 Mon Sep 17 00:00:00 2001 From: Felix Fang Date: Sat, 20 Feb 2016 08:05:04 +0000 Subject: [PATCH 1/2] This change allows GeoPointCoder to serialize to GeoJson as opposed to legacy coordinates. GeoPointCoder can now deserialize both legacy coordinates and GeoJson and legacy coordinates for backward compatibility. In addition, '2dsphere' instead of '2d' is used for spatial indexing so that GeoJson can be indexed. --- src/ExportAdapter.js | 2 +- src/transform.js | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/ExportAdapter.js b/src/ExportAdapter.js index abcc862dcf..5b460e4842 100644 --- a/src/ExportAdapter.js +++ b/src/ExportAdapter.js @@ -479,7 +479,7 @@ ExportAdapter.prototype.smartFind = function(coll, where, options) { } var index = {}; - index[key] = '2d'; + index[key] = '2dsphere'; //TODO: condiser moving index creation logic into Schema.js return coll.createIndex(index).then(() => { // Retry, but just once. diff --git a/src/transform.js b/src/transform.js index 0e99b48836..a92ad4a909 100644 --- a/src/transform.js +++ b/src/transform.js @@ -748,21 +748,41 @@ var BytesCoder = { var GeoPointCoder = { databaseToJSON(object) { - return { - __type: 'GeoPoint', - latitude: object[1], - longitude: object[0] + if (GeoPointCoder.isValidGeoJson(object)) { + return { + __type: 'GeoPoint', + latitude: object['coordinates'][1], + longitude: object['coordinates'][0] + } + } else if (GeoPointCoder.isValidLegacyCoordinates(object)) { + return { + __type: 'GeoPoint', + latitude: object[1], + longitude: object[0] + } + } else { + throw 'invalid database object'; } }, + isValidGeoJson(object) { + return (object instanceof Object + && object['type'] == 'Point' + && 'coordinates' in object + && GeoPointCoder.isValidLegacyCoordinates(object['coordinates'])); + }, + + isValidLegacyCoordinates(object) { + return (object instanceof Array + && object.length == 2); + }, + isValidDatabaseObject(object) { - return (object instanceof Array && - object.length == 2 - ); + return GeoPointCoder.isValidGeoJson(object) || GeoPointCoder.isValidLegacyCoordinates(object); }, JSONToDatabase(json) { - return [ json.longitude, json.latitude ]; + return { type: 'Point', coordinates: [ json.longitude, json.latitude ] }; }, isValidJSON(value) { From deaa6c534731328a69e45b65be975b26685e63cc Mon Sep 17 00:00:00 2001 From: Felix Fang Date: Sat, 20 Feb 2016 08:26:45 +0000 Subject: [PATCH 2/2] fixed broken test by GeoPointCoder --- spec/transform.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/transform.spec.js b/spec/transform.spec.js index c7780ffbd2..91d074a5e6 100644 --- a/spec/transform.spec.js +++ b/spec/transform.spec.js @@ -66,7 +66,7 @@ describe('transformCreate', () => { it('plain', (done) => { var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180}; var out = transform.transformCreate(dummySchema, null, {location: geoPoint}); - expect(out.location).toEqual([180, -180]); + expect(out.location).toEqual({ type: 'Point', coordinates: [ 180, -180 ] }); done(); });