Ropstah
Ropstah

Reputation: 17794

MongoDb cannot create 2dsphere index on collection with Polygon and MultiPolygon

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

Upvotes: 3

Views: 1958

Answers (1)

Blakes Seven
Blakes Seven

Reputation: 50406

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.

Upvotes: 3

Related Questions