Mongo-geoWithin错误:多边形坐标必须是一个数组



我有一个带有地理点的数据集。

{_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]未捕获异常:错误:计数失败:{quot;ok":0,"errmsg":"多边形坐标必须是数组","code":2,"代号":"BadValue"}:_getErrorWithCode@src/mongo/shell/utils.js:25:13DBQuery.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;
}
}

我想使用多极子,因为有很多水体。

到目前为止,我已经阅读了我在谷歌上能找到的所有东西,但对我来说什么都不起作用。所有的例子我都找到了一个坐标数组的硬代码,但我不想这样做。请帮忙,让我知道是否有办法做到这一点或删除所有在陆地上找不到的点。

提前谢谢。

这对你有用吗?

db.data.insert({
geo: {
type: "Point",
coordinates: [0,0]
}
})
db.polygons.insert({
geo: {
type: "Polygon",
coordinates: [[[0,0],[0,3],[3,0],[0,0]]]
}
})

var polygon = db.polygons.findOne()    
db.data.find({
geo: {
$geoWithin: {
$geometry: {
type: 'Polygon',
coordinates: polygon.geo.coordinates
}
}
}
}).count()

最新更新