Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb error : Can't extract geo keys from object, malformed geometry?

Tags:

mongodb

I get the following error with mongodb 2.4.3

Can't extract geo keys from object, malformed geometry?

{type: "Polygon",
coordinates: [
    [
        [
            103.8324334524412,
            1.284232321447769
        ],
        [
            103.8342325475588,
            1.284232321447769
        ],
        [
            103.8342325469261,
            1.282433678236006
        ],
        [
            103.8324334530738,
            1.282433678236006
        ]
    ]
]}

Can someone help me understand the problem? it looks like a valid geoJSON object. My index is of type 2dsphere.

The two steps i am running are :

collection.ensureIndex {'geometry' : "2dsphere"}, (error) =>
  # some error checking
  # and then
  collection.insert features, (error) =>
    # features is an array of geoJSON feature objects
    # {"type" : "Feature"
    #  "geometry" : <the Polygon object above>
      }

The insert query gives this error. The complete document i am trying to insert is:

{
    "type":"Feature",
    "geometry": {
        "type":"Polygon",
        "coordinates":[
           [
             [103.83243345244122,1.2842323214477689],
             [103.83423254755876,1.2842323214477689],
             [103.83423254692615,1.2824336782360055],
             [103.83243345307383,1.2824336782360055]
           ]
        ]
      },
    "properties":{"name" : "My location"}
  }
like image 973
apostopher Avatar asked Jun 21 '13 06:06

apostopher


2 Answers

The polygon object in geoJSON requires first point ([lon, lat]) to be same as last point. By making this change:

{type: "Polygon",
coordinates: [
    [
        [
            103.8324334524412,
            1.284232321447769
        ],
        [
            103.8342325475588,
            1.284232321447769
        ],
        [
            103.8342325469261,
            1.282433678236006
        ],
        [
            103.8324334530738,
            1.282433678236006
        ],
        [
            103.8324334524412,
            1.284232321447769
        ]
    ]
]}

The insert query works fine.

like image 198
apostopher Avatar answered Nov 14 '22 21:11

apostopher


In my case the issue was the same as above but the solution to that what worked for me is

point ([longitude, latitude]) 

I had just done the opposite like

point ([latitude, longitude])

That was pretty stupid of me, but really hard to figure out by the error Can't extract geo keys from object, malformed geometry?

But just incase someone like me find this please check this as well.

like image 3
PravinDodia Avatar answered Nov 14 '22 20:11

PravinDodia