【发布时间】:2013-04-19 09:11:45
【问题描述】:
我的问题和这个Given a set of polygons and a series of points, find the which polygons are the points located类似
我有一个 mongodb 数据库,其中包含两个集合,regions 存储一组多边形(意大利省份),points 存储一组样本,每个样本都有一个坐标一对。我使用 mongodb 2.4 和 GeoJSON 格式来存储数据,两个集合都有一个 2dsphere 索引。
我能够找到给定点是否在给定多边形内。
现在我想找到包含标本列表的所有多边形,以绘制像这样的地图http://www.peerates.org/province.png
有没有比遍历所有点并检查它是否在每个多边形内更好的解决方案,利用 mongodb 地理索引?
编辑: 我使用存储在 system.js 集合中的函数找到了部分解决方案
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() 不再受支持。
@RickyA 谢谢,我会考虑转到 postgis
【问题讨论】:
-
我认为不会。考虑将您的数据放入启用postgis 的数据库中。 Postgis 能够在数据库级别进行此类查询。
-
聚合框架现在支持地理空间查询,所以也许可以用它做点什么,但我没有尝试过。