2

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

Community
  • 1
  • 1
Alessandro
  • 303
  • 4
  • 12
  • I think not. Concider putting your data in a [postgis](http://postgis.net/) enabled database. Postgis is capable of these kind of quries at db level. – RickyA Apr 19 '13 at 09:19
  • Check: http://stackoverflow.com/questions/17355519/find-if-given-lat-long-lies-in-any-of-the-polygon-in-mongodb/20123041#20123041 – bonyiii Nov 21 '13 at 14:04
  • The aggregation framework now supports geo-spatial queries, so it might be possible to do something with that, but I haven't tried. – John Powell Jun 04 '14 at 14:28

1 Answers1

0

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

yanivnizry
  • 86
  • 3