class Delivery < ActiveRecord::Baseset_rgeo_factory_for_column(:area_poly, RGeo::Geographic.spherical_factory(:srid => 4326))end
add_column :deliveries, :area_poly, :polygon, :geographic => trueadd_index :deliveries, :area_poly, :spatial => true
t.polygon :region, :srid => 3785
FACTORY = RGeo::Geographic.simple_mercator_factory set_rgeo_factory_for_column(:region, FACTORY.projection_factory)
class Delivery < ActiveRecord::Base
geo_factory = RGeo::Geographic.simple_mercator_factory
set_rgeo_factory_for_column(:area_poly, geo_factory.projection_factory)
end
irb(main):003:0> d = Delivery.first
Delivery Load (0.5ms) SELECT "deliveries".* FROM "deliveries" LIMIT 1
=> #<Delivery id: 190, created_at: "2012-07-23 20:47:08", updated_at: "2012-07-23 20:47:08", establishment_id: 1, area_name: "area sem nome", color: "#1E90FF", fee: #<BigDecimal:7fad75c8e3b0,'0.0',9(18)>, area_poly: #<RGeo::Cartesian::PolygonImpl:0x3fd6bac1c070 "POLYGON ((-53.81023368203125 -29.68610556264785, -53.80130729042969 -29.713541939011943, -53.76388511025391 -29.713243757817693, -53.7631984647461 -29.683421166953423, -53.74088248574219 -29.67506925523435, -53.7686916288086 -29.656573267686216, -53.780707925195316 -29.678947014819027, -53.78551444375 -29.655379861388898, -53.80577048623047 -29.66582168658083, -53.81057700478516 -29.647622375495548, -53.82705649697266 -29.67775387395295, -53.83804282509766 -29.689088140444216, -53.81023368203125 -29.68610556264785))">>
irb(main):004:0> d.area_poly.area
RGeo::Error::UnsupportedOperation: Method Surface#area not defined.
from /Users/danieldocki/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rgeo-0.3.14/lib/rgeo/feature/surface.rb:85:in `area'
from (irb):4
from /Users/danieldocki/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
from /Users/danieldocki/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
from /Users/danieldocki/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
irb(main):005:0>
create_table "deliveries", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.spatial "area_poly", :limit => {:srid=>3785, :type=>"polygon"}
end
add_index "deliveries", ["area_poly"], :name => "index_deliveries_on_area_poly", :spatial => true
% irb
ruby-1.9.3-p0 :001 > require 'rgeo'
=> true
ruby-1.9.3-p0 :002 > RGeo::Geos.supported?
=> true
ruby-1.9.3-p0 :003 > RGeo::CoordSys::Proj4.supported?
=> true
ruby-1.9.3-p0 :004 > puts "Hooray!"
Hooray!
=> nil
ruby-1.9.3-p0 :005 >
Otherwise review the gem install (Daniel's series part 2)
> RGeo::Geos.supported?
=> false
> RGeo::Geos.supported?
=> true
=> #<Delivery id: 193, created_at: "2012-07-24 00:19:38", updated_at: "2012-07-24 00:19:38", establishment_id: 1, area_name: "area sem nome", color: "#1E90FF", fee: #<BigDecimal:7fd970f72780,'0.0',9(18)>, area_poly: #<RGeo::Geos::CAPIPolygonImpl:0x3fecb87bbe54 "POLYGON ((-53.81704735334472 -29.6765678441966, -53.80880760725097 -29.726966297749417, -53.70409416730956 -29.737400685635574, -53.66289543684081 -29.674479788979816, -53.72572350080566 -29.65777378643096, -53.74014305646972 -29.67477808523697, -53.74769615705566 -29.65986218847794, -53.75730919416503 -29.675672968698954, -53.76314568098144 -29.660757204657195, -53.768982167797844 -29.671496777739407, -53.778938527661126 -29.65807213223437, -53.81704735334472 -29.6765678441966))">>
irb(main):004:0> d.area_poly.area
=> 0.008779721810393215
irb(main):011:0> d = Delivery.find(209) Delivery Load (0.6ms) SELECT "deliveries".* FROM "deliveries" WHERE "deliveries"."id" = $1 LIMIT 1 [["id", 209]]
=> #<Delivery id: 209, created_at: "2012-07-24 18:37:58", updated_at: "2012-07-24 18:37:58", establishment_id: 1, area_name: "area sem nome", color: "#1E90FF", fee: #<BigDecimal:7fbc0947dd68,'0.0',9(18)>, area_poly: nil>
irb(main):012:0> d.area_poly = "POLYGON((-53.7800212796875 -29.668804866149454, -53.78791770302735 -29.689088140444216, -53.753242104882816 -29.68968464538161, -53.74534568154297 -29.667611604939303))"
=> "POLYGON((-53.7800212796875 -29.668804866149454, -53.78791770302735 -29.689088140444216, -53.753242104882816 -29.68968464538161, -53.74534568154297 -29.667611604939303))"
irb(main):013:0> d.save
(0.2ms) BEGIN
(0.2ms) COMMIT
=> true
irb(main):014:0> d
=> #<Delivery id: 209, created_at: "2012-07-24 18:37:58", updated_at: "2012-07-24 18:37:58", establishment_id: 1, area_name: "area sem nome", color: "#1E90FF", fee: #<BigDecimal:7fbc0913ed00,'0.0',9(18)>, area_poly: nil>
irb(main):015:0>