ruby – 如何找到中心圆半径的所有重叠圆?
发布时间:2020-12-17 03:36:21 所属栏目:百科 来源:网络整理
导读:如何在mongo shell中进行交叉或重叠查询 – 哪些圆圈与我的搜索区域重叠?内部仅与中心位置相关,但不包括搜索范围内其他圆的半径. 蒙戈: # My bad conception:var search = [[30,30],10]db.places.find({circle : {"$within" : {"$center" : [search]}}})
如何在mongo
shell中进行交叉或重叠查询 – 哪些圆圈与我的搜索区域重叠?内部仅与中心位置相关,但不包括搜索范围内其他圆的半径.
蒙戈: # My bad conception: var search = [[30,30],10] db.places.find({circle : {"$within" : {"$center" : [search]}}}) 现在我只能获得中心点内的这个圆圈位于搜索区域的圆圈中: ruby: # field :circle,type: Circle # eg. [ [ 30,30 ],10 ] field :radius,type: Integer field :location,:type => Array,:spatial => true spatial_index :location Places.within_circle(location: [ [ 30,10 ]) # {"$query"=>{"location"=>{"$within"=>{"$center"=>[[30,10]}}} 我创建了具有附加位置(特殊索引)和半径而不是圆的示例数据,因为mongodb geo index不支持circle: { "_id" : 1,"name" : "a","circle" : [ [ 5,5 ],40 ],"latlng" : [ 5,"radius" : 40 } { "_id" : 2,"name" : "b","circle" : [ [ 10,10 ],"latlng" : [ 10,"radius" : 5 } { "_id" : 3,"name" : "c","circle" : [ [ 20,20 ],"latlng" : [ 20,"radius" : 5 } { "_id" : 4,"name" : "d","circle" : [ [ 30,50 ],"latlng" : [ 30,"radius" : 50} { "_id" : 5,"name" : "e","circle" : [ [ 80,80 ],"latlng" : [ 80,"radius" : 30} { "_id" : 6,"name" : "f","radius" : 20} 期望的查询结果: { "_id" : 1,"radius" : 40 } { "_id" : 3,"radius" : 30} 下面的解决方案假设我得到所有行,然后在ruby侧过滤我的半径,但它只返回: { "_id" : 4,"radius" : 50} 解决方法
我不熟悉mongodb,但我认为在[[x,y],r]值中意味着
假设您有一个圆圈S,这是您的搜索和一个随机圆圈A.然后您可以计算两个圆圈中心(S.center和A.center)之间的距离,看看它是否低于两个圆圈半径(Sr Ar) ). def distance_between(a,b) ((b.first - a.first)**2 + (b.last - a.last)**2)**0.5 end elements = [{ _id: 1,name: "a",circle: [ [ 5,40 ] },{ _id: 2,name: "b",circle: [ [ 10,5 ] },{ _id: 3,name: "c",circle: [ [ 20,{ _id: 4,name: "d",circle: [ [ 30,50 ] },{ _id: 5,name: "e",circle: [ [ 80,30 ] },{ _id: 6,name: "f",20 ] }] search = [[30,10] elements.select do |elem| circle = elem[:circle] distance_between(circle.first,search.first) <= circle.last + search.last end #{:_id=>1,:name=>"a",:circle=>[[5,5],40]} #{:_id=>3,:name=>"c",:circle=>[[20,20],5]} #{:_id=>4,:name=>"d",:circle=>[[30,50]} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- ruby-on-rails – 破坏Ruby on Rails神话
- ios – Cocoapods project.pbxproj合并冲突
- ArrayCollection与FLEX中的Vector对象
- c# – ServiceStack在没有MVC的情况下启用Razor
- swift – 使用files数组上传MultipartFormData
- 【杂项】Activity中通过 ctrl+鼠标单击 直接链接
- Oracle 补丁那些事儿(PS、PSU、CPU、SPU、BP、DB
- 在Visual C 2013中,x64版本的浮动负无穷大的std
- swift – 从没有抛出的继承函数中抛出异常
- logback.xml配置
热点阅读