如何获取mongodb中的边界框内的任何项目(Point,LineString,Polygon)

时间:2013-11-14 15:16:32

标签: javascript mongodb mongoose geospatial

我遇到了一个查询匹配BoundingBox内的项目的问题。 如何匹配2dsphere类型的所有项目(GEO JSON)? 在这种情况下,我只获得Point类型的数据,但LineString类型的项目不会出现在结果中。

架构定义如下例所示:

/**
 * Media location values (Point, LineString, Polygon)
 * @property location
 * @type {Object}
 */
 location:{
      "type":Object,
      "index":"2dsphere"
 },

我已经例如这个项目在其中:

[

    {
        "_account": "52796da308d618090b000001",
        "_id": "5284e5798a1c039735000001",
        "location": {
            "coordinates": [
                8.663705555555556,
                50.10165277777778
            ],
            "type": "Point"
        },
        "name": "Foto.JPG",
        "preview": "/img/thumbs/13719-3zavxm.JPG",
        "type": "image/jpeg",
        "added": "2013-11-14T15:00:09.113Z",
        "latlng": [ ],
        "shares": [ ],
        "shared": false,
        "tags": [ ]
    },
    {
        "_account": "52796da308d618090b000001",
        "name": "Filtererd_Track.kml",
        "type": "application/vnd.google-earth.kml+xml",
        "_id": "5284e5c48a1c039735000002",
        "added": "2013-11-14T15:01:24.280Z",
        "latlng": [ ],
        "shares": [ ],
        "shared": false,
        "tags": [ ]
    },
    {
        "_account": "52796da308d618090b000001",
        "_id": "5284e5c48a1c039735000003",
        "location": {
            "coordinates": [
                [
                    9.49653,
                    50.94791
                ],
                [
                    9.49731,
                    50.94811
                ],
                [
                    9.49744,
                    50.94812
                ],
                [
                    9.49755,
                    50.94808
                ],
                [
                    9.4991,
                    50.94579
                ],
                [
                    9.49969,
                    50.94545
                ],
                [
                    9.50037,
                    50.94525
                ],
                [
                    9.50136,
                    50.9452
                ],
                [
                    9.50851,
                    50.98557
                ]
            ],
            "type": "LineString"
        },
        "name": "test 2.gpx",
        "preview": "/img/thumbs/13719-14rvt8w.png",
        "type": "application/gpx+xml",
        "added": "2013-11-14T15:01:24.529Z",
        "latlng": [ ],
        "shares": [ ],
        "shared": false,
        "tags": [ ]
    }

]

获取项目的查询类似于以下示例,但它与LineStrings / Polygons不匹配.... 认为这是因为子阵列,但不知道如何在查询中包含它。

{
    {
        "location": {
            "$geoWithin": {
                "$box": [
                    [
                        -39.375,
                        36.73888412439431
                    ],
                    [
                        76.640625,
                        56.897003921272606
                    ]
                ]
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

我找到了一种方法,可以使用$geoIntersects将所有内容放入边界框,并从边界框创建多边形。 如下面的例子。

    {
    "location": {
        "$geoIntersects": {
            "$geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            5.372314453125,
                            52.288322586002984
                        ],
                        [
                            12.623291015625,
                            52.288322586002984
                        ],
                        [
                            12.623291015625,
                            49.67829251994456
                        ],
                        [
                            5.372314453125,
                            49.67829251994456
                        ],
                        [
                            5.372314453125,
                            52.288322586002984
                        ]
                    ]
                ]
            }
        }
    }
    ]
}