我最近尋找這個,過了一段時間,我發現以下內容。也許別人會用這個..
$ geoIntersects現在在mongoid 4.0.0.beta1實現,但沒有很好的記載。我發現這個在產地的changelog: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)和來源(2.1.0) 我有一個模型多邊形
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
和模型祿
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
此代碼返回內部有這點所有多邊形。
可能有更多優雅的方式來做到這一點。如果是的話,我想聽聽:)
根據杜蘭,支持$ geoIntersects將在Mongoid 4.0版本。 – chrishol 2013-04-03 21:12:47
4.0已發佈,但我沒有在更改日誌中看到'$ geoIntersects' :(https://github.com/mongoid/mongoid/blob/master/CHANGELOG.md – oyatek 2013-05-12 16:02:06
還是未發佈?:)我在changelog中看到4.0,但是4.0不能下載 – oyatek 2013-05-13 10:00:50