How find all overlapping circles from radius of central circle?

mongodb, mongodb-query, mongoid3, ruby

Solution

I'm not familiar with mongodb but I assume that in [[x, y], r] values mean

x: value of the center on the axis x. y: value of the center on the axis y. r: circle radius.

Say you have circle S which is your search and a random circle A. Then you could calcule the distance between both circles' center (S.center and A.center) and see if it is inferior to those both circles radius added (S.r + A.r).

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, 5 ], 40 ] },
            { _id: 2, name: "b", circle: [ [ 10, 10 ], 5 ] },
            { _id: 3, name: "c", circle: [ [ 20, 20 ], 5 ] },
            { _id: 4, name: "d", circle: [ [ 30, 30 ], 50 ] },
            { _id: 5, name: "e", circle: [ [ 80, 80 ], 30 ] },
            { _id: 6, name: "f", circle: [ [ 80, 80 ], 20 ] }]
search = [[30, 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, 30], 50]}

Problem

How to do an intersection or overlap query in mongo shell - what circles overlap my search region? `Within` relate only to the center position but doesn't include radius of the other circles in searched scope. Mongo: ``` # My bad conception: var search = [[30, 30], 10] db.places.find({circle : {"$within" : {"$center" : [search]}}}) ``` Now I can obtain only this circles within central point lies in searched area of circle: 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, 30 ], 10 ]) # {"$query"=>{"location"=>{"$within"=>{"$center"=>[[30, 30], 10]}}} ``` I created example data with additional location (special index) and radius instead circle because circle isn't supported by mongodb geo index: ``` { "_id" : 1, "name" : "a", "circle" : [ [ 5, 5 ], 40 ], "latlng" : [ 5, 5 ], "radius" : 40 } { "_id" : 2, "name" : "b", "circle" : [ [ 10, 10 ], 5 ], "latlng" : [ 10, 10 ], "radius" : 5 } { "_id" : 3, "name" : "c", "circle" : [ [ 20, 20 ], 5 ], "latlng" : [ 20, 20 ], "radius" : 5 } { "_id" : 4, "name" : "d", "circle" : [ [ 30, 30 ], 50 ], "latlng" : [ 30, 30 ], "radius" : 50} { "_id" : 5, "name" : "e", "circle" : [ [ 80, 80 ], 30 ], "latlng" : [ 80, 80 ], "radius" : 30} { "_id" : 6, "name" : "f", "circle" : [ [ 80, 80 ], 20 ], "latlng" : [ 80, 80 ], "radius" : 20} ``` Desired query result: ``` { "_id" : 1, "name" : "a", "circle" : [ [ 5, 5 ], 40 ], "latlng" : [ 5, 5 ], "radius" : 40 } { "_id" : 3, "name" : "c", "circle" : [ [ 20, 20 ], 5 ], "latlng" : [ 20, 20 ], "radius" : 5 } { "_id" : 4, "name" : "d", "circle" : [ [ 30, 30 ], 50 ], "latlng" : [ 30, 30 ], "radius" : 50} { "_id" : 5, "name" : "e", "circle" : [ [ 80, 80 ], 30 ], "latlng" : [ 80, 80 ], "radius" : 30} ``` Solution below assumes that I get all rows and then filter on the ruby side my radius but it returns only: ``` { "_id" : 4, "name" : "d", "circle" : [ [ 30, 30 ], 50 ], "latlng" : [ 30, 30 ], "radius" : 50} ```

Original source