2

I'm trying to add a 2dsphere index and run into problems with malformed geometry (using MongoDB 2.6 with 2dsphere index version 2)

The collection contains both documents with Polygon geometry as documents with MultiPolygon geometry. Mongo throws a #16755 error (Malformed geometry) a soon as it reaches a document which has a MultiPolygon geometry. The GeoJSON is correct according to GeoJSONlint.com

Is it allowed to mix geometry types when setting an index?

How do I cope with this issue?

The document which fails looks like this: (I omitted several points for readability. Both polygons are still closing...)

{
    "type" : "MultiPolygon",
    "coordinates" : [ 
        [ 
            [ 
                [ 
                    4.8730935147694279, 
                    51.4125385138567450
                ], 
                [ 
                    4.8731073690744831, 
                    51.4124188435040280
                ], 
                [ 
                    4.8719363156445858, 
                    51.4121631573312000
                ], 
                [ 
                    4.8720931816264326, 
                    51.4120192196300750
                ], 
                [ 
                    4.8730935147694279, 
                    51.4125385138567450
                ]
            ]
        ], 
        [ 
            [ 
                [ 
                    4.9354151466562142, 
                    51.4320525317730240
                ], 
                [ 
                    4.9341804433318899, 
                    51.4319519241268350
                ], 
                [ 
                    4.9341480860178217, 
                    51.4323138673607550
                ], 
                [ 
                    4.9341289343773811, 
                    51.4329459213489240
                ], 
                [ 
                    4.9341142802746933, 
                    51.4334292461250870
                ], 
                [ 
                    4.9354151466562142, 
                    51.4320525317730240
                ]
            ]
        ]
    ]
}
Ropstah
  • 17,538
  • 24
  • 120
  • 194

1 Answers1

4

Took my a while to find an available "tuit" but the answer here is really quite simple. It seems the problem here lies with the very first "Polygon" object in your "MultiPolygon" definition.

Just extracting:

{
    "type": "Polygon",
    "coordinates": [[
        [ 
            4.9354151466562142, 
            51.4320525317730240
        ], 
        [ 
            4.9341804433318899, 
            51.4319519241268350
        ], 
        [ 
            4.9341480860178217, 
            51.4323138673607550
        ], 
        [ 
            4.9341289343773811, 
            51.4329459213489240
        ], 
        [ 
            4.9341142802746933, 
            51.4334292461250870
        ], 
        [ 
            4.9354151466562142, 
            51.4320525317730240
        ]
    ]]
}

So that's the part, but of course it helps to see what that looks like:

Polygon1

So the great big "OOPS!" here is that the "Polygon" itself is intersecting to form "two" distinct areas. Now MongoDB and other GIS capable engines "do not like that" and expect a "Polygon" with at least consistent outer bounds. Having an "inner ring" is fine, but since this separates two areas then the shape is deemed invalid for storage.

The clear fix for this is to inspect your data and find any such "intersecting" boundaries. Then separate those into individual "Polygon" object definitions ( within the array of a "MultiPolygon" is also fine ) as you store them.

So your current "MultiPolygon" definition defines "two" "Polygon" objects, but what is expected here is "three" with the intersecting object being broken into "two" objects at the point of intersection. As long as you fit within those constraints then your "indexing" will work and you can query on those objects with all the normal geospatial operations.

Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
  • Ah didn't know this wasn't allowed. I'm gonna have to check the original MultiPolygon to make sure this is also the case for original data (as stated I trimmed the points for readability in my question). Thanks for the lead so far! – Ropstah Jul 10 '15 at 22:04
  • 1
    @Ropstah It would be good to hear your feedback on what happends with that result. But the "question you asked here" is basically answered by the "geometery you have shown" is deemed invalid for storage. It also gives you a good deal of information for which to debug your present geometry collection. Therefore I deem "the question you actually asked" to be answered. So please mark it as such. If you have another detailed question to ask with more information than this then please feel free to ask it. I'm sure someone will answer that question when you form it. This is answered. – Blakes Seven Jul 11 '15 at 12:47
  • Makes sense, however I did mention that I omitted points for readability but kept the polygon closing. I will be able to check if the polygon has overlapping areas this week. Keep you posted for sure! – Ropstah Jul 20 '15 at 13:53
  • @Ropstah So far you have twice tried to justify that "ahh but my question is incomplete" as if that is a "good" thing. It is not. Please don't think you are coming back here and changing anything in your question. You asked a question and it was answered, and from that you learned something. If you need to ask another question after what you haved leaned here then [post another question.](http://stackoverflow.com/questions/ask) – Blakes Seven Jul 20 '15 at 22:02
  • Ok you are right, i was too focused on solving my problem, i lost the goal of this site out of sight. – Ropstah Jul 22 '15 at 00:11