Search all polygons that contains a series of points in mongodb

核能气质少年 提交于 2019-12-05 03:53:06

问题


My question is similar to this one Given a set of polygons and a series of points, find the which polygons are the points located

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.

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

Is there a better solution than iterate over all points and check if it is inside each polygon, leveraging mongodb geoindexes?

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
    };
}

sadly I cannot use it on mongohq.com it looks like eval() is no more supported.

@RickyA thank you, I will consider moving to a postgis


回答1:


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



来源:https://stackoverflow.com/questions/16101132/search-all-polygons-that-contains-a-series-of-points-in-mongodb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!