postgis_adapter 0.3.0 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt CHANGED
@@ -7,7 +7,6 @@ init.rb
7
7
  install.rb
8
8
  lib/postgis_adapter.rb
9
9
  lib/postgis_adapter/acts_as_geom.rb
10
- lib/postgis_adapter/acts_as_geom_flymake.rb
11
10
  lib/postgis_adapter/common_spatial_adapter.rb
12
11
  lib/postgis_functions.rb
13
12
  lib/postgis_functions/bbox.rb
data/README.rdoc CHANGED
@@ -5,6 +5,7 @@ in a transparent way (that is like the other base data type columns).
5
5
  It also provides a way to manage these columns in migrations.
6
6
 
7
7
  This fork adds handy methods to make geometrical calculations on postgis.
8
+ Based on http://georuby.rubyforge.org Spatial Adapter
8
9
 
9
10
  Postgis Manual - http://postgis.refractions.net/documentation/manual-svn
10
11
 
@@ -322,5 +323,5 @@ Project Tracker http://nofxx.lighthouseapp.com/projects/20712-postgis_adapter
322
323
 
323
324
  === SpatialAdapter
324
325
 
325
-
326
+ http://georuby.rubyforge.org
326
327
  guilhem.vellut+georuby@gmail.com.
@@ -1,9 +1,12 @@
1
- # #
1
+ #
2
2
  # PostGIS Adapter
3
3
  #
4
4
  # Common Spatial Adapter for ActiveRecord
5
5
  #
6
+ # Code from
7
+ # http://georuby.rubyforge.org Spatial Adapter
6
8
  #
9
+
7
10
  #Addition of a flag indicating if the index is spatial
8
11
  ActiveRecord::ConnectionAdapters::IndexDefinition.class_eval do
9
12
  attr_accessor :spatial
@@ -1,8 +1,9 @@
1
- # #
1
+ #
2
2
  # PostGIS Adapter
3
3
  #
4
- # Spatial Adapter PostGIS Adapter for ActiveRecord
5
4
  #
5
+ # Code from
6
+ # http://georuby.rubyforge.org Spatial Adapter
6
7
  #
7
8
  require 'activerecord'
8
9
  require 'active_record/connection_adapters/postgresql_adapter'
@@ -18,7 +19,7 @@ include GeoRuby::SimpleFeatures
18
19
  include SpatialAdapter
19
20
 
20
21
  module PostgisAdapter
21
- VERSION = '0.3.0'
22
+ VERSION = '0.3.4'
22
23
  end
23
24
 
24
25
  #tables to ignore in migration : relative to PostGIS management of geometric columns
@@ -1,31 +1,30 @@
1
1
  module PostgisFunctions
2
-
3
-
4
- ###
5
- ##
2
+
6
3
  #
7
4
  # Class Methods
8
5
  #
9
- # Falling back to AR here.
10
- #
11
6
  module ClassMethods
12
7
 
13
8
  #
14
- # Returns the closest record
15
- #
16
- def closest_to(p, srid=4326)
17
- find(:first, :order => "ST_Distance(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))" )
9
+ # Returns the closest record
10
+ def closest_to(p, opts = {})
11
+ srid = opts.delete(:srid) || 4326
12
+ opts.merge!(:order => "ST_Distance(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))")
13
+ find(:first, opts)
18
14
  end
19
15
 
20
- def close_to(p, srid=4326)
21
- find(:all, :order => "ST_Distance(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))" )
16
+ #
17
+ # Order by distance
18
+ def close_to(p, opts = {})
19
+ srid = opts.delete(:srid) || 4326
20
+ opts.merge!(:order => "ST_Distance(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))")
21
+ find(:all, opts)
22
22
  end
23
23
 
24
- #
25
- #
26
- #
27
- def by_length sort='asc'
28
- find(:all, :order => "ST_length(geom) #{sort}" )
24
+ def by_length opts = {}
25
+ sort = opts.delete(:sort) || 'asc'
26
+ opts.merge!(:order => "ST_length(geom) #{sort}")
27
+ find(:all, opts)
29
28
  end
30
29
 
31
30
  def longest
@@ -58,7 +57,7 @@ module PostgisFunctions
58
57
  end
59
58
 
60
59
  end
