0

根据文档,2dSphere 地理空间索引也可用于复杂的 GeoJSON 形状,例如由多个节点组成的 LineString 和 Polygon。

当您使用$nearSphere搜索填充了此类复杂形状的字段以接近另一个 GeoJSON 对象(也可能是复杂形状)时,如何计算这些形状的距离?

它会比较形状的最近节点吗?最远的节点?节点的一些平均值?形状的任何边缘上的最近点?

4

1 回答 1

2

$nearSphere运算符仅允许您指定一个点(您可以通过提供maxDistance参数定义的“球体”的中心)。因此,唯一有意义的答案可能是关于形状如何排序最近到最远。

答案是所有具有等距“最近”点的形状都将排在距离最近点更远的形状之前。换言之,点与形状之间的距离是点与形状上最近点之间的距离。

我们可以看到,通过运行一个变体$nearSphere$geoNear命令,因为它返回计算出的距离,然后我们可以检查。

我们的样本数据:

> db.near.find()
{ "_id" : 10, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 5 ], [ 5, 5 ], [ 5, 0 ], [ 0, 0 ] ] ] } }
{ "_id" : 11, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 1, 1 ], [ 1, 5 ], [ 5, 5 ], [ 5, 1 ], [ 1, 1 ] ] ] } }
{ "_id" : 12, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [ 0, 0 ] ] ] } }
{ "_id" : 13, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [0, 0 ] ], [ [ 1, 1 ], [ 1, 5 ], [ 5, 5 ], [ 5, 1 ], [ 1, 1 ] ] ] } }
{ "_id" : 14, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [ 0, 0 ] ], [ [ 0.1, 0.1 ], [ 0.1, 5 ], [ 5, 5 ], [ 5, 0.1 ], [ 0.1, 0.1 ] ] ] } }
{ "_id" : 15, "loc" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 1 ], [ 1, 1 ], [ 1, 0 ], [0, 0 ] ],  [ [ 0.1, 0.1 ], [ 0.1, 0.5 ], [ 0.5, 0.5 ], [ 0.5, 0.1 ], [ 0.1, 0.1 ] ] ] } }
{ "_id" : 16, "loc" : { "type" : "LineString", "coordinates" : [ [ 0, 0 ], [ 0.1, 0.1 ] ] } }
{ "_id" : 17, "loc" : { "type" : "LineString", "coordinates" : [ [ 0, 0 ], [ 0, 0.1 ] ] } }
{ "_id" : 18, "loc" : { "type" : "LineString", "coordinates" : [ [ 0, 0 ], [ 0.1, 0 ] ] } }

我们使用 $nearSphere 的结果:

{ "_id" : 16, "loc" : { "type" : "LineString", "coordinates" : [  [ 0, 0 ],  [  0.1,  0.1 ] ] } }
{ "_id" : 18, "loc" : { "type" : "LineString", "coordinates" : [  [ 0, 0 ],  [  0.1,  0 ] ] } }
{ "_id" : 14, "loc" : { "type" : "Polygon", "coordinates" : [  [  [ 0, 0 ], [ 0, 6 ], [     6,  6 ],    [   6,  0 ],    [   0,  0 ] ],  [   [   0.1,    0.1 ],  [   0.1,    5 ],    [   5,  5 ],    [   5,  0.1 ],  [   0.1,    0.1 ] ] ] } }
{ "_id" : 13, "loc" : { "type" : "Polygon", "coordinates" : [  [  [ 0, 0 ], [ 0, 6 ], [     6,  6 ],    [   6,  0 ],    [   0,  0 ] ],  [   [   1,  1 ],    [   1,  5 ],    [   5,  5 ],    [   5,  1 ],    [   1,  1 ] ] ] } }
{ "_id" : 15, "loc" : { "type" : "Polygon", "coordinates" : [  [  [ 0, 0 ], [ 0, 1 ], [     1,  1 ],    [   1,  0 ],    [   0,  0 ] ],  [   [   0.1,    0.1 ],  [   0.1,    0.5 ],  [   0.5,    0.5 ],  [   0.5,    0.1 ],  [   0.1,    0.1 ] ] ] } }
{ "_id" : 12, "loc" : { "type" : "Polygon", "coordinates" : [  [  [ 0, 0 ],  [  0,  6 ],  [  6,  6 ],  [  6,  0 ],  [  0,  0 ] ] ] } }
{ "_id" : 17, "loc" : { "type" : "LineString", "coordinates" : [  [ 0, 0 ],  [  0,  0.1 ] ] } }
{ "_id" : 10, "loc" : { "type" : "Polygon", "coordinates" : [  [  [ 0, 0 ],  [  0,  5 ],  [  5,  5 ],  [  5,  0 ],  [  0,  0 ] ] ] } }
{ "_id" : 11, "loc" : { "type" : "Polygon", "coordinates" : [  [  [ 1, 1 ],  [  1,  5 ],  [  5,  5 ],  [  5,  1 ],  [  1,  1 ] ] ] } }

运行$geoSphere命令,我们可以确认点到形状的距离(出于空间考虑,省略坐标):

> db.runCommand({geoNear:"near", near: {type:"Point", coordinates:[0,0]}, spherical:true})
[
    {
        "dis" : 0,
        "obj" : {
            "_id" : 16,
            "loc" : {
                "type" : "LineString"
            }
        }
    },
    {
        "dis" : 0,
        "obj" : {
            "_id" : 18,
            "loc" : {
                "type" : "LineString"
            }
        }
    },
    {
        "dis" : 0,
        "obj" : {
            "_id" : 14,
            "loc" : {
                "type" : "Polygon"
            }
        }
    },
    {
        "dis" : 0,
        "obj" : {
            "_id" : 13,
            "loc" : {
                "type" : "Polygon"
            }
        }
    },
    {
        "dis" : 0,
        "obj" : {
            "_id" : 15,
            "loc" : {
                "type" : "Polygon"
            }
        }
    },
    {
        "dis" : 0,
        "obj" : {
            "_id" : 12,
            "loc" : {
                "type" : "Polygon"
            }
        }
    },
    {
        "dis" : 0,
        "obj" : {
            "_id" : 17,
            "loc" : {
                "type" : "LineString"
            }
        }
    },
    {
        "dis" : 0,
        "obj" : {
            "_id" : 10,
            "loc" : {
                "type" : "Polygon"
            }
        }
    },
    {
        "dis" : 157424.6238723255,
        "obj" : {
            "_id" : 11,
            "loc" : {
                "type" : "Polygon"
            }
        }
    }
]

如您所见,每个形状(除了从 [1,1] 开始的最后一个形状)到点 [0,0] 的距离都是 0,即使这些形状的大小和形状都不同。在到指定点的距离相同的文档中,不定义顺序。这意味着即使它看起来是确定性的,也不能保证它是相同的。

于 2013-10-18T18:55:23.500 回答