首页 > 解决方案 > MongoDb/GeoJson: MultiPolygon vs GeometryCollection containing only polygons

问题描述

I'm collecting location information from different sources and storing everything in a MongoDb collection. Apart from point locations with a single lat/lng coordinates, I'm also storing areas.

Now, one data gives me the location information as GeometryCollection but with all elements being Polygons. Another data source gives me the location as MultiPolygon. While I'm actually considering have a collection for each data source, I'm wondering which approach is better in the whole.

GeometryCollection is certainly more flexible, but maybe MultiPolygon shows better query performance (given that I always create a 2dspehere index over the location field). Is it worth it to convert one representation into the other?

标签: mongodbgeospatialgeojsonspatial-index

解决方案


好消息:对于所有支持的 GeoJSON 类型,MongoDB 中的查询性能和可索引性都是相同的。

您决定的主要驱动因素应该是您的地理字段信息架构和使用它的软件是否需要包含更多类型而不仅仅是多边形。你说你在存储点位置?如果您想将所有地理数据保存在单个字段中,例如location(并且可能带有 2dsphere 索引),那么您将需要GeometryCollection可以放入其中PointMultiPolygon. 在 GeoJSON 规范https://www.rfc-editor.org/rfc/rfc7946#page-9中建议 不要嵌套GeometryCollection,因此对于那些给您 a 的数据源GeometryCollection,您将迭代内容并填充您自己的内容,GeometryCollection这也适用你Point的等等

如果您将点分开存储,例如eventCenter与 分开存储eventAreasEffected,则eventCenter可以只是 aPointeventAreasEffected可以是单个“MultiPolygon”;不需要GeometryCollection。在多个字段中拥有 geo 并且在这些字段上拥有或不拥有多个 2dsphere 索引是完全可以的。从 MongoDB 4.0 开始,您可以通过包含该选项$geoNear在具有多个 2dsphere 索引的集合上使用。key

这是一种非官方但合理的定义方法:AMultiPolygon不是任意集合,Polygon而是恰好具有不相交多边形的单个“形状概念”。美国可以用一个单一的来描述MultiPolygon,有阿拉斯加、夏威夷、美国大陆,可能还有波多黎各等。事实上,为此,您会注意到存储与每个成员相关的数据有点棘手因为只能MultiPolygoncoordinates点数组的数组。例如,关于第三个多边形的信息必须在对等字段中传送到单个顶级coordinates字段。Polygon但是一个或一个GeometryCollection的离散数组可以在每个Polygon数组中存储额外的信息形状。type请注意,GeoJSON和MongoDB 都不会限制您coordinates为每个形状添加字段。

一个更微妙的问题是 a GeometryCollectionof Polygonvs.的设计和语义MultiPolygon。更复杂的是,存在一个明确的漏洞问题,即定义在Polygon与隐式“分层”集合中的显式漏洞,这些漏洞Polygon由地理软件在数据库之外进行后处理。


推荐阅读