61
-
62
-
63
-
64
- end
60
+
61
+
62
+
63
+ end
@@ -379,7 +379,7 @@ module PostgisFunctions
379
379
  #
380
380
  # Returns Geometry ST_Polygonize(geometry set geomfield);
381
381
  #
382
- def polygonize#(geom)
382
+ def polygonize
383
383
  postgis_calculate(:polygonize, self)
384
384
  end
385
385
 
@@ -437,8 +437,12 @@ module PostgisFunctions
437
437
  #
438
438
  # Return Geometry ST_Transform(geometry g1, integer srid);
439
439
  #
440
+ def transform!(new_srid)
441
+ self[get_column_name] = postgis_calculate("Transform", self, new_srid)
442
+ end
443
+
440
444
  def transform(new_srid)
441
- postgis_calculate("Transform", self, new_srid)
445
+ dup.transform!(new_srid)
442
446
  end
443
447
 
444
448
  #
@@ -453,6 +457,52 @@ module PostgisFunctions
453
457
  postgis_calculate("segmentize", self, max_length)
454
458
  end
455
459
 
460
+ #
461
+ # Returns the instance`s geom srid
462
+ #
463
+ def srid
464
+ self[get_column_name].srid
465
+ end
466
+
467
+ #
468
+ # Return UTM Zone for a geom
469
+ #
470
+ # Return Integer
471
+ def utm_zone
472
+ if srid == 4326
473
+ geom = centroid
474
+ else
475
+ geomdup = transform(4326)
476
+ mezzo = geomdup.length / 2
477
+ geom = case geomdup
478
+ when Point then geomdup
479
+ when LineString then geomdup[mezzo]
480
+ else
481
+ geomgeog[mezzo][geomgeo[mezzo]/2]
482
+ end
483
+
484
+ end
485
+
486
+ pref = geom.y > 0 ? 32700 : 32600
487
+ zone = ((geom.x + 180) / 6 + 1).to_i
488
+ zone + pref
489
+ end
490
+
491
+ #
492
+ # Returns the Geometry in its UTM Zone
493
+ #
494
+ # Return Geometry
495
+ def to_utm!(utm=nil)
496
+ utm ||= utm_zone
497
+ self[get_column_name] = transform(utm)
498
+ end
499
+
500
+ def to_utm
501
+ dup.to_utm!
502
+ end
503
+
504
+ #
505
+ #
456
506
  #
457
507
  # LINESTRING
458
508
  #
@@ -623,15 +673,24 @@ module PostgisFunctions
623
673
  end
624
674
 
625
675
 
626
- ####
627
- ###
628
- ##
676
+ #
677
+ #
678
+ #
629
679
  #
630
680
  # POINT
631
681
  #
632
682
  #
683
+ #
684
+ #
633
685
  module PointFunctions
634
686
 
687
+ #
688
+ # Some nice getters
689
+ #
690
+ def x; @x ||= self[get_column_name].x; end
691
+ def y; @y ||= self[get_column_name].y; end
692
+ def z; @z ||= self[get_column_name].z; end
693
+
635
694
  #
636
695
  # Returns a float between 0 and 1 representing the location of the closest point
637
696
  # on LineString to the given Point, as a fraction of total 2d line length.
@@ -710,12 +769,15 @@ module PostgisFunctions
710
769
 
711
770
  end
712
771
 
713
- ###
714
- ##
772
+ #
773
+ #
774
+ #
715
775
  #
716
776
  # Polygon
717
777
  #
718
778
  #
779
+ #
780
+ #
719
781
  module PolygonFunctions
720
782
 
721
783
  #
@@ -11,13 +11,13 @@
11
11
  #
12
12
  # Some links:
13
13
  #
14
- # PostGis Manual - http://postgis.refractions.net/documentation/manual-svn/ch07.html
14
+ # PostGis Manual - http://postgis.refractions.net/documentation/manual-svn/ch07.html
15
15
  # Earth Spheroid - http://en.wikipedia.org/wiki/Figure_of_the_Earth
16
16
  #
17
- #
17
+
18
18
  module PostgisFunctions
19
+ # WGS84 Spheroid
19
20
  EARTH_SPHEROID = "'SPHEROID[\"GRS-80\",6378137,298.257222101]'" # SRID => 4326
