mongodb - 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?
解决方案
好消息:对于所有支持的 GeoJSON 类型,MongoDB 中的查询性能和可索引性都是相同的。
您决定的主要驱动因素应该是您的地理字段信息架构和使用它的软件是否需要包含更多类型而不仅仅是多边形。你说你在存储点位置?如果您想将所有地理数据保存在单个字段中,例如location
(并且可能带有 2dsphere 索引),那么您将需要GeometryCollection
可以放入其中Point
的MultiPolygon
. 在 GeoJSON 规范https://www.rfc-editor.org/rfc/rfc7946#page-9中建议 不要嵌套GeometryCollection
,因此对于那些给您 a 的数据源GeometryCollection
,您将迭代内容并填充您自己的内容,GeometryCollection
这也适用你Point
的等等
如果您将点分开存储,例如eventCenter
与 分开存储eventAreasEffected
,则eventCenter
可以只是 aPoint
而eventAreasEffected
可以是单个“MultiPolygon”;不需要GeometryCollection
。在多个字段中拥有 geo 并且在这些字段上拥有或不拥有多个 2dsphere 索引是完全可以的。从 MongoDB 4.0 开始,您可以通过包含该选项$geoNear
在具有多个 2dsphere 索引的集合上使用。key
这是一种非官方但合理的定义方法:AMultiPolygon
不是任意集合,Polygon
而是恰好具有不相交多边形的单个“形状概念”。美国可以用一个单一的来描述MultiPolygon
,有阿拉斯加、夏威夷、美国大陆,可能还有波多黎各等。事实上,为此,您会注意到存储与每个成员相关的数据有点棘手因为只能MultiPolygon
是coordinates
点数组的数组。例如,关于第三个多边形的信息必须在对等字段中传送到单个顶级coordinates
字段。Polygon
但是一个或一个GeometryCollection
的离散数组可以在每个Polygon
数组中存储额外的信息形状。type
请注意,GeoJSON和MongoDB 都不会限制您coordinates
为每个形状添加字段。
一个更微妙的问题是 a GeometryCollection
of Polygon
vs.的设计和语义MultiPolygon
。更复杂的是,存在一个明确的漏洞问题,即定义在Polygon
与隐式“分层”集合中的显式漏洞,这些漏洞Polygon
由地理软件在数据库之外进行后处理。
推荐阅读
- swift - viewDidDisappear 在主线程上吗?
- chart.js - 从点样式图例中删除 ChartJS 2 边框和阴影
- spring - Spring Boot 中的 Freemarker 模板渲染错误返回 200 OK
- java - Selenium Java 测试等待重定向完成
- sql - SQL 在使用减法运算时考虑重复
- java - Java GUI:repaint() 或类交互问题
- javascript - 选择的悬停背景 - 默认 chrome 如何更改为其他
- html - 如何包含 css 父编号 html?
- python - Python 使用 len() 方法列出计数
- python - 高基数数据的挑战