5

Sinatraとmongoidドライバーを使用していますが、このクエリをmongoidで実行しようとしています。実際には、「geometry」という地理空間(ポリゴン)フィールドがあります。

db.states.find({
    geometry: {
        $geoIntersects: {
            $geometry: {
                type: "Point",
                coordinates: [-99.176524, 18.929204]
            }
        }
    }
})

実際、このクエリはmongodbシェルで機能します。

ただし、モンゴイドまたは他のルビードライバーを使用して、指定されたポイント(Point-in-polygon)と交差する状態を見つけたいと思います。

どんな助けでも大歓迎です。

ありがとう。

4

3 に答える 3

5

最近探していたところ、しばらくすると次のようになりました。多分誰か他の人がこれを使うでしょう。

$geoIntersectsは現在mongoid4.0.0.beta1に実装されていますが、十分に文書化されていません。これは、オリジンの変更ログで見つかりました:https ://github.com/mongoid/origin/blob/master/CHANGELOG.md#new-features -1

query.geo_spacial(:location.intersects_line => [[ 1, 10 ], [ 2, 10 ]])
query.geo_spacial(:location.intersects_point => [[ 1, 10 ]])
query.geo_spacial(:location.intersects_polygon => [[ 1, 10 ], [ 2, 10 ], [ 1, 10 ]])
query.geo_spacial(:location.within_polygon => [[ 1, 10 ], [ 2, 10 ], [ 1, 10 ]])

およびコミット:https ://github.com/mongoid/origin/commit/30938fad644f17fe38f62cf90571b78783b900d8

 # Add a $geoIntersects selection. Symbol operators must be used as shown in
 # the examples to expand the criteria.
 #
 # @note The only valid geometry shapes for a $geoIntersects are: :line,
 #   :point, and :polygon.
 # ...
 # @example Add a geo intersect criterion for a point.
 #   query.geo_intersects(:location.point => [[ 1, 10 ]])

私のプロジェクトでは、mongoid(4.0.0.beta1)とorigin(2.1.0)があります。モデルPolygonがあります。

class Polygon
  include Mongoid::Document
  # some fields 

  embeds_many :loc

  # coordinates is an array of two points: [10, 12]
  def find_polygons_with_point(coordinates)
    # This is where the magic happens!
    Polygon.all.geo_spacial(:loc.intersects_point => coordinates)
  end

end

そしてモデルLoc

class Loc
  field :type, type: String #Need to be set to 'Polygon' when creating a new location.
  field :coordinates, type: Array
  # For some reason the array has to be in the format
  # [ [ [1,1], [2,3], [5,3], [1,1] ] ]
  # And the first coordinate needs to be the same as the last
  # to close the polygon

  embedded_in :polygon

  index({ coordinates: "2d" }, { min: -200, max: 200 }) #may not need min/max
end

このコードは、このポイントを内部に持つすべてのポリゴンを返します。

これを行うためのよりエレガントな方法があるかもしれません。もしそうなら、私はそれを聞きたいです:)

于 2014-03-27T14:02:13.213 に答える
3

私は同じことをすることを考えてきました。私が見たところ、これはMongoidでまだサポートされておらず、実装の観点からどのような時間枠であるかわかりません。

それまでの間、Mongoid / Mopedドライバーを使用してクエリを実行できますが、Mongoidが提供するオブジェクトマッピングの優れた機能は得られません。配列/ハッシュが返されるだけです。以下の構文例:

ids = Mongoid.default_session["states"].find( geometry:
    { "$geoIntersects" =>
        { "$geometry" =>
            { type: "Point", coordinates: [-99.176524, 18.929204] }
        }
    }
).select( id: 1 )

これは実際には、キー「_id」と_idフィールドの値を持つハッシュの配列を返しますが、必要に応じてこれを構成できます。

于 2013-04-02T02:29:14.487 に答える
1

$geoIntersectsのサポートが追加されたMongoid4.0を待っている間に、自分で追加しました。それは連鎖と他のすべてのクールなことを可能にします。このファイルを見つけます(パスが少し異なる場合があります):

/usr/local/lib/ruby/gems/1.9.1/gems/origin-1.1.0/lib/origin/selectable.rb

これをファイルの任意の場所に追加します。

def geo_intersects(criterion = nil)
   __override__(criterion, "$geoIntersects")
end
key :geo_intersects, :override, "$geoIntersects"

今、あなたはすることができます:

Houses.where(:color => "red").geo_intersects(:loc => {"$geometry" => {:type => "Polygon", :coordinates => [[[1,2],[2,3][1,2]]]})
于 2013-11-28T06:21:22.583 に答える