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