Question

I'm having the following model:

class
  include Mongoid::Document

  field :polygons, type: Hash
  index({"polygon" => "2dsphere"})
end

When i try to insert the following document I get an error:

{ 
  _id: ObjectId('53467c7f476f6c551c020000'), 
  polygons: { 
    type: "MultiPolygon", 
    coordinates: [ [ [ [ 13.00695419311523, 47.81822655820738 ], [ 13.03579330444336, 47.75825258545904 ], [ 13.09175491333008, 47.7658685011539 ], [ 13.07767868041992, 47.81707386519431 ] ], [ [ 13.00695419311523, 47.81822655820738 ], [ 13.03579330444336, 47.75825258545904 ], [ 13.09175491333008, 47.7658685011539 ], [ 13.07767868041992, 47.81707386519431 ] ] ], [ [ [ 13.07355880737305, 47.8260641920274 ], [ 13.05810928344727, 47.80323955290061 ], [ 13.10857772827148, 47.80116408820393 ], [ 13.1041145324707, 47.82514217887775 ], [ 13.08626174926758, 47.8290606216547 ] ], [ [ 13.07355880737305, 47.8260641920274 ], [ 13.05810928344727, 47.80323955290061 ], [ 13.10857772827148, 47.80116408820393 ], [ 13.1041145324707, 47.82514217887775 ], [ 13.08626174926758, 47.8290606216547 ] ] ] ] 
  }
}

The error message only says:

The operation: #<Moped::Protocol::Command ...> failed with error 16755: "Can't extract geo keys from object, malformed geometry?

According to http://geojsonlint.com/ the above geojson is valid. What am I doing wrong?

Was it helpful?

Solution

From what I see, your multipolygon rings are not closed. You need to actively end each ring with the same coordinate where it started. In your case, this would be valid geoJson Multipolygon.

{ 
    type: "MultiPolygon", 
    coordinates: [ 
                    [
                        [ 
                            [ 13.00695419311523, 47.81822655820738 ], 
                            [ 13.03579330444336, 47.75825258545904 ], 
                            [ 13.09175491333008, 47.7658685011539 ], 
                            [ 13.07767868041992, 47.81707386519431 ],
                            [ 13.00695419311523, 47.81822655820738 ]
                        ], 
                        [ 
                            [ 13.00695419311523, 47.81822655820738 ], 
                            [ 13.03579330444336, 47.75825258545904 ], 
                            [ 13.09175491333008, 47.7658685011539 ], 
                            [ 13.07767868041992, 47.81707386519431 ] ,
                            [ 13.00695419311523, 47.81822655820738 ]
                        ] 
                    ], 
                    [ 
                        [ 
                            [ 13.07355880737305, 47.8260641920274 ], 
                            [ 13.05810928344727, 47.80323955290061 ], 
                            [ 13.10857772827148, 47.80116408820393 ], 
                            [ 13.1041145324707, 47.82514217887775 ], 
                            [ 13.08626174926758, 47.8290606216547 ],
                            [ 13.07355880737305, 47.8260641920274 ]
                        ], 
                        [ 
                            [ 13.07355880737305, 47.8260641920274 ], 
                            [ 13.05810928344727, 47.80323955290061 ], 
                            [ 13.10857772827148, 47.80116408820393 ], 
                            [ 13.1041145324707, 47.82514217887775 ], 
                            [ 13.08626174926758, 47.8290606216547 ],
                            [ 13.07355880737305, 47.8260641920274 ]
                        ] 
                    ] 
                ] 
  }

As per geojsonlint, I believe the syntax is valid, for example for multilinestring, but the linter would not enforce particular geometry type rules.

Please note however, that your MultiPolygon consists of two polygons, and each of them of two rings, but the inner ring has the same coordinates than the outer ring. While this might render in most geojson parsers, it is not topologically correct. Inner rings should intersect outer rings in a finite quantity of points. Having one or more edges equal means they intersect in infinite points.

This is the geometry without the inner rings

type: "MultiPolygon", 
    coordinates: [ 
                    [
                        [ 
                            [ 13.00695419311523, 47.81822655820738 ], 
                            [ 13.03579330444336, 47.75825258545904 ], 
                            [ 13.09175491333008, 47.7658685011539 ], 
                            [ 13.07767868041992, 47.81707386519431 ],
                            [ 13.00695419311523, 47.81822655820738 ]
                        ]
                    ], 
                    [ 
                        [ 
                            [ 13.07355880737305, 47.8260641920274 ], 
                            [ 13.05810928344727, 47.80323955290061 ], 
                            [ 13.10857772827148, 47.80116408820393 ], 
                            [ 13.1041145324707, 47.82514217887775 ], 
                            [ 13.08626174926758, 47.8290606216547 ],
                            [ 13.07355880737305, 47.8260641920274 ]
                        ]
                    ] 
                ] 
  }

In second place, even if you remove the inner rings, you'd still have two overlapping polygons, which are not a valid multipolygon. You might be able to draw it, but somewhere down the line you might not be able to parse geometrical queries with that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top