gpt4 book ai didi

mongodb - 如何使用地理相交检查点是否在 mongodb 中的多边形内

转载 作者:行者123 更新时间:2023-12-03 15:00:05 34 4
gpt4 key购买 nike

我正在尝试学习如何在 MongoDB 中使用地理空间查询,但似乎我真的做对了!所以我在 mongo 中有我的瓷砖集合,为了争论,假设我只有一个文档,最准确的是,这个:

{
"_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 如何工作的东西,我错过了。到目前为止,我的发现是通过一个例子:

让我们假设我们的数据库中有 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,因为您必须在末尾附加初始点)。

现在,使用相同的查询实际上会产生这样的结果:
> 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页指示。

最佳答案

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

我已提出 an issue in mongodb Jira .

关于mongodb - 如何使用地理相交检查点是否在 mongodb 中的多边形内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37290249/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com