【问题标题】:$geoIntersects don't work like expected$geoIntersects 不像预期的那样工作
【发布时间】:2014-05-16 21:47:01
【问题描述】:

我有一个问题要咨询 $geoIntersects-Operator。 我有以下搜索框和集合内容:

> BOX
{
        "type" : "Polygon",
        "coordinates" : [
                [
                        [
                                0,
                                0
                        ],
                        [
                                3,
                                0
                        ],
                        [
                                3,
                                3
                        ],
                        [
                                0,
                                3
                        ],
                        [
                                0,
                                0
                        ]
                ]
        ]
}
> db.polygon.find()
{ "_id" : "Poly1", "shape" : { "type" : "Polygon", "coordinates" : [  [  [  0, 0 ],  [  3,  0 ],  [  3,  3 ],  [  0, 3 ],  [  0,  0 ] ] ] } } 
{ "_id" : "Poly2", "shape" : { "type" : "Polygon", "coordinates" : [  [  [  3, 0 ],  [  6,  0 ],  [  6,  3 ],  [  3,  3 ],  [  3,  0 ] ] ] } }
> db.polygon.find( {shape: {$geoIntersects: {$geometry: BOX}}}, {_id:1})
{ "_id" : "Poly1" }

如您所见,BOX 和 Poly1 是相同的。 Poly2 与 BOX 共享一条边。 因此,当我执行 $geoIntersects-Query 时,我期待两个多边形的 where 由于共享边而返回,但只找到了 Poly1。 有人可以向我解释一下吗?还是我犯了一个我看不到的愚蠢错误:(

Auf Wiedersehen,安德烈

【问题讨论】:

    标签: mongodb geospatial geo


    【解决方案1】:

    好问题。看起来这是一个错误或文档不准确。只是想分享一下我对这个问题的小研究成果

    要点:

    .find( {shape: {$geoIntersects: {$geometry: {type: "Point", coordinates : [3,0] }}}}, {_id:1})
    

    毫不奇怪,它同时返回 Poly1 和 Poly2。

    线串:

    .find( {shape: {$geoIntersects: {$geometry: {type: "LineString", coordinates : [[3,0], [3, 3]] }}}}, {_id:1})
    

    仅返回 Poly1,如果我们将线点的顺序颠倒怎么办?

    .find( {shape: {$geoIntersects: {$geometry: {type: "LineString", coordinates : [[3,3], [3, 0]] }}}}, {_id:1})
    

    现在只返回 Poly2。所以点的顺序对于 LineString 来说很重要,这对我来说真的很奇怪。

    多边形: 我们也尝试改变多边形查询的点顺序。

    .find( {shape: {$geoIntersects: {$geometry: {type: "Polygon", coordinates : [  [  [  3, 0 ],  [  3,  3 ],  [  6,  3 ],  [  6, 0 ],  [  3,  0 ] ] ]}}
    

    现在,即使线 [ 3, 0 ], [ 3, 3 ] 的点顺序与 Poly1 定义匹配,但它仍然只返回 Poly2。

    总结:

    所以当文档说

    这包括具有共享边的文档

    对于 Point 来说,这并不奇怪,对于 LineString 来说部分正确,因为点的顺序很重要!最后,Polygon 完全不是这样。

    这实际上很可悲,但真的很高兴知道。我希望我在研究过程中做错了什么,如果有人能提供一个很好的解释,我会很高兴。

    【讨论】:

    • @Andre 你好,你没有对我的回答做出任何贡献,只是想知道它是否对你有帮助?如果没有,我能帮您解决问题吗?
    【解决方案2】:

    只需使用较小的数字。对于像这样的三角形:

    [{
    _id:54cfbc19d9e1f418373ee427,
    geo:{type:Polygon,
    coordinates:[[[0.3,0.3],[0,0.3],[0,0],[0.3,0.3]]]}
    },
    {_id:54cfbc19d9e1f418373ee428,
    geo:{type:Polygon,
    coordinates:[[[0,0],[0.3,0],[0.3,0.3],[0,0]]]}
    }]
    
    .find({
        geo: {
            $geoIntersects: {
                $geometry: {
                    type: "Point" ,
                    coordinates: [0.005,0.005]
                }
            }
        }
    })
    

    会给你正确的结果。 我猜 $geoIntersects 认为地球是球体。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-13
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 2016-03-07
      • 1970-01-01
      相关资源
      最近更新 更多