Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a Mongo GeoJSON 2dsphere index on LoopbackJs' attribute GeoPoint

As the title suggests i tried to add a 2dsphere index on my GeoPoints attributes on LoopbackJs. My MongoDB shell version is 3.2.3 - so it should do.

Here are my tries so far:

  • Adding enableGeoIndexing in my server/datasource.js

          {
    ...
    "myDs": {
            "host": "localhost",
            "port": 27017,
            "database": "myDB",
            "name": "myDs",
            "connector": "mongodb",
            "enableGeoIndexing": true
          }
    

    ... }

Nothing seemed to change.

  • Adding indexes the Loopback way + having an autoupdate script:

    { "name": "NsUser", "base": "User", "idInjection": true, "options": { "validateUpsert": true }, "indexes": { "geopoint_index": { "geopoint": "2dsphere" } }, "properties": { "created": { "type": "date" }, "firstname": { "type": "string" }, "lastname": { "type": "string" }, "geopoint": { "type": "geopoint" }, ... } … }

And I'm getting an error :

"ok" : 0,
    "errmsg" : "Can't extract geo keys: { _id: ObjectId('5807689f01723b3ca6ba08e5'), created: new Date(1426545369000), email: \"[email protected]\", firstname: \"Louis\", lastname: \"L\", geopoint: { lat: -17.52243049, lng: -149.54396636 } }  can't project geometry into spherical CRS: { lat: -17.52243049, lng: -149.54396636 }",
    "code" : 16755
like image 203
F3L1X79 Avatar asked Oct 26 '16 08:10

F3L1X79


People also ask

What is 2dsphere index in MongoDB?

A 2dsphere index supports queries that calculate geometries on an earth-like sphere. 2dsphere index supports all MongoDB geospatial queries: queries for inclusion, intersection and proximity. For more information on geospatial queries, see Geospatial Queries.

What is 2d sphere?

a 2-sphere is an ordinary 2-dimensional sphere in 3-dimensional Euclidean space, and is the boundary of an ordinary ball (3-ball). a 3-sphere is a 3-dimensional sphere in 4-dimensional Euclidean space.


1 Answers

The only solution I found to add the 2dsphere index is to add a new attribute geopoint_mongo which is an array [lng, lat] .

Here's the script I used to convert the datas:

NsUser.createMongoGeoloc = function (cb) {
        NsUser.find(function (err, users) {
            var count = 0;
            users.forEach(function (user) {
                var change = false;
                if (user.geopoint && user.geopoint.lat && user.geopoint.lng) {
                    count++;
                    user.geopoint_mongo = [user.geopoint.lng, user.geopoint.lat];
                    change = true;
                }
                if (change) {
                    NsUser.upsert(user);
                }
            });
            cb(err, count);
        });
    };

    NsUser.remoteMethod(
            'createMongoGeoloc',
            {
                description: 'Update the Geolocation for NsUser',
                returns: {arg: 'count', type: 'number'},
                http: {verb: 'put'}
            }
    );

Then adding the mongo index did the trick:

db.NsUser.createIndex({geopoint_mongo:"2dsphere"})

But as you can see it does not really answer the question - it is just a workaround.

like image 154
F3L1X79 Avatar answered Oct 07 '22 05:10

F3L1X79