Skip to content

Serialize GeoPoint to GeoJson #524

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

Closed
wants to merge 7 commits into from
2 changes: 1 addition & 1 deletion spec/transform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down
2 changes: 1 addition & 1 deletion src/ExportAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 28 additions & 8 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,21 +747,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) {
Expand Down