2013-03-21 138 views
5

我使用西納特拉和mongoid司機,現在我想執行mongoid此查詢,其實我有一個地理空間稱爲「幾何」(多邊形)字段:

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

其實這查詢在mongodb shell中工作。

但是,我想找到與給定點(Point-in-polygon)與mongoid或其他ruby驅動程序相交的狀態。

任何幫助將不勝感激。

謝謝。

回答

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字段的值散列的數組,但你願意,你可以配置此。

+0

根據杜蘭,支持$ geoIntersects將在Mongoid 4.0版本。 – chrishol 2013-04-03 21:12:47

+0

4.0已發佈,但我沒有在更改日誌中看到'$ geoIntersects' :(https://github.com/mongoid/mongoid/blob/master/CHANGELOG.md – oyatek 2013-05-12 16:02:06

+0

還是未發佈?:)我在changelog中看到4.0,但是4.0不能下載 – oyatek 2013-05-13 10:00:50

1

雖然我們正在等待Mongoid 4.0加入$ geoIntersects支持,我自己添加了它。它允許鏈接和所有其他酷東西。找到這個文件(你的路徑可能看起來有點不同):

/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]]]}) 
+0

我創建了一個fork [這裏](https://github.com/GovSciences/origin/tree/geo_intersects),它增加了這個功能。你可以在Gemfile中使用它,如'gem'origin',git:「https://github.com/GovSciences/origin.git」,分支:「geo_intersects」' – 2015-05-25 20:23:35

5

我最近尋找這個,過了一段時間,我發現以下內容。也許別人會用這個..

$ 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 

此代碼返回內部有這點所有多邊形。

可能有更多優雅的方式來做到這一點。如果是的話,我想聽聽:)

+1

格式的原因是它使用GeoJSON。請參閱https://docs.mongodb.com/manual/reference/geojson/#polygon – 2016-05-12 01:20:07