5

mongo シェルで交差または重複クエリを実行する方法 - 検索領域と重複する円は何ですか? Within中心位置のみに関連し、検索範囲内の他の円の半径は含みません。

モンゴ:

# My bad conception:
var search = [[30, 30], 10]
db.places.find({circle : {"$within" : {"$center" : [search]}}})

これで、円の検索領域にある中心点内の円のみを取得できます。

ルビー:

# 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]}}}

mongodb geo インデックスでは円がサポートされていないため、追加の場所 (特別なインデックス) と円の代わりに半径を使用してサンプル データを作成しました。

{ "_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}

望ましいクエリ結果:

{ "_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}

以下の解決策は、すべての行を取得してから、ルビー側で半径をフィルタリングすると想定していますが、返されるのは次のとおりです。

{ "_id" : 4, "name" : "d", "circle" : [ [ 30, 30 ], 50 ], "latlng" : [ 30, 30 ], "radius" : 50}
4

2 に答える 2

5

私はmongodbに精通していませんが、[[x、y]、r]の値は

x: x 軸上の中心の値。y: 軸 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, 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]}
于 2012-10-15T14:33:34.433 に答える
3

残念ながら、Mongo には現在、オーバーラップしているオブジェクトについて直接クエリを実行する機能はなく、オブジェクト内のポイントのみです。

@oldergod の回答では、2 つの円が重なるかどうかを計算するアルゴリズムについて説明しています。

その計算に基づくシェルでの回避策を次に示します。

function distance(a, b) {
    return Math.pow(Math.pow(a[0] - b[0], 2) + Math.pow(a[1] - b[1], 2), 0.5);
}

コレクション「サークル」に挿入されたサンプルデータについて:

> db.circle.find().forEach(
    function(c) {  
        if ( (distance(c.latlng, search.latlng) < c.radius + search.radius) ) 
          {   
               print(c.name); 
          } 
    } )
a
c
d
> 
于 2012-10-20T22:55:14.713 に答える