【发布时间】: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