如何使用Mongoid执行$ geoIntersects查询?

时间:2013-03-21 21:11:03

标签: mongodb sinatra mongoid geospatial

我正在使用Sinatra和 mongoid 驱动程序,现在我正在尝试在mongoid中执行此查询,实际上我有一个名为' geometry 的地理空间(Polygon)字段“:

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

实际上这个查询适用于mongodb shell。

但是,我想找到与给定点(多边形点)与mongoid或其他红宝石驱动程序相交的状态。

非常感谢任何帮助。

感谢。

3 个答案:

答案 0 :(得分:5)

我最近在搜索这个,过了一会儿我发现了以下内容。也许其他人会对此有用..

$ geoIntersects现在在mongoid 4.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

此代码返回内部包含此点的所有多边形。

可能有更优雅的方式来做到这一点。如果是这样,我想听听它:)

答案 1 :(得分: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字段值的哈希数组,但您可以根据需要进行配置。

答案 2 :(得分:1)

虽然我们正在等待添加 $ geoIntersects 支持的Mongoid 4.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]]]})
相关问题