2

我在 2dsphere 索引集合中插入一个点,并尝试在多边形中找到它:

c = db.foo;
c.ensureIndex({'value.geometry': '2dsphere'});
c.insert({value: {geometry: {type: "Point", coordinates: [0, 0]}}});
c.findOne({'value.geometry': {$geoWithin: {$geometry:
    {"type":"Polygon","coordinates":[[[-90,-90],[90,-90],[90,90],[-90,90],[-90,-90]]]}}}})
// Point is found

但是,当我对宽度超过 180° 的多边形执行相同操作时,找不到该点:

c = db.foo;
c.ensureIndex({'value.geometry': '2dsphere'});
c.insert({value: {geometry: {type: "Point", coordinates: [0, 0]}}});
c.findOne({'value.geometry': {$geoWithin: {$geometry:
    {"type":"Polygon","coordinates":[[[-90.1,-90],[90.1,-90],[90.1,90],[-90.1,90],[-90.1,-90]]]}}}})
// no result -- why?

我在 MongoDB 手册中找不到这方面的任何信息。为什么有限制?

4

2 回答 2

2

我想如果你的多边形超过 180 度的经度,它可能会“关闭”地球的另一端。所以点 (0,0) 实际上不再在你的多边形内。点 (180,0) 可能是。您可以通过在地球的“前”部分添加更多点来创建所需的多边形,例如 (0,-90) 和 (0,90)。

于 2013-09-12T20:44:17.147 回答
0

在对此进行试验后,我得出结论 drmirrors 解决方案不会产生预期的结果:您不仅要在框的边界周围插入很多点,还要在框的内部插入很多点才能真正找到相交形状。

至于这是否是预期行为,MongoDB 文档实际上声明:“使用 GeoJSON 指定的任何几何到 $geoIntersects 查询,必须适合单个半球。MongoDB 将大于半球的几何解释为对较小的互补几何的查询。”

http://docs.mongodb.org/manual/reference/operator/geoIntersects/#op._S_geoIntersects

对于包含多个半球的查询,我采取了完全禁用过滤的方法。

于 2013-10-01T20:21:07.870 回答