Mongodb 根据每个圆的相对半径查找用户所在的圆
这是场地。我有一组圆,它们主要有两个属性:位置是一个点,半径是以米为单位的距离 用户还具有profile.location点属性 在我的出版物中,我想找到用户所在的所有圆,根据每个圆的半径属性,找到用户离得足够近的圆。总而言之,下面是它的样子:Mongodb 根据每个圆的相对半径查找用户所在的圆,mongodb,collections,meteor,geometry,Mongodb,Collections,Meteor,Geometry,这是场地。我有一组圆,它们主要有两个属性:位置是一个点,半径是以米为单位的距离 用户还具有profile.location点属性 在我的出版物中,我想找到用户所在的所有圆,根据每个圆的半径属性,找到用户离得足够近的圆。总而言之,下面是它的样子: Meteor.publish('circles', function() { var curUser = Meteor.users.findOne({_id:this.userId}); if (curUser) { return
Meteor.publish('circles', function() {
var curUser = Meteor.users.findOne({_id:this.userId});
if (curUser) {
return Circles.find({
location: {$near:
{$geometry:curUser.profile.location,$maxDistance:self.radius} // HERE
}
}));
}
this.ready();
});
除了self.radius是一个代表我的完全虚构的术语。但有可能实现这样的目标吗
解决后编辑:
多亏了Electric Jesus,我的匹配可以完美地处理多边形,因为到目前为止圆还不是GeoJSON类型。因此,它们不是可以查询的单一属性,所以我将圆转换为多边形!下面是一个JavaScript函数来执行此操作:
function generateGeoJSONCircle(center, radius, numSides) {
var points = [];
var earthRadius = 6371;
var halfsides = numSides / 2;
//angular distance covered on earth's surface
var d = parseFloat(radius / 1000.) / earthRadius;
var lat = (center.coordinates[1] * Math.PI) / 180;
var lon = (center.coordinates[0] * Math.PI) / 180;
for(var i = 0; i < numSides; i++) {
var gpos = {};
var bearing = i * Math.PI / halfsides; //rad
gpos.latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(bearing));
gpos.longitude = ((lon + Math.atan2(Math.sin(bearing) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(gpos.latitude))) * 180) / Math.PI;
gpos.latitude = (gpos.latitude * 180) / Math.PI;
points.push([gpos.longitude, gpos.latitude]);
};
points.push(points[0]);
return {
type: 'Polygon',
coordinates: [ points ]
};
}
您可以尝试添加加权的voronoi图。距离函数是euklidian距离减去权重。权重较大的站点和附近的其他站点被排序到同一个单元格中。Mongo的地理空间操作符包括,它返回在圆圈范围内的文档:
Entities.find({
location : {
$geoWithin : {
$centerSphere: [
[ curUser.profile.location.lng , curUser.profile.location.lat ],
radius / 6378100 // convert radians to meters by dividing by the Earth's radius
]
}
}
} );
不,地理索引永远不会以您要求它根据文档的“半径”动态的方式工作。必须将圆转换为几何图形,并使用$GEOCERSECTS查询查找与当前位置/位置参数点几何图形相交的圆多边形几何图形
var location = {
longitude: -1.85549,
latitude: 52.9445
}
// create a circle with a Polygon geometry location, with converted coordinates
Circles.insert({name: "My Circle 1", loc: {
type: "Polygon",
coordinates: [[[ ... ]]],
}
});
// finding one or more Circles that intersect with current location.
Circles.find({loc: {
$geoIntersects: {
$geometry: {
type: "Point" coordinates: [location.longitude, location.latitude]
}
}
}});
这是你先前问题的翻版吗?不是。上一个问题已经回答了。这里的问题更复杂。你的例子中的半径是多少?不,这不是我想要的。我想找到所有半径不同的圆,每个圆都包含curUser.profile.location。不是什么圆心在当前用户的固定距离内。它可以工作!承认很痛苦,但这是正确的答案。希望我能有一个圆圈。。。
var location = {
longitude: -1.85549,
latitude: 52.9445
}
// create a circle with a Polygon geometry location, with converted coordinates
Circles.insert({name: "My Circle 1", loc: {
type: "Polygon",
coordinates: [[[ ... ]]],
}
});
// finding one or more Circles that intersect with current location.
Circles.find({loc: {
$geoIntersects: {
$geometry: {
type: "Point" coordinates: [location.longitude, location.latitude]
}
}
}});