2012-11-17 38 views
9

के साथ जिओकोडर मणि और पोस्टजीआईएस डेटाबेस के साथ जियोकोडिंग पते मैं पते और निर्देशांक देखने के लिए भूगर्भिक मणि का उपयोग करने की कोशिश कर रहा हूं। मैं चाहता हूं कि यह मेरे पोस्टजीआईएस स्थानिक डेटाबेस और आरजीओ मणि के साथ मिलकर काम करे, जो अक्षांश और देशांतर मूल्यों को अलग से बचाने के बजाय POINT सुविधाओं का उपयोग करता है।आरजीओ

class Location < ActiveRecord::Base 
    attr_accessible :latlon, :name, :address 
    set_rgeo_factory_for_column(:latlon, RGeo::Geographic.spherical_factory(:srid => 4326)) 

    geocoded_by :name_and_address do |obj, results| 
    if geo = results.first 
     obj.latlon = Location.rgeo_factory_for_column(:latlon).point(geo.longitude, geo.latitude) 
    end 
    end 

    after_initialize :init 
    after_validation :geocode 


    def init 
    self.latlon ||= Location.rgeo_factory_for_column(:latlon).point(0, 0) 
    end 

    def latitude 
    self.latlon.lat 
    end 

    def latitude=(value) 
    lon = self.latlon.lon 
    self.latlon = Location.rgeo_factory_for_column(:latlon).point(lon, value) 
    end 

    def longitude 
    self.latlon.lon 
    end 

    def longitude=(value) 
    lat = self.latlon.lat 
    self.latlon = Location.rgeo_factory_for_column(:latlon).point(value, lat) 
    end 

    def name_and_address 
    "#{self.name}, #{self.address}" 
    end 

end 

रेल सांत्वना मैं अब क्या कर सकते हैं में:

test = Location.new(name: "Eiffel Tower") // => #<Location id: nil, name: "Eiffel Tower", latlon: #<RGeo::Geographic::SphericalPointImpl:0x81579974 "POINT (0.0 0.0)">, created_at: nil, updated_at: nil, address: nil> 
test.geocode // => #<RGeo::Geographic::SphericalPointImpl:0x81581ac0 "POINT (2.294254 48.858278)"> 

कमाल तो मैं अपने मॉडल में काम करने के लिए एक RGeo सूत्री सुविधा में जियोकोडर देखने के परिणामों को सहेजने की कोशिश की है! हालांकि, जब मैं अपने परीक्षण स्थान है, जो :geocode के लिए :after_validation कॉल से चलाता सहेजना चाहते मैं एक त्रुटि मिलती है:

NoMethodError: undefined method `lon' for nil:NilClass 
    from /Users/sg/rails-projects/geo_rails_test/app/models/location.rb:24:in `latitude=' 
    from /Users/sg/rails-projects/geo_rails_test/app/models/location.rb:7:in `block in <class:Location>' 
    from /Users/sg/.rvm/gems/ruby-1.9.3-p194/gems/geocoder-1.1.2/lib/geocoder/stores/base.rb:108:in `call' 
    from /Users/sg/.rvm/gems/ruby-1.9.3-p194/gems/geocoder-1.1.2/lib/geocoder/stores/base.rb:108:in `do_lookup' 
    from /Users/sg/.rvm/gems/ruby-1.9.3-p194/gems/geocoder-1.1.2/lib/geocoder/stores/active_record.rb:278:in `geocode' 
    from /Users/sg/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `_run__1498365873506454431__validation__51840359655181017__callbacks' 
    [...] 

ऐसा लगता है कि geocode कॉलबैक RGeo वस्तु को हटा देता है ताकि उसके तरीकों में बनाया गया है और साथ ही मेरे गेटर और सेटर विधियां अब उपलब्ध नहीं हैं। क्यों? कोई विचार?

उत्तर

6

यह एक खूनी टाइपो था ...

यह काम करता है। मैं इसे छोड़ देता हूं, शायद किसी और के पास एक ही समस्या है या बेहतर समाधान है ...

मैंने reverse_geocode केस भी जोड़ा है।

अब देखो यह क्या कर सकते हैं:

1.9.3p194 :310 > loc = Location.new(name: "Vatikan") 
=> #<Location id: nil, name: "Vatikan", latlon: #<RGeo::Geographic::SphericalPointImpl:0x8148add8 "POINT (0.0 0.0)">, created_at: nil, updated_at: nil, address: nil> 
1.9.3p194 :311 > loc.save 
    (0.2ms) BEGIN 
    SQL (0.6ms) INSERT INTO "locations" ("address", "created_at", "latlon", "name", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["address", "Via Pio X, 00120, Vatican City"], ["created_at", Sat, 17 Nov 2012 19:26:59 UTC +00:00], ["latlon", #<RGeo::Geographic::SphericalPointImpl:0x81570554 "POINT (12.453389 41.902916)">], ["name", "Vatikan"], ["updated_at", Sat, 17 Nov 2012 19:26:59 UTC +00:00]] 
    (0.9ms) COMMIT 
=> true 

मैं इस प्यार! :-)