简体   繁体   中英

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

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"}
  }

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.

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.

I had the same problem and my longitude, latitude order was correct. My mistake was that I had written coordiantes instead of coordinates the mongo gave me this:

pymongo.errors.WriteError: Can't extract geo keys: {data}  Point must be an array or object

so maybe you have the right ordering but you have problem in your key names, make sure you obey the mongo rules!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM