【问题标题】:How to use $geoIntersects in MongoDB properly?如何在 MongoDB 中正确使用 $geoIntersects?
【发布时间】:2015-03-05 10:14:17
【问题描述】:

我想使用 $geoIntersects 来检查一个点是否在多边形中。

我在我的 mongo 数据库中保存了来自 here 的英国地区。 我用 mongoose-geojson-schema 模块保存了所有 geoJSON 实体。

这里是 mongoose 中定义的模式

var geozoneSchema = mongoose.model('Geozone', {
    geoFeature:  GeoJSON.Feature,
    average:    {type: Number, default: null},
    updated_at: {type: Date, default: Date.now}
});

我写了这个查询,但是没有用。

Geozone.findOne({
    "geometry.coordinates": {
        "$geoIntersects": {
            "$geometry": {
                "type": "Point",
                "coordinates": fakeUser.loc
            }
        }
    }
}, function(err, foundGeozone) {
    if (err) throw err;
    console.log('found geozone: ', foundGeozone);
    if (foundGeozone) {
        console.log('found geozone! Good! Save User.');
    }
});

fakeUser.loc中存储了这一点

 user.loc = [-3.2, 54.5]; //real loc

所以我确定该点确实在特定存储区域内,但变量 foundGeozone 为空。

也许,我的查询有误或其他原因。你能帮帮我吗?

【问题讨论】:

  • 那么“点”实际上是在构成多边形的“线”上吗?或者你的意思是$geoWithin?样本数据,预期结果。这就是一个明确的问题。
  • @NeilLunn 点现在不在多边形边界上,但我认为将来有可能。我使用的geoIntersect或geoWithin有什么显着不同吗?我认为 geoIntersect 应该可以正常工作,还是我错了?
  • 它要么“相交”,要么不相交。这是true|false 评估。 “相交”和“内”是两个不同的东西。根据需要测试一项或两项。
  • @NeilLunn $geoWithin 不起作用。
  • 考虑到您的问题缺乏数据和预期结果,因此信息量并不大。如果您想要解决方案,请给人们一些可以合作的东西:stackoverflow.com/help/how-to-ask

标签: node.js mongodb mongoose geospatial mongodb-query


【解决方案1】:

根据https://github.com/glynnbird/ukcountiesgeojson 中的数据架构,您要搜索的位置字段应该是“geometry”,而不是“geometry.coordinates”:

Geozone.findOne({
    "geometry": {
        "$geoIntersects": {
            "$geometry": {
                "type": "Point",
                "coordinates": fakeUser.loc
            }
        }
    }
}, function(err, foundGeozone) {
    if (err) throw err;
    console.log('found geozone: ', foundGeozone);
    if (foundGeozone) {
        console.log('found geozone! Good! Save User.');
    }
});

【讨论】:

    【解决方案2】:

    在 Victor 的情况下,位置字段应该是 'geoFeature.geometry'

    Geozone.findOne({ 'geoFeature.geometry':  
        {$geoIntersects:
          {$geometry:{ "type" : "Point",
            "coordinates" : [longitude, latitude] } //fakeUser.loc
          }
        }
    })
    

    【讨论】:

      猜你喜欢
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 2016-09-14
      • 2020-10-07
      • 1970-01-01
      • 2019-04-13
      相关资源
      最近更新 更多