在mongodb中搜索包含一系列点的所有多边形 [英] Search all polygons that contains a series of points in mongodb
问题描述
I have a mongodb database with two collections, regions that stores a set of polygons (italy provinces), and points that stores a set of specimens each with a coordinate pair. I am using mongodb 2.4 and GeoJSON format to store data, both collections have a 2dsphere index.
我能够找到给定的点是否在给定的多边形内.
I am able to find if a given point is inside a given polygon.
现在,我想查找包含标本列表的所有多边形,以绘制像这样的地图 http://www.peerates.org/province.png
Now I would like to find all polygons that contains a list of specimens, to draw a map like this one http://www.peerates.org/province.png
有没有比遍历所有点并利用mongodb地理索引检查每个点是否在每个多边形内更好的解决方案了?
Is there a better solution than iterate over all points and check if it is inside each polygon, leveraging mongodb geoindexes?
我使用存储在system.js集合中的函数找到了部分解决方案
edit: i found a partial solution using a function stored in system.js collection
function(){
var found = [];
var notfound = [];
db.regions.find().forEach(
function(region){
var regionId = region._id;
var query = {
'loc':{
$geoWithin: {
$geometry: region.loc
}
}
};
var len = db.points.find(query).size();
if(len>0){
found.push(regionId);
}else{
notfound.push(regionId);
}
}
);
return {
"found":found,
"notfound":notfound
};
}
很遗憾,我无法在mongohq.com上使用它,似乎不再支持eval().
sadly I cannot use it on mongohq.com it looks like eval() is no more supported.
@RickyA谢谢,我会考虑搬到邮递区
@RickyA thank you, I will consider moving to a postgis
推荐答案
There is $geoIntersects
in the mongodb geospatial library that does that.
这篇关于在mongodb中搜索包含一系列点的所有多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!