2013-03-21 4 views
5

मैं सिनात्रा उपयोग कर रहा हूँ और ड्राइवर mongoid, अब मैं mongoid में इस क्वेरी प्रदर्शन करने के लिए कोशिश कर रहा हूँ, वास्तव में मैं एक भू-स्थानिक है (बहुभुज) क्षेत्र कहा जाता 'ज्यामिति':

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

वास्तव में इस क्वेरी mongodb खोल में काम करता है।

हालांकि, मैं उन राज्यों को ढूंढना चाहता हूं जो मोंगोइड या शायद अन्य रूबी ड्राइवर के साथ दिए गए बिंदु (प्वाइंट-इन-पॉलीगॉन) के साथ छेड़छाड़ करते हैं।

किसी भी मदद की सराहना की जाएगी।

धन्यवाद।

उत्तर

3

मैं वही काम कर रहा हूं। जो मैं देख सकता था, यह अभी तक मोंगोइड में समर्थित नहीं है, और मुझे नहीं पता कि समय सीमा क्या लागू की जा रही है।

इस बीच, आप क्वेरी चलाने के लिए मोंगोइड/मोपेड ड्राइवर का उपयोग कर सकते हैं, लेकिन आपको ऑब्जेक्ट मैपिंग की कोई भी वस्तु मैपॉइड प्रदान नहीं करेगी - आपको केवल सरणी/हैश वापस मिल जाएगी। नीचे दिए गए उदाहरण वाक्य रचना:

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

+0

4.0 जारी किया गया है लेकिन मुझे चेंजलॉग में '$ geoIntersects' नहीं दिखाई देता है :(https://github.com/mongoid/mongoid/blob/master/CHANGELOG.md – oyatek

+0

या जारी नहीं किया गया? :) मैं चेंजलॉग में 4.0 देखता हूं लेकिन 4.0 – oyatek

1

जबकि हम $ geoIntersects समर्थन के साथ मोंगोइड 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]]]}) 
+0

डाउनलोड करने के लिए उपलब्ध नहीं है मैंने एक कांटा [यहां] (https://github.com/GovSciences/origin/tree/geo_intersects) बनाया है जो कार्यक्षमता को जोड़ता है। आप इसे अपने जेमफाइल में 'मणि' उत्पत्ति ', गिट: "https://github.com/GovSciences/origin.git", शाखा: "geo_intersects" ' –

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) और मूल है (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 देखें –

संबंधित मुद्दे