20
- #EARTH_SPHEROID = "'SPHEROID[\"IERS_2003\",6378136.6,298.25642]'" # SRID =>
21
21
 
22
22
  def postgis_calculate(operation, subjects, options = {})
23
23
  subjects = [subjects] unless subjects.respond_to?(:map)
@@ -29,24 +29,24 @@ module PostgisFunctions
29
29
  private
30
30
 
31
31
  def get_column_name
32
- @geo_column ||= postgis_geoms[:columns].first
32
+ @geo_column ||= postgis_geoms[:columns]
33
33
  end
34
34
 
35
35
  #
36
- # Construct the postgis sql query
36
+ # Construct the PostGIS SQL query
37
37
  #
38
- # Area return in square feet
39
- # Distance/DWithin/Length/Perimeter in projected units.
40
- # DistanceSphere/Spheroid in meters.
38
+ # Returns:
39
+ # Area/Distance/DWithin/Length/Perimeter => projected units
40
+ # DistanceSphere/Spheroid => meters
41
41
  #
42
42
  def construct_geometric_sql(type,geoms,options)
43
43
 
44
44
  tables = geoms.map do |t| {
45
- :class => t.class.to_s.downcase.pluralize,
45
+ :name => t.class.table_name,
46
46
  :uid => unique_identifier,
47
47
  :id => t[:id] }
48
48
  end
49
-
49
+
50
50
  # Implement a better way for options?
51
51
  if options.instance_of? Hash
52
52
  transform = options.delete(:transform)
@@ -56,7 +56,7 @@ module PostgisFunctions
56
56
  fields = tables.map { |f| "#{f[:uid]}.#{get_column_name}" } # W1.geom
57
57
  fields.map! { |f| "ST_Transform(#{f}, #{transform})" } if transform # ST_Transform(W1.geom,x)
58
58
  conditions = tables.map { |f| "#{f[:uid]}.id = #{f[:id]}" } # W1.id = 5
59
- tables.map! { |f| "#{f[:class]} #{f[:uid]}" } # streets W1
59
+ tables.map! { |f| "#{f[:name]} #{f[:uid]}" } # streets W1
60
60
 
61
61
  #
62
62
  # Data => SELECT Func(A,B)
@@ -149,7 +149,7 @@ end
149
149
  #ST_AsGML
150
150
  #ST_AsKML
151
151
  #ST_AsSVG
152
- #
152
+ # #EARTH_SPHEROID = "'SPHEROID[\"IERS_2003\",6378136.6,298.25642]'" # SRID =>
153
153
  # def distance_convert(value, unit, from = nil)
154
154
  # factor = case unit
155
155
  # when :km, :kilo then 1
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{postgis_adapter}
5
- s.version = "0.3.0"
5
+ s.version = "0.3.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Marcos Piccinini"]
9
- s.date = %q{2009-03-09}
9
+ s.date = %q{2009-04-13}
10
10
  s.description = %q{Postgis Adapter for Activer Record}
11
11
  s.email = ["x@nofxx.com"]
12
12
  s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"]
