1

我将位置存储到集合中,并且基于边界框,我想检索此边界框中的位置。对于小边界框,一切似乎都很好,但对于较大的边界框,我没有得到预期的结果。当边界框的面积大于半球的面积时,我发现了一些关于指定 CRS 的信息:https ://docs.mongodb.com/manual/reference/operator/query/geoWithin/

尽管我的大边界框的面积不大于半球的面积,但我对小边界框使用的查询没有得到任何结果。

有关我的位置和下面使用的边界框的可视化表示,请参阅以下网站和地理点详细信息:https ://mobisoftinfotech.com/tools/plot-multiple-points-on-map/

小边界框:

32.441459,-39.891999,blue,marker,""
32.441459,80.332689,blue,marker,""
67.613969,80.332689,blue,marker,""
67.613969,-39.891999,blue,marker,""
50.850189,2.887406,green,marker,"Ypres"
29.780336,-95.359167,red,marker,"Houston"

大边界框:

32.441459,-79.891999,blue,marker,""
32.441459,80.332689,blue,marker,""
67.613969,80.332689,blue,marker,""
67.613969,-79.891999,blue,marker,""
50.850189,2.887406,green,marker,"Ypres"
29.780336,-95.359167,red,marker,"Houston"

我正在尝试在蓝色标记的边界框中获取数据,这应该会导致带有绿色标记的位置;伊普尔。

所以在 MongoDB 中,我有这两个职位:

db.getCollection("positions").insertOne(
{
    "name" : "Ypres, Belgium",
    "pos" : {
        "type" : "Point",
        "coordinates" : [ 
            2.887406, 
            50.850189
        ]
    }
})

db.getCollection("positions").insertOne(
{
    "name" : "Houston, Texas, US",
    "pos" : {
        "type" : "Point",
        "coordinates" : [ 
            -95.359167, 
            29.780336
        ]
    }
})

以逆时针顺序指定坐标的小多边形;结果OK:Ypres

db.getCollection("positions").find({
    "pos": {
        "$geoWithin": {
            "$geometry": {
                "type": "Polygon",
                "coordinates": [[
                    [-39.891999, 32.441459], 
                    [80.332689, 32.441459], 
                    [80.332689, 67.613969], 
                    [-39.891999, 67.613969], 
                    [-39.891999, 32.441459]]]
            }
        }
    }
})

坐标按顺时针顺序指定的同一个小多边形;结果OK:Ypres

db.getCollection("positions").find({
    "pos": {
        "$geoWithin": {
            "$geometry": {
                "type": "Polygon",
                "coordinates": [[
                    [-39.891999, 32.441459], 
                    [-39.891999, 67.613969], 
                    [80.332689, 67.613969], 
                    [80.332689, 32.441459], 
                    [-39.891999, 32.441459]]]
            }
        }
    }
})

更大的多边形,坐标按逆时针顺序指定;结果不行;没有地点。

db.getCollection("positions").find({
    "pos": {
        "$geoWithin": {
            "$geometry": {
                "type": "Polygon",
                "coordinates": [[
                    [-79.891999, 32.441459], 
                    [80.332689, 32.441459], 
                    [80.332689, 67.613969], 
                    [-79.891999, 67.613969], 
                    [-79.891999, 32.441459]]]
            }
        }
    }
})

所以在这里我想我可能需要 CRS,但是添加它时,我得到了相同的结果;没有地点。

db.getCollection("positions").find({
    "pos": {
        "$geoWithin": {
            "$geometry": {
                "type": "Polygon",
                "crs": {
                    "type": "name",
                    "properties": {
                        "name": "urn:x-mongodb:crs:strictwinding:EPSG:4326"
                    }
                },
                "coordinates": [[
                    [-79.891999, 32.441459], 
                    [80.332689, 32.441459], 
                    [80.332689, 67.613969], 
                    [-79.891999, 67.613969], 
                    [-79.891999, 32.441459]]]
            }
        }
    }
})

由于我不完全确定使用 CRS 时的顺时针/逆时针规格,因此我尝试颠倒多边形角的顺序。所以现在按顺时针顺序。但是在这里我得到了两个位置,而我只期待伊普尔。

db.getCollection("positions").find({
    "pos": {
        "$geoWithin": {
            "$geometry": {
                "type": "Polygon",
                "crs": {
                    "type": "name",
                    "properties": {
                        "name": "urn:x-mongodb:crs:strictwinding:EPSG:4326"
                    }
                },
                "coordinates": [[
                    [-79.891999, 32.441459], 
                    [-79.891999, 67.613969], 
                    [80.332689, 67.613969], 
                    [80.332689, 32.441459], 
                    [-79.891999, 32.441459]]]
            }
        }
    }
})

省略 CRS 时,我没有找到这个位置。

所以,我不完全确定这里发生了什么。我的查询有问题吗?

4

1 回答 1

0

显然,在计算一个点是否在多边形内时,$geoWithin与多边形相结合的运算符$geometry会遭受“大圆距”“失真”(不知道正确的术语如何,但这是我理解的方式)。为了我的目的和预期的结果,我不得不将我的查询改为$geoWithin组合$box

db.getCollection("positions").find({ "pos" : { "$geoWithin" : { "$box" : [[-79.891999, 32.441459], [80.3326890, 67.613969]] } } })
于 2022-02-02T10:40:49.330 回答