getting polygon area

703 views
Skip to first unread message

Daniel Docki

unread,
Jul 23, 2012, 12:06:11 PM7/23/12
to rgeo-...@googlegroups.com
Hi There,

I have this model

class Delivery < ActiveRecord::Base
  set_rgeo_factory_for_column(:area_poly, RGeo::Geographic.spherical_factory(:srid => 4326))
end

Migration
    add_column :deliveries, :area_poly, :polygon, :geographic => true
    add_index :deliveries, :area_poly, :spatial => true

I need getting area total polygon, how I do that? I don't know if migration and model is correct, because of this http://www.daniel-azuma.com/blog/archives/191

t.polygon :region, :srid => 3785
FACTORY = RGeo::Geographic.simple_mercator_factory
 set_rgeo_factory_for_column(:region, FACTORY.projection_factory)

Thanks

Erik Benoist

unread,
Jul 23, 2012, 4:23:55 PM7/23/12
to rgeo-...@googlegroups.com
Daniel-
If you have a polygon that is projected correctly, I would suggest using 3785 (web mercator) or some other true projection in the unit of measurement you need. With that set up it should be as simple as calling area_poly.area. Rgeo::Feature::Polygon inherits from a Surface which will give you the area method.

So the setup should look something like this:

class Delivery < ActiveRecord::Base
geo_factory = RGeo::Geographic.simple_mercator_factory set_rgeo_factory_for_column(:area_poly, geo_factory.projection_factory)
end

Migration:
class Deliveries < ActiveRecord::Migration
def change
create_table :deliveries, {:primary_key => :id} do |t|


t.polygon :area_poly, :geometry => true, :srid => 3785
end
add_index(:deliveries, :area_poly, :spatial => true)
end
end


--
Erik Benoist

Daniel Docki

unread,
Jul 23, 2012, 4:52:27 PM7/23/12
to rgeo-...@googlegroups.com
I'm sorry Erik, I did what you told me and not working =/

 
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


Thanks,

Daniel Docki
Skype: daniel.docki
MSN/Gtalk: daniel...@gmail.com
Twitter: http://twitter.com/danieldocki

Ernesto Echeverria

unread,
Jul 24, 2012, 11:27:45 AM7/24/12
to rgeo-...@googlegroups.com
Method not defined?

Have you checked for the libraries?

% 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)

Daniel Docki

unread,
Jul 24, 2012, 12:36:34 PM7/24/12
to rgeo-...@googlegroups.com
Ernesto,

> RGeo::Geos.supported?
=> false

I reviewed the rgeo install ... and I followed this https://github.com/dazuma/rgeo/issues/6#issuecomment-2868865

> 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

Thanks working fine :)

Daniel Docki
Skype: daniel.docki
MSN/Gtalk: daniel...@gmail.com
Twitter: http://twitter.com/danieldocki


Daniel Docki

unread,
Jul 24, 2012, 2:45:20 PM7/24/12
to rgeo-...@googlegroups.com
I'm sorry guys, but now not store polygon in database

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> 

What is wrong?
Before was storing polygon in database

Daniel Docki
Skype: daniel.docki
MSN/Gtalk: daniel...@gmail.com
Twitter: http://twitter.com/danieldocki


Jose Ernesto Echeverria

unread,
Jul 24, 2012, 11:25:36 PM7/24/12
to rgeo-...@googlegroups.com
I don't know how it used to work before, when you have something as:

 "POLYGON((-53.7800212796875 -29.668804866149454, -53.78791770302735 -29.689088140444216, -53.753242104882816 -29.68968464538161, -53.74534568154297 -29.667611604939303))"

that is just a string. The way I've seen it is something like

[14] pry(main)> real_polygon = FACTORY.parse_wkt("POLYGON((-53.7800212796875 -29.668804866149454, -53.78791770302735 -29.689088140444216, -53.753242104882816 -29.68968464538161, -53.74534568154297 -29.667611604939303))")
=> #<RGeo::Geographic::ProjectedPolygonImpl:0x3fda2b193238 "POLYGON ((-53.7800212796875 -29.668804866149454, -53.78791770302735 -29.689088140444216, -53.753242104882816 -29.68968464538161, -53.74534568154297 -29.667611604939303, -53.7800212796875 -29.668804866149454))">

Making sure you use the proper factory for the data. Are the coordinates of your polygon already projected? How did you get them to be like that? In my project data comes in lat-lon, so the overall process for obtaining polygons is not so direct, but actually have to take care of the single points, then create lines, line_strings, linear_rings and eventually create polygons that will be stored using similar settings than yours. Doing it step by step allows me also to ensure proper data quality (such as not complex linear_rings)

It is better if you go step by step, and in the way try to use a visualization (like gmaps4rails) so that you are completely sure of what you are doing. Otherwise you could end up with a working model with the wrong data.

Take care.
--
José Ernesto Echeverría
Tel +57 321 4900227 (Bogotá, Colombia)


Daniel Docki

unread,
Jul 29, 2012, 5:21:20 PM7/29/12
to rgeo-...@googlegroups.com
Erik, thank you that helped a lot.

Daniel Docki
Skype: daniel.docki
MSN/Gtalk: daniel...@gmail.com
Twitter: http://twitter.com/danieldocki


Reply all
Reply to author
Forward
0 new messages