根据每个圆的相对半径查找用户所在的圆圈

时间:2014-10-27 19:50:16

标签: mongodb collections meteor geometry

这是球场。我有一个圈子集合,主要有两个属性:location是一个点,radius是一个以米为单位的距离。

用户还拥有profile.location点属性。

在我的出版物中,我想找到用户所在的所有圈子,根据每个Circle的radius属性,他或她足够接近。总结一下,这就是它的样子:

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代表我完全是一个完整的术语。但是有可能实现这样的目标吗?

POST-SOLVING编辑:

感谢电气耶稣,我的匹配与多边形完美配合,因为圆圈不是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 ]
  };
}

你走了。我真的不知道我应该使用多少方面,所以我也为此留下了一个论据。从那里,你可以使用电气耶稣的答案来获得我要去的地方。不要忘记在多边形上放置一个2dsphere索引!

Circles._ensureIndex({'polygonConversion': "2dsphere"});

3 个答案:

答案 0 :(得分:0)

您可以尝试加性加权​​的voronoi图。距离函数是euklidian距离减去重量。权重较大的网站和附近的其他网站会被分类到同一个单元格中。

答案 1 :(得分:0)

Mongo的地理空间运算符包括$centerSphere,它返回一个圆圈范围内的文档:

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

答案 2 :(得分:0)

不,根据文档的“半径”,Geo索引永远不会以您要求它为动态的方式工作。您必须将圈子转换为Polygon几何图形,并使用$ geoIntersects查询查找哪个圆(多边形几何体)与当前位置/位置参数(点几何体)相交。

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