如何使用geointersects检查一个点是否在mongodb中的多边形内

时间:2016-05-18 04:20:21

标签: mongodb geospatial

我试图学习如何在MongoDB中使用地理空间查询,但似乎我无法真正做到正确!所以我在mongo中有我的tile集合,为了论证,假设我只有一个文档,最准确地说,这个:

{
    "_id" : "-180-9018090",
    "geometry" : {
        "type" : "Polygon",
        "coordinates" : [
            [
                [
                    -180,
                    -90
                ],
                [
                    180,
                    -90
                ],
                [
                    180,
                    90
                ],
                [
                    -180,
                    90
                ],
                [
                    -180,
                    -90
                ]
            ]
        ]
    },
    "type" : "Feature",
    "properties" : {
        "zoom-level" : 0,
        "center" : [
            0,
            0
        ]
    }
}

基本上代表整个世界。现在我想看看是否某一点,让我们说(0,0)在这个区域内。我的理解是我应该使用geointersects来完成这个任务,所以查询应该看起来像这样(?):

db.tiles.find({
  geometry: {
     $geoIntersects: {
        $geometry: {
           type: "Point" ,
           coordinates: [ 0,0]
        }
     }
  }
});

但当然结果集是空的,因为我的想法是关于为什么会这样。你能帮我理解我做错了吗?

编辑:

经过进一步的尝试,查询似乎是正确的,因此必须有一些关于$ geointersects如何工作的东西,我错过了。到目前为止我的发现只是一个例子:

让我们假设我们的db中有5个文档:

          *-----*
          | 4|3 | => whole world tile: from [-90,-180] to [90,180]
          | 1|2 |
          *-----* 

let's take this tile and divide it in 4:

*---*  *---*  *---*  *---*
| 1 |  | 2 |  | 3 |  | 4 | => 1) [-90,-180]-> [0,0]   (lower left)
*---*  *---*  *---*  *---*    2) [0,-90]   -> [180,0] (lower right)
                              3) [0,0]     -> [180,90](upper right)
                              4) [-180,0]  -> [0,90]  (upper left)

所以,由于糟糕的架构难以展示,我们有5个文档,每个文档代表4个顶点的多边形(由于你必须在末尾附加初始点,因此geoJson中的5个变为5)。

现在,使用相同的查询实际上会产生这样的结果:

> db.tiles.find({ geometry: { $geoIntersects: { $geometry: { type: "Point", coordinates: [ 0,0 ] } } } }).sort({"zoom": 1})
    { "_id" : "0.0-90.0180.00.0", "geometry" : { "type" : "Polygon", "coordinates" : [ [ [ 0, -90 ], [ 180, -90 ], [ 180, 0 ], [ 0, 0 ], [ 0, -90 ] ] ] }, "zoom" : 1 }
    { "_id" : "-180.00.00.090.0", "geometry" : { "type" : "Polygon", "coordinates" : [ [ [ -180, 0 ], [ 0, 0 ], [ 0, 90 ], [ -180, 90 ], [ -180, 0 ] ] ] }, "zoom" : 1 }
    { "_id" : "0.00.0180.090.0", "geometry" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 180, 0 ], [ 180, 90 ], [ 0, 90 ], [ 0, 0 ] ] ] }, "zoom" : 1 }

是瓷砖2,3,4。换句话说,这两个文件落在了后面:

    {
        "_id" : "-180-9018090",   ---> world wide tile
        "geometry" : {
            "type" : "Polygon",
            "coordinates" : [
                [
                    [
                        -180,
                        -90
                    ],
                    [
                        180,
                        -90
                    ],
                    [
                        180,
                        90
                    ],
                    [
                        -180,
                        90
                    ],
                    [
                        -180,
                        -90
                    ]
                ]
            ]
        },
        "zoom" : 0
    }
{
    "_id" : "-180.0-90.00.00.0",  ----> tile number 1 of the example
    "geometry" : {
        "type" : "Polygon",
        "coordinates" : [
            [
                [
                    -180,
                    -90
                ],
                [
                    0,
                    -90
                ],
                [
                    0,
                    0
                ],
                [
                    -180,
                    0
                ],
                [
                    -180,
                    -90
                ]
            ]
        ]
    },
    "zoom" : 1
}

现在,我的第一个猜测是全球文件没有被选中,因为它太大了,而且可能是传统原因的另一个?有人可以验证或反驳这个吗?谢谢

编辑:

This可以解释为什么没有选择较大的那个,我会测试它。

编辑:

看起来不是。 CRS仅在您尝试与两个多边形相交时才有效,因此输入查询不能是一个点,如this页面所示。​​

1 个答案:

答案 0 :(得分:0)

这不是解决方案,只是一个通知,表明mongodb仅在您查询多边形时才允许CRS。 (see this)。但是,即使您查询多边形,我也看不出它会有什么帮助:您想指定数据库中的多边形是“大”的,大于半球形(一个跨越地球一半以上的多边形)表面)。因此,在插入时启用CRS对我来说很有意义。

我已经提出an issue in mongodb Jira

相关问题