在mongodb中搜索包含一系列点的所有多边形 [英] Search all polygons that contains a series of points in mongodb

查看:61
本文介绍了在mongodb中搜索包含一系列点的所有多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与此类似.我有一个mongodb数据库,其中包含两个集合:区域,用于存储一组多边形(意大利省),以及,用于存储一组具有坐标的标本一对.我正在使用mongodb 2.4和GeoJSON格式存储数据,两个集合都有一个2dsphere索引.

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

推荐答案

$geoIntersects rel ="nofollow">做到这一点的mongodb地理空间库.

There is $geoIntersects in the mongodb geospatial library that does that.

这篇关于在mongodb中搜索包含一系列点的所有多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