我有一个带有地理点的数据集。
{ _id ...其他字段...位置:{类型:“点”,坐标:[0,0]}}
我一直在尝试做的是过滤并删除任何在水体中有点的文件。我下载并转换了地球水体的形状文件,并将其存储在同一数据库中的单独集合中。我一直在尝试使用 Mongo 的 geoWithin 函数,但是当我指定水体的多边形文档时,我无法让它为我工作。如果我对多边形进行硬编码,它可以工作,但我真的不想将地球上所有的水多边形都输入到我的代码中......
这不起作用:
var geo = {type: "Point", coordinates: [0,0]}
db.waterBodies.find({
geo: {
$geoWithin: {
$geometry: {
type: 'Polygon',
coordinates: "$geometry.coordinates"
}
}
}
}).count()
或这个
var geo = {type: "Point", coordinates: [0,0]}
var poly = [[[0,0],[0,3],[3,0],[0,0]]]
db.data.find({
geo: {
$geoWithin: {
$geometry: {
type: 'Polygon',
coordinates: "$poly"
}
}
}
}).count()
它会生成此错误:
E QUERY [js] 未捕获的异常:错误:计数失败:{“ok”:0,“errmsg”:“多边形坐标必须是数组”,“code”:2,“codeName”:“BadValue”}:_getErrorWithCode@ src/mongo/shell/utils.js:25:13 DBQuery.prototype.count@src/mongo/shell/query.js:376:11 @(shell):1:1
这有效并且不会引发错误,但需要硬编码值:
var geo = {type: "Point", coordinates: [0,0]}
db.data.find({
geo: {
$geoWithin: {
$geometry: {
type: 'Polygon',
coordinates: [[[0,0],[0,3],[3,0],[0,0]]]
}
}
}
}).count()
和这个
db.data.find({
'location.coordinates': {
$geoWithin: {
$geometry: {
type: 'Polygon',
coordinates: [[[0,0],[0,3],[3,0],[0,0]]]
}
}
}
}).count()
我粗略的不想计数,但将其用于测试目的。
对我来说,简单的答案看起来像这样:
const polygons = await getPolygons(); //get all the polygons from the water body's collection
const filter = {
'location.coordinates': { //the points I want to filter
$geoWithin: { //could also be geoIntersects
$geometry: {
type: 'MultiPolygon',
coordinates: polygons.geometry.coordinates //the water body polygons
}
}
}
};
try {
await db.collection(collection).deleteMany(filter);
} catch (e) {
if (e instanceof MongoError) {
Logger.info('Error deleting log');
throw e;
}
}
我想使用多面体,因为有很多水体。
到目前为止,我已经阅读了我在谷歌上可以找到的所有内容,但对我没有任何帮助。我找到的所有示例都为坐标数组硬编码,但我不想这样做。请帮助并让我知道是否有办法做到这一点或删除所有在陆地上找不到的点。
提前致谢。