Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"MongoError: Can't extract geo keys from object" with Type : Point

Tags:

I try to prepare my database field for geocoding with this:

MyCollection._ensureIndex({'data.address.located':'2dsphere'}); 

But then this error comes:

MongoError: Can't extract geo keys from object, malformed geometry?: { type: "Point", coordinates: [ 32.4586858, -110.8571443 ] } 

I can not see whats wrong with this field ? Any idea ?

When I take a look to this it shows up this:

The following example stores a GeoJSON Point:  { loc: { type: "Point", coordinates: [ 40, 5 ] } } 
like image 307
TJR Avatar asked Aug 06 '14 00:08

TJR


2 Answers

The problem is that

[ 32.4586858, -110.8571443 ] 

is not a valid coordinate. The order should be longitude followed by latitude whereas that coordinate appears to be the reverse (judging by the fact that the valid latitude range is -90 to 90 and -110.8571443 is outside of that).

I think you meant:

{ type: "Point", coordinates: [ -110.8571443, 32.4586858 ] } 

or there was some other entry error.

like image 160
go-oleg Avatar answered Sep 27 '22 19:09

go-oleg


As mentioned by @go-oleg the problem is that range of coordinate are next:

  • Longitude: (+-) 180°
  • Latitude: (+-) 90°

and index expects coordinates to be in that bounds.

But if you are trying to apply index on already imported data and found out that coordinates are swapped, you'll probably want to swap it back rather than reimporting whole collection. For this purpose you can use next mongoshell script.

Assuming that you already have well-formed GeoJSON of type Point.

db.coll.find().forEach(function (e) {     // assuming that `geoJson` is our field containing `coordinates`     e.geoJson.coordinates = [ // Swapping Lat/Lon         e.geoJson.coordinates[1], // Longitude goes first         e.geoJson.coordinates[0] // Latitude goes last     ];     db.coll.save(e);     // Some `print(e.name)` can go here just to understand the progress }); 
like image 34
Paul T. Rawkeen Avatar answered Sep 27 '22 19:09

Paul T. Rawkeen