【发布时间】:2016-09-14 09:17:58
【问题描述】:
我正在尝试学习如何在 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 页面所示。
【问题讨论】:
-
我看到当您在文档中插入一个位置点并尝试通过 $geoIntersects 使用多边形找到它时,它可以工作,但它不能以其他方式工作(您的方式)。
-
现在我明白为什么它不起作用了。请查看stackoverflow.com/questions/7810008/…
-
@AMITAVA 你是说不可能吗?因为这个:stackoverflow.com/questions/20161180/… 似乎另有说明
-
@AMITAVA 另外,你能说清楚点吗? “当您在文档中插入一个位置点并尝试用多边形找到它时”没有多大意义。您确定没有将 geoIntersects 与 geoWithin 混淆吗?
-
@AMITAVA 哦,我认为你链接的帖子已经过时了。
标签: mongodb geospatial