使用mongodb 2.6多边形支持与mongoid

时间:2014-04-10 11:49:19

标签: mongodb geospatial mongoid4

我有以下型号:

class
  include Mongoid::Document

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

当我尝试插入以下文档时,出现错误:

{ 
  _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 operation: #<Moped::Protocol::Command ...> failed with error 16755: "Can't extract geo keys from object, malformed geometry?

根据http://geojsonlint.com/,上述geojson是有效的。我做错了什么?

1 个答案:

答案 0 :(得分:1)

从我看到的情况来看,你的多边形戒指没有关闭。您需要使用开始时的相同坐标主动结束每个环。在您的情况下,这将是有效的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 ]
                        ] 
                    ] 
                ] 
  }

根据geojsonlint,我相信语法是有效的,例如对于多线串,但是linter不会强制执行特定的几何类型规则。

请注意但是,您的MultiPolygon由两个多边形组成,每个多边形由两个环组成,但内环的坐标与外环的坐标相同。虽然这可能会在大多数geojson解析器中呈现,但它在拓扑上并不正确。内圈应在有限数量的点上与外圈相交。使一个或多个边相等意味着它们在无限点相交。

这是没有内圈的几何

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

在第二位,即使您移除内圈,您仍然有两个重叠的多边形,这些多边形不是有效的多边形。您可能能够绘制它,但在某个地方,您可能无法使用它来解析几何查询。