13
- s.files = ["History.txt", "MIT-LICENSE", "Manifest.txt", "README.rdoc", "Rakefile", "init.rb", "install.rb", "lib/postgis_adapter.rb", "lib/postgis_adapter/acts_as_geom.rb", "lib/postgis_adapter/acts_as_geom_flymake.rb", "lib/postgis_adapter/common_spatial_adapter.rb", "lib/postgis_functions.rb", "lib/postgis_functions/bbox.rb", "lib/postgis_functions/class.rb", "lib/postgis_functions/common.rb", "postgis_adapter.gemspec", "rails/init.rb", "script/console", "script/destroy", "script/generate", "spec/db/database_postgis.yml", "spec/db/models_postgis.rb", "spec/db/schema_postgis.rb", "spec/postgis_adapter/acts_as_geom_spec.rb", "spec/postgis_adapter/common_spatial_adapter_spec.rb", "spec/postgis_adapter_spec.rb", "spec/postgis_functions/bbox_spec.rb", "spec/postgis_functions/class_spec.rb", "spec/postgis_functions/common_spec.rb", "spec/postgis_functions_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "uninstall.rb"]
13
+ s.files = ["History.txt", "MIT-LICENSE", "Manifest.txt", "README.rdoc", "Rakefile", "init.rb", "install.rb", "lib/postgis_adapter.rb", "lib/postgis_adapter/acts_as_geom.rb", "lib/postgis_adapter/common_spatial_adapter.rb", "lib/postgis_functions.rb", "lib/postgis_functions/bbox.rb", "lib/postgis_functions/class.rb", "lib/postgis_functions/common.rb", "postgis_adapter.gemspec", "rails/init.rb", "script/console", "script/destroy", "script/generate", "spec/db/database_postgis.yml", "spec/db/models_postgis.rb", "spec/db/schema_postgis.rb", "spec/postgis_adapter/acts_as_geom_spec.rb", "spec/postgis_adapter/common_spatial_adapter_spec.rb", "spec/postgis_adapter_spec.rb", "spec/postgis_functions/bbox_spec.rb", "spec/postgis_functions/class_spec.rb", "spec/postgis_functions/common_spec.rb", "spec/postgis_functions_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "uninstall.rb"]
14
14
  s.has_rdoc = true
15
15
  s.homepage = %q{http://github.com/nofxx/postgis_adapter}
16
16
  s.rdoc_options = ["--main", "README.rdoc"]
@@ -25,16 +25,16 @@ Gem::Specification.new do |s|
25
25
 
26
26
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
27
  s.add_runtime_dependency(%q<activerecord>, [">= 2.0.2"])
28
- s.add_development_dependency(%q<newgem>, [">= 1.2.3"])
28
+ s.add_development_dependency(%q<newgem>, [">= 1.3.0"])
29
29
  s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
30
30
  else
31
31
  s.add_dependency(%q<activerecord>, [">= 2.0.2"])
32
- s.add_dependency(%q<newgem>, [">= 1.2.3"])
32
+ s.add_dependency(%q<newgem>, [">= 1.3.0"])
33
33
  s.add_dependency(%q<hoe>, [">= 1.8.0"])
34
34
  end
35
35
  else
36
36
  s.add_dependency(%q<activerecord>, [">= 2.0.2"])
37
- s.add_dependency(%q<newgem>, [">= 1.2.3"])
37
+ s.add_dependency(%q<newgem>, [">= 1.3.0"])
38
38
  s.add_dependency(%q<hoe>, [">= 1.8.0"])
39
39
  end
40
40
  end
@@ -80,4 +80,10 @@ ActiveRecord::Schema.define() do
80
80
  t.line_string :geom,:null=>false,:srid=>4326
81
81
  end
82
82
 
83
+ create_table :common_geos do |t|
84
+ t.string :data, :limit => 100
85
+ t.integer :value
86
+ t.point :geom,:null=>false,:srid=>4326
87
+ end
88
+
83
89
  end
@@ -0,0 +1,54 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "Class Functions" do
4
+ before(:all) do
5
+ @c1 ||= City.create!(:data => "City1", :geom => Polygon.from_coordinates([[[12,45],[45,41],[4,1],[12,45]],[[2,5],[5,1],[14,1],[2,5]]],4326))
6
+ @s1 ||= Street.create!(:data => "Street1", :geom => LineString.from_coordinates([[1,1],[88,88]],4326))
7
+ @p1 ||= Position.create!(:data => "Point1", :geom => Point.from_x_y(1,1,4326))
8
+ end
9
+
10
+ it "should find the closest other point" do
11
+ Position.close_to(@p1.geom, :srid => 4326)[0].data.should == @p1.data
12
+ end
13
+
14
+ it "should find the closest other point and limit" do
15
+ Position.close_to(@p1.geom, :limit => 10).should have(10).positions
16
+ end
17
+
18
+ it "should find the closest other point" do
19
+ Position.closest_to(@p1.geom).data.should == @p1.data
20
+ end
21
+
22
+ it "should sort by linestring length" do
23
+ Street.by_length.should be_instance_of(Array)
24
+ end
25
+
26
+ it "should sort by linestring length" do
27
+ Street.by_length(:limit => 10).should have(10).streets
28
+ end
29
+
30
+ it "should find the longest" do
31
+ Street.longest.should be_instance_of(Street)
32
+ end
33
+
34
+ it "should find all dwithin one" do
35
+ Position.all_within(@s1.geom).should be_instance_of(Array)
36
+ end
37
+
38
+ it "should find all dwithin one" do
39
+ City.by_perimeter.should be_instance_of(Array)
40
+ end
41
+
42
+ it "should sort by polygon area" do
43
+ City.by_area.should be_instance_of(Array)
44
+ end
45
+
46
+ it "should sort by all within" do
47
+ City.all_within(@s1.geom).should be_instance_of(Array)
48
+ end
49
+
50
+ it "should sort by all within" do
51
+ City.by_boundaries.should be_instance_of(Array)
52
+ end
53
+
54
+ end
@@ -20,11 +20,11 @@ describe "Common Functions" do
20
20
  describe "Point" do
21
21
 
22
22
  it "should find the closest other point" do
23
- Position.close_to(@p1.geom,4326)[0].data.should == @p1.data
23
+ Position.close_to(@p1.geom)[0].data.should == @p1.data
24
24
  end
25
25
 
26
26
  it "should find the closest other point" do
27
- Position.closest_to(@p1.geom,4326).data.should == @p1.data
27
+ Position.closest_to(@p1.geom).data.should == @p1.data
28
28
  end
29
29
 
30
30
  it { @p1.distance_to(@s2).should be_close(4.24264068711928, 0.0001) }
@@ -72,6 +72,22 @@ describe "Common Functions" do
72
72
  @p2.where_on_line(@s2).should be_close(0.3333, 0.1)
73
73
  end
74
74
 
75
+ it "should have a srid getter" do
76
+ @p1.srid.should eql(29101)
77
+ end
78
+
79
+ it "should calculate the UTM srid" do
80
+ @p2.utm_zone.should eql(32731)
81
+ end
82
+
83
+ it "should convert to utm zone" do
84
+ lambda { @p2.to_utm! }.should change(@p2, :srid)
85
+ end
86
+
87
+ it { @p3.x.should be_close(8.0, 0.1) }
88
+ it { @p3.y.should be_close(8.0, 0.1) }
89
+ it { @p3.z.should be_close(0.0, 0.1) }
90
+
75
91
  end
76
92
 
77
93
  describe "Polygon" do
@@ -161,6 +177,10 @@ describe "Common Functions" do
161
177
  # @c1.disjoint?(@p2).should be_true
162
178
  # end
163
179
 
180
+ it "should find the UTM zone" do
181
+ @c2.utm_zone.should eql(32737)
182
+ end
183
+
164
184
  end
165
185
 
166
186
  describe "LineString" do
@@ -292,6 +312,15 @@ describe "Common Functions" do
292
312
  @s2.segmentize(0.1).should be_instance_of(LineString)
293
313
  end
294
314
 
315
+ it "should find the UTM zone" do
316
+ @s2.utm_zone.should eql(32731)
317
+ end
318
+
319
+ it "should find the UTM zone" do
320
+ @s2.transform!(29101)
321
+ @s2.utm_zone.should eql(32732)
322
+ end
323
+
295
324
  end
296
325
 
297
326
  end
@@ -6,6 +6,7 @@ describe "PostgisFunctions" do
6
6
  @c1 ||= City.create!(:data => "City1", :geom => Polygon.from_coordinates([[[12,45],[45,42],[4,1],[12,45]],[[2,5],[5,1],[14,1],[2,5]]],4326))
7
7
  @s1 ||= Street.create!(:data => "Street1", :geom => LineString.from_coordinates([[-43,-20],[-42,-28]],4326))
8
8
  @p1 ||= Position.create!(:data => "Point1", :geom => Point.from_x_y(-43,-22,4326))
9
+ @cg ||= CommonGeo.create!(:data => "Point1", :geom => Point.from_x_y(-43,-22,4326))
9
10
  end
10
11
 
11
12
  describe "Common Mix" do
@@ -14,6 +15,14 @@ describe "PostgisFunctions" do
14
15
  @p1.distance_to(@s1).should be_close(0.248069469178417, 0.00000001)
15
16
  end
16
17
 
18
+ it "should calculate distance point to line" do
19
+ @cg.distance_to(@s1).should be_close(0.248069469178417, 0.00000001)
20
+ end
21
+
22
+ it "should calculate distance point to line" do
23
+ @p1.geom.as_kml.should eql("<Point>\n<coordinates>-43,-22</coordinates>\n</Point>\n")
24
+ end
25
+
17
26
  it "should calculate inside a city" do
18
27
  @p1.should_not be_inside(@c1)
19
28
  end
@@ -30,29 +39,4 @@ describe "PostgisFunctions" do
30
39
 
31
40
  end
32
41
 
33
- #TODO is sorted rspec helper
34
- describe "Class methods" do
35
-
36
- it "should find all dwithin one" do
37
- Position.all_within(@s1.geom).should be_instance_of(Array)
38
- end
39
-
40
- it "should find all dwithin one" do
41
- City.by_perimeter.should be_instance_of(Array)
42
- end
43
-
44
- it "should sort by polygon area" do
45
- City.by_area.should be_instance_of(Array)
46
- end
47
-
48
- it "should sort by all within" do
49
- City.all_within(@s1.geom).should be_instance_of(Array)
50
- end
51
-
52
- it "should sort by all within" do
53
- City.by_boundaries.should be_instance_of(Array)
54
- end
55
-
56
- end
57
-
58
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postgis_adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcos Piccinini
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-09 00:00:00 -03:00
12
+ date: 2009-04-13 00:00:00 -03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 1.2.3
33
+ version: 1.3.0
34
34
  version:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: hoe
@@ -63,7 +63,6 @@ files:
63
63
  - install.rb
64
64
  - lib/postgis_adapter.rb
65
65
  - lib/postgis_adapter/acts_as_geom.rb
66
- - lib/postgis_adapter/acts_as_geom_flymake.rb
67
66
  - lib/postgis_adapter/common_spatial_adapter.rb
68
67
  - lib/postgis_functions.rb
69
68
  - lib/postgis_functions/bbox.rb
@@ -1,69 +0,0 @@
1
- # #
2
- # PostGIS Adapter
3
- #
4
- #
5
- # http://github.com/nofxx/postgis_adapter
6
- #
7
- module PostgisFunctions
8
- def self.included(base)
9
- base.send :extend, ClassMethods
10
- end
11
-
12
- module ClassMethods
13
- #
14
- # class City
15
- # has_geom :point, :bounds
16
- # end
17
- #
18
- # @city.bounds.area #=> 12.456
19
- # @city.point.distance_to(@p) #=> "1000"
20
- # @product.tax_currency #=> "USD"
21
- #
22
- # Opts:
23
- # :cents => "pennys" #=> @product.pennys
24
- # :currency => "currency" #=> @product.currency
25
- # :allow_nil => true
26
- # :with_currency => true
27
- #
28
- def has_geom(*attributes)
29
- config = {:with_currency => true, :converter => lambda { |m| m.to_money },
30
- :allow_nil => false }.update(attributes.extract_options!)
31
-
32
- attributes.each do |attr|
33
- mapping = [[config[:cents] || "#{attr}_cents", 'cents']]
34
- mapping << [config[:currency] || "#{attr}_currency", 'currency'] if config[:with_currency]
35
-
36
- composed_of attr, :class_name => 'Money',:allow_nil => config[:allow_nil],
37
- :mapping => mapping, :converter => config[:converter]
38
- end
39
-
40
- end
41
-
42
- # acts_as_geom :geom
43
- def acts_as_geom(*columns)
44
- cattr_accessor :postgis_geoms
45
- geoms = columns.map do |g|
46
- geom_type = get_geom_type(g)
47
- case geom_type
48
- when :point
49
- send :include, PointFunctions
50
- when :polygon
51
- send :include, PolygonFunctions
52
- when :line_string
53
- send :include, LineStringFunctions
54
- end
55
- g
56
- end
57
- self.postgis_geoms = {:columns => geoms}#, :opts => options}
58
- end
59
-
60
- def get_geom_type(column)
61
- self.columns.select { |c| c.name == column.to_s}.first.geometry_type
62
- rescue ActiveRecord::StatementInvalid => e
63
- nil
64
- end
65
- end
66
- end
67
-
68
- ActiveRecord::Base.send :include, PostgisFunctions
69
-