postgis_adapter 0.1.8 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,9 +18,8 @@
18
18
  #
19
19
  #
20
20
  module PostgisFunctions
21
- # EARTH_SPHEROID = "'SPHEROID[\"GRS-80\",6378137,298.257222101]'"
22
-
23
- EARTH_SPHEROID = "'SPHEROID[\"IERS_2003\",6378136.6,298.25642]'"
21
+ EARTH_SPHEROID = "'SPHEROID[\"GRS-80\",6378137,298.257222101]'" # SRID => 4326
22
+ #EARTH_SPHEROID = "'SPHEROID[\"IERS_2003\",6378136.6,298.25642]'" # SRID =>
24
23
 
25
24
  def postgis_calculate(operation, subjects, options = nil)
26
25
  subjects = [subjects] unless subjects.respond_to?(:map)
@@ -29,12 +28,13 @@ module PostgisFunctions
29
28
  raise StandardError, "#{e}"
30
29
  end
31
30
 
32
-
33
31
  private
34
32
 
35
-
33
+ def get_column_name
34
+ @geo_column ||= postgis_geoms[:columns].first
35
+ end
36
+
36
37
  # Construct the postgis sql query
37
- # TODO: ST_Transform() ?? # Convert between distances. Implement this?
38
38
  #
39
39
  # Area return in square feet
40
40
  # Distance/DWithin/Length/Perimeter — in projected units.
@@ -49,7 +49,7 @@ module PostgisFunctions
49
49
  :id => t[:id] }
50
50
  end
51
51
 
52
- fields = tables.map { |f| "#{f[:uid]}.geom" } # W1.geom
52
+ fields = tables.map { |f| "#{f[:uid]}.#{get_column_name}" } # W1.geom
53
53
  conditions = tables.map { |f| "#{f[:uid]}.id = #{f[:id]}" } # W1.id = 5
54
54
  tables.map! { |f| "#{f[:class]} #{f[:uid]}" } # streets W1
55
55
 
@@ -57,17 +57,16 @@ module PostgisFunctions
57
57
  # Data => SELECT Func(A,B)
58
58
  # BBox => SELECT (A <=> B)
59
59
  #
60
- if type == :bbox
61
- opcode = nil
62
- s_join = " #{options} "
63
- else
60
+ unless type == :bbox
64
61
  opcode = type.to_s
65
62
  opcode = "ST_#{opcode}" unless opcode =~ /th3d|pesinter/
66
- s_join = ","
67
63
  fields << options if options
64
+ fields = fields.join(",")
65
+ else
66
+ fields = fields.join(" #{options} ")
68
67
  end
69
68
 
70
- sql = "SELECT #{opcode}(#{fields.join(s_join)}) "
69
+ sql = "SELECT #{opcode}(#{fields}) "
71
70
  sql << "FROM #{tables.join(",")} " if tables
72
71
  sql << "WHERE #{conditions.join(" AND ")}" if conditions
73
72
  #p sql; sql
@@ -94,7 +93,7 @@ module PostgisFunctions
94
93
 
95
94
  # Get a unique ID for tables
96
95
  def unique_identifier
97
- @u_id ||= "W1"
96
+ @u_id ||= "T1"
98
97
  @u_id = @u_id.succ
99
98
  end
100
99
 
@@ -1,21 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  Gem::Specification.new do |s|
2
- s.name = "postgis_adapter"
3
- s.version = "0.1.8"
4
+ s.name = %q{postgis_adapter}
5
+ s.version = "0.2.1"
6
+
4
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
5
8
  s.authors = ["Marcos Piccinini"]
6
- s.date = "2008-12-16"
7
- s.description = "Postgis Adapter for Activer Record"
9
+ s.date = %q{2009-01-10}
10
+ s.description = %q{Postgis Adapter for Activer Record}
8
11
  s.email = ["x@nofxx.com"]
9
12
  s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"]
10
- 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", "lib/postgis_functions/linestring.rb", "lib/postgis_functions/point.rb", "lib/postgis_functions/polygon.rb", "postgis_adapter.gemspec", "rails/init.rb", "script/console", "script/destroy", "script/generate", "uninstall.rb"]
11
- s.test_files = ["spec/acts_as_geom_spec.rb", "spec/common_spatial_adapter_spec.rb", "spec/db/database_postgis.yml", "spec/db/models_postgis.rb", "spec/db/schema_postgis.rb", "spec/postgis_adapter_spec.rb", "spec/postgis_functions/bbox_spec.rb", "spec/postgis_functions/linestring_spec.rb", "spec/postgis_functions/point_spec.rb", "spec/postgis_functions/polygon_spec.rb", "spec/postgis_functions_spec.rb", "spec/spec.opts", "spec/spec_helper.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"]
12
14
  s.has_rdoc = true
13
- s.homepage = "http://github.com/nofxx/postgis_adapter"
15
+ s.homepage = %q{http://github.com/nofxx/postgis_adapter}
14
16
  s.rdoc_options = ["--main", "README.rdoc"]
15
17
  s.require_paths = ["lib"]
16
18
  s.rubyforge_project = %q{postgis_adapter}
17
19
  s.rubygems_version = %q{1.3.1}
18
- s.summary = "Postgis Adapter for Activer Record"
20
+ s.summary = %q{Postgis Adapter for Activer Record}
19
21
 
20
22
  if s.respond_to? :specification_version then
21
23
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -23,16 +25,16 @@ Gem::Specification.new do |s|
23
25
 
24
26
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
25
27
  s.add_runtime_dependency(%q<activerecord>, [">= 2.0.2"])
26
- s.add_development_dependency(%q<newgem>, [">= 1.1.0"])
28
+ s.add_development_dependency(%q<newgem>, [">= 1.2.3"])
27
29
  s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
28
30
  else
29
31
  s.add_dependency(%q<activerecord>, [">= 2.0.2"])
30
- s.add_dependency(%q<newgem>, [">= 1.1.0"])
32
+ s.add_dependency(%q<newgem>, [">= 1.2.3"])
31
33
  s.add_dependency(%q<hoe>, [">= 1.8.0"])
32
34
  end
33
35
  else
34
36
  s.add_dependency(%q<activerecord>, [">= 2.0.2"])
35
- s.add_dependency(%q<newgem>, [">= 1.1.0"])
37
+ s.add_dependency(%q<newgem>, [">= 1.2.3"])
36
38
  s.add_dependency(%q<hoe>, [">= 1.8.0"])
37
39
  end
38
40
  end
@@ -1,4 +1,4 @@
1
1
  adapter: postgresql
2
- database: postgis_plugin_test
2
+ database: postgis_plugin
3
3
  username: postgres
4
4
  password:
@@ -53,4 +53,5 @@ class Street < ActiveRecord::Base
53
53
  end
54
54
 
55
55
  class CommonGeo < ActiveRecord::Base
56
+ acts_as_geom :geom
56
57
  end
@@ -54,33 +54,30 @@ ActiveRecord::Schema.define() do
54
54
  t.point "geom", :null => false, :with_m => true, :with_z => true
55
55
  end
56
56
 
57
- create_table "table_srid_line_strings", :force => true do |t|
58
- t.line_string "geom", :null => false , :srid => 123
57
+ create_table "table_srid_line_strings", :force => true do |t|
58
+ t.line_string "geom", :null => false , :srid => 4326
59
59
  end
60
60
 
61
61
  create_table "table_srid4d_polygons", :force => true do |t|
62
- t.polygon "geom", :with_m => true, :with_z => true, :srid => 123
62
+ t.polygon "geom", :with_m => true, :with_z => true, :srid => 4326
63
63
  end
64
64
 
65
- create_table :cities do |t|
66
- t.string :data, :limit => 100
67
- t.integer :value
68
- t.polygon :geom,:null=>false,:srid=>123
69
- end
70
-
71
- create_table :positions do |t|
72
- t.string :data, :limit => 100
73
- t.integer :value
74
- t.point :geom,:null=>false,:srid=>123
75
- end
76
-
77
- create_table :streets do |t|
78
- t.string :data, :limit => 100
79
- t.integer :value
80
- t.line_string :geom,:null=>false,:srid=>123
81
- end
82
-
65
+ create_table :cities do |t|
66
+ t.string :data, :limit => 100
67
+ t.integer :value
68
+ t.polygon :geom,:null=>false,:srid=>4326
69
+ end
83
70
 
71
+ create_table :positions do |t|
72
+ t.string :data, :limit => 100
73
+ t.integer :value
74
+ t.point :geom,:null=>false,:srid=>4326
75
+ end
84
76
 
77
+ create_table :streets do |t|
78
+ t.string :data, :limit => 100
79
+ t.integer :value
80
+ t.line_string :geom,:null=>false,:srid=>4326
81
+ end
85
82
 
86
83
  end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActsAsGeom" do
4
+
5
+ before(:each) do
6
+ class DiffColumn < ActiveRecord::Base
7
+ acts_as_geom :ponto
8
+ end
9
+
10
+ class NotInDb < ActiveRecord::Base
11
+ acts_as_geom :geom
12
+ end
13
+ end
14
+
15
+ it "should get the geom type" do
16
+ City.get_geom_type(:geom).should eql(:polygon)
17
+ end
18
+
19
+ it "should not interfere with migrations" do
20
+ NotInDb.get_geom_type(:geom).should be_nil
21
+ end
22
+
23
+ it "should query a diff column name" do
24
+ # DiffColumn
25
+ end
26
+
27
+ end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/spec_helper.rb'
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
2
 
3
3
  describe "CommonSpatialAdapter" do
4
4
 
@@ -48,9 +48,9 @@ describe "PostgisAdapter" do
48
48
  end
49
49
 
50
50
  it "should test multipoint" do
51
- mp = TableMultiPoint.create!(:geom => MultiPoint.from_coordinates([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]]))
51
+ mp = TableMultiPoint.create!(:geom => MultiPoint.from_coordinates([[12.4,-4326.3],[-65.1,4326.4],[4326.55555555,4326]]))
52
52
  find = TableMultiPoint.find(:first)
53
- find.geom.should == MultiPoint.from_coordinates([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]])
53
+ find.geom.should == MultiPoint.from_coordinates([[12.4,-4326.3],[-65.1,4326.4],[4326.55555555,4326]])
54
54
  end
55
55
 
56
56
  end
@@ -68,18 +68,18 @@ describe "PostgisAdapter" do
68
68
  end
69
69
 
70
70
  it "should test_srid_line_string" do
71
- ls = TableSridLineString.create!(:geom => LineString.from_coordinates([[1.4,2.5],[1.5,6.7]],123))
71
+ ls = TableSridLineString.create!(:geom => LineString.from_coordinates([[1.4,2.5],[1.5,6.7]],4326))
72
72
  ls = TableSridLineString.find(:first)
73
- ls_e = LineString.from_coordinates([[1.4,2.5],[1.5,6.7]],123)
73
+ ls_e = LineString.from_coordinates([[1.4,2.5],[1.5,6.7]],4326)
74
74
  ls.geom.should be_instance_of(LineString)
75
- ls.geom.srid.should eql(123)
75
+ ls.geom.srid.should eql(4326)
76
76
  end
77
77
 
78
78
 
79
79
  it "hsould test_multi_line_string" do
80
- ml = TableMultiLineString.create!(:geom => MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012]]),LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012],[45.123,123.3]])]))
80
+ ml = TableMultiLineString.create!(:geom => MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.432612,-0.012]]),LineString.from_coordinates([[1.5,45.2],[-54.432612,-0.012],[45.4326,4326.3]])]))
81
81
  find = TableMultiLineString.find(:first)
82
- find.geom.should == MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012]]),LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012],[45.123,123.3]])])
82
+ find.geom.should == MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.432612,-0.012]]),LineString.from_coordinates([[1.5,45.2],[-54.432612,-0.012],[45.4326,4326.3]])])
83
83
  end
84
84
  end
85
85
 
@@ -103,11 +103,11 @@ describe "PostgisAdapter" do
103
103
  end
104
104
 
105
105
  it "should test_srid_4d_polygon" do
106
- pg = TableSrid4dPolygon.create(:geom => Polygon.from_coordinates([[[0,0,2,-45.1],[4,0,2,5],[4,4,2,4.67],[0,4,2,1.34],[0,0,2,-45.1]],[[1,1,2,12.3],[3,1,2,123],[3,3,2,12.2],[1,3,2,12],[1,1,2,12.3]]],123,true,true))
106
+ pg = TableSrid4dPolygon.create(:geom => Polygon.from_coordinates([[[0,0,2,-45.1],[4,0,2,5],[4,4,2,4.67],[0,4,2,1.34],[0,0,2,-45.1]],[[1,1,2,12.3],[3,1,2,4326],[3,3,2,12.2],[1,3,2,12],[1,1,2,12.3]]],4326,true,true))
107
107
  find = TableSrid4dPolygon.find(:first)
108
- pg_e = Polygon.from_coordinates([[[0,0,2,-45.1],[4,0,2,5],[4,4,2,4.67],[0,4,2,1.34],[0,0,2,-45.1]],[[1,1,2,12.3],[3,1,2,123],[3,3,2,12.2],[1,3,2,12],[1,1,2,12.3]]],123,true,true)
108
+ pg_e = Polygon.from_coordinates([[[0,0,2,-45.1],[4,0,2,5],[4,4,2,4.67],[0,4,2,1.34],[0,0,2,-45.1]],[[1,1,2,12.3],[3,1,2,4326],[3,3,2,12.2],[1,3,2,12],[1,1,2,12.3]]],4326,true,true)
109
109
  pg.geom.should == pg_e
110
- pg.geom.srid.should eql(123)
110
+ pg.geom.srid.should eql(4326)
111
111
  end
112
112
  end
113
113
 
@@ -134,7 +134,7 @@ describe "PostgisAdapter" do
134
134
  create_table "parks", :force => true do |t|
135
135
  t.column "data" , :string, :limit => 100
136
136
  t.column "value", :integer
137
- t.column "geom", :point,:null=>false,:srid=>123
137
+ t.column "geom", :point,:null=>false,:srid=>4326
138
138
  end
139
139
  add_index "parks","geom",:spatial=>true,:name => "example_spatial_index"
140
140
  end
@@ -143,13 +143,13 @@ describe "PostgisAdapter" do
143
143
  end
144
144
 
145
145
  it "should create some points" do
146
- Park.create!(:data => "Point1", :geom => Point.from_x_y(1.2,0.75,123))
147
- Park.create!(:data => "Point2",:geom => Point.from_x_y(0.6,1.3,123))
148
- Park.create!(:data => "Point3", :geom => Point.from_x_y(2.5,2,123))
146
+ Park.create!(:data => "Point1", :geom => Point.from_x_y(1.2,0.75,4326))
147
+ Park.create!(:data => "Point2",:geom => Point.from_x_y(0.6,1.3,4326))
148
+ Park.create!(:data => "Point3", :geom => Point.from_x_y(2.5,2,4326))
149
149
  end
150
150
 
151
151
  it "should find by geom" do
152
- pts = Park.find_all_by_geom(LineString.from_coordinates([[0,0],[2,2]],123))
152
+ pts = Park.find_all_by_geom(LineString.from_coordinates([[0,0],[2,2]],4326))
153
153
  pts.should be_instance_of(Array)
154
154
  pts.length.should eql(2)
155
155
  pts[0].data.should match /Point/
@@ -157,12 +157,12 @@ describe "PostgisAdapter" do
157
157
  end
158
158
 
159
159
  it "should find by geom again" do
160
- pts = Park.find_all_by_geom(LineString.from_coordinates([[2.49,1.99],[2.51,2.01]],123))
160
+ pts = Park.find_all_by_geom(LineString.from_coordinates([[2.49,1.99],[2.51,2.01]],4326))
161
161
  pts[0].data.should eql("Point3")
162
162
  end
163
163
 
164
164
  it "should find by geom column bbox condition" do
165
- pts = Park.find_all_by_geom([[0,0],[2,2],123])
165
+ pts = Park.find_all_by_geom([[0,0],[2,2],4326])
166
166
  pts.should be_instance_of(Array)
167
167
  pts.length.should eql(2)
168
168
  pts[0].data.should match /Point/
@@ -4,18 +4,17 @@ require File.dirname(__FILE__) + '/../spec_helper.rb'
4
4
  describe "Point" do
5
5
 
6
6
  before(:all) do
7
- @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]]],123))
8
- @c2 ||= City.create!(:data => "City1", :geom => Polygon.from_coordinates([[[22,66],[65,65],[20,10],[22,66]],[[10,15],[15,11],[34,14],[10,15]]],123))
9
- @c3 ||= City.create!(:data => "City3", :geom => Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]],123))
10
- @s1 ||= Street.create!(:data => "Street1", :geom => LineString.from_coordinates([[1,1],[2,2]],123))
11
- @s2 ||= Street.create!(:data => "Street2", :geom => LineString.from_coordinates([[4,4],[7,7]],123))
12
- @s3 ||= Street.create!(:data => "Street3", :geom => LineString.from_coordinates([[8,8],[18,18],[20,20],[25,25],[30,30],[38,38]],123))
13
- @s4 ||= Street.create!(:data => "Street3", :geom => LineString.from_coordinates([[10,8],[15,18]],123))
14
- @p1 ||= Position.create!(:data => "Point1", :geom => Point.from_x_y(1,1,123))
15
- @p2 ||= Position.create!(:data => "Point2", :geom => Point.from_x_y(5,5,123))
16
- @p3 ||= Position.create!(:data => "Point3", :geom => Point.from_x_y(8,8,123))
7
+ @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))
8
+ @c2 ||= City.create!(:data => "City1", :geom => Polygon.from_coordinates([[[22,66],[65,65],[20,10],[22,66]],[[10,15],[15,11],[34,14],[10,15]]],4326))
9
+ @c3 ||= City.create!(:data => "City3", :geom => Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]],4326))
10
+ @s1 ||= Street.create!(:data => "Street1", :geom => LineString.from_coordinates([[1,1],[2,2]],4326))
11
+ @s2 ||= Street.create!(:data => "Street2", :geom => LineString.from_coordinates([[4,4],[7,7]],4326))
12
+ @s3 ||= Street.create!(:data => "Street3", :geom => LineString.from_coordinates([[8,8],[18,18],[20,20],[25,25],[30,30],[38,38]],4326))
13
+ @s4 ||= Street.create!(:data => "Street3", :geom => LineString.from_coordinates([[10,8],[15,18]],4326))
14
+ @p1 ||= Position.create!(:data => "Point1", :geom => Point.from_x_y(1,1,4326))
15
+ @p2 ||= Position.create!(:data => "Point2", :geom => Point.from_x_y(5,5,4326))
16
+ @p3 ||= Position.create!(:data => "Point3", :geom => Point.from_x_y(8,8,4326))
17
17
  end
18
-
19
18
 
20
19
  describe "BBox operations" do
21
20
 
@@ -27,49 +26,17 @@ describe "Point" do
27
26
  @p1.bbox(">>", @c1).should be_false
28
27
  end
29
28
 
30
- it do
31
- @p1.should be_strictly_left_of(@c1)
32
- end
33
-
34
- it do
35
- @p1.should_not be_strictly_right_of(@c1)
36
- end
37
-
38
- it do
39
- @p1.should_not be_overlaps_or_right_of(@c1)
40
- end
41
-
42
- it do
43
- @p1.should be_overlaps_or_left_of(@c1)
44
- end
45
-
46
- it do
47
- @p1.should_not be_completely_contained_by(@c1)
48
- end
49
-
50
- it do
51
- @c2.completely_contains?(@p1).should be_false
52
- end
53
-
54
- it do
55
- @p1.should be_overlaps_or_above(@c1)
56
- end
57
-
58
- it do
59
- @p1.should be_overlaps_or_below(@c1)
60
- end
61
-
62
- it do
63
- @p1.should_not be_strictly_above(@c1)
64
- end
65
-
66
- it do
67
- @p1.should_not be_strictly_below(@c1)
68
- end
69
-
70
- it do
71
- @p1.interacts_with?(@c1).should be_false
72
- end
29
+ it do @p1.should be_strictly_left_of(@c1) end
30
+ it do @p1.should_not be_strictly_right_of(@c1) end
31
+ it do @p1.should_not be_overlaps_or_right_of(@c1) end
32
+ it do @p1.should be_overlaps_or_left_of(@c1) end
33
+ it do @p1.should_not be_completely_contained_by(@c1) end
34
+ it do @c2.completely_contains?(@p1).should be_false end
35
+ it do @p1.should be_overlaps_or_above(@c1) end
36
+ it do @p1.should be_overlaps_or_below(@c1) end
37
+ it do @p1.should_not be_strictly_above(@c1) end
38
+ it do @p1.should_not be_strictly_below(@c1) end
39
+ it do @p1.interacts_with?(@c1).should be_false end
73
40
 
74
41
  it do
75
42
  @p1.binary_equal?(@c1).should be_false
@@ -79,6 +46,6 @@ describe "Point" do
79
46
  @p1.same_as?(@c1).should be_false
80
47
  end
81
48
 
82
-
83
49
  end
84
- end
50
+
51
+ end
File without changes
@@ -0,0 +1,281 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "Common Functions" do
4
+
5
+ before(:all) do
6
+ @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))
7
+ @c2 ||= City.create!(:data => "City1", :geom => Polygon.from_coordinates([[[22,66],[65,65],[20,10],[22,66]],[[10,15],[15,11],[34,14],[10,15]]],4326))
8
+ @c3 ||= City.create!(:data => "City3", :geom => Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]],4326))
9
+ @s1 ||= Street.create!(:data => "Street1", :geom => LineString.from_coordinates([[1,1],[2,2]],4326))
10
+ @s2 ||= Street.create!(:data => "Street2", :geom => LineString.from_coordinates([[4,4],[7,7]],4326))
11
+ @s3 ||= Street.create!(:data => "Street3", :geom => LineString.from_coordinates([[8,8],[18,18],[20,20],[25,25],[30,30],[38,38]],4326))
12
+ @s4 ||= Street.create!(:data => "Street4", :geom => LineString.from_coordinates([[10,8],[15,18]],4326))
13
+ @p1 ||= Position.create!(:data => "Point1", :geom => Point.from_x_y(1,1,4326))
14
+ @p2 ||= Position.create!(:data => "Point2", :geom => Point.from_x_y(5,5,4326))
15
+ @p3 ||= Position.create!(:data => "Point3", :geom => Point.from_x_y(8,8,4326))
16
+ @p4 ||= Position.create!(:data => "Point4", :geom => Point.from_x_y(18.1,18,4326))
17
+ @p5 ||= Position.create!(:data => "Point5", :geom => Point.from_x_y(30,30,4326))
18
+ end
19
+
20
+ describe "Point" do
21
+
22
+ it "should find the closest other point" do
23
+ Position.close_to(@p1.geom,4326)[0].data.should == @p1.data
24
+ end
25
+
26
+ it "should find the closest other point" do
27
+ Position.closest_to(@p1.geom,4326).data.should == @p1.data
28
+ end
29
+
30
+ it { @p1.distance_to(@s2).should be_close(4.24264068711928, 0.0001) }
31
+ it { @p1.distance_to(@s3).should be_close(9.89949493661167, 0.0001) }
32
+ it { @p2.distance_to(@s3).should be_close(4.24264068711928, 0.0001) }
33
+ it { @p1.distance_to(@p2).should be_close(5.65685424949238, 0.0001) }
34
+ it { @p1.distance_to(@c1).should be_close(3.0, 0.0001) }
35
+ it { @p1.distance_to(@c2).should be_close(21.0237960416286, 0.000001) }
36
+ it { @p1.distance_to(@s2).should be_close(4.24264068711928, 0.000001) }
37
+ it { @p1.distance_sphere_to(@p2).should be_close(628516.874554178, 0.0001) }
38
+ it { @p1.distance_sphere_to(@p3).should be_close(1098726.61466584, 0.00001) }
39
+ it { @p1.distance_spheroid_to(@p2).should be_close(627129.50,0.01) }
40
+ it { @p1.distance_spheroid_to(@p2).should be_close(627129.502639041, 0.000001) }
41
+ it { @p1.distance_spheroid_to(@p3).should be_close(1096324.48117672, 0.000001) }
42
+
43
+ it { @p1.should_not be_inside(@c1) }
44
+ it { @p1.should be_outside(@c1) }
45
+ it { @p1.should be_inside_circle(2.0,2.0,20.0) }
46
+ it { @p1.should_not be_inside_circle(50,50,2) }
47
+ it { @p1.should be_in_bounds(@s1) }
48
+ it { @p3.should_not be_in_bounds(@s1, 1) }
49
+ it { @p4.in_bounds?(@s3, 0.01).should be_false }
50
+
51
+ it { @p1.azimuth(@p2).should be_close(0.785398163397448,0.000001) }
52
+ it { @p1.azimuth(@s2).should raise_error }
53
+ it { @p1.disjoint?(@s2).should be_true }
54
+ it { @p3.polygonize.geometries.should be_empty }
55
+ it { @p4.where_on_line(@s3).should be_close(0.335, 0.0001) }
56
+ it { @s3.locate_point(@p4).should be_close(0.335, 0.1)}
57
+ it { @s3.interpolate_point(0.335).x.should be_close(18.05, 0.01) }
58
+
59
+ it { @p1.relate?(@s3, "T*T***FF*").should be_false }
60
+ it { @p1.relate?(@s3).should eql("FF0FFF102") }
61
+
62
+ it "should transform srid" do
63
+ @p1.geom = @p1.transform(29101)
64
+ @p1.geom.srid.should eql(29101)
65
+ end
66
+
67
+ it "should see in what fraction of the ls it is" do
68
+ @p1.where_on_line(@s1).should eql(0.0)
69
+ end
70
+
71
+ end
72
+
73
+ describe "Polygon" do
74
+
75
+ it "sort by area size" do
76
+ City.by_area.first.data.should == "City1" #[@c1, @c2, @c3]
77
+ end
78
+
79
+ it "find all cities that contains a point" do
80
+ City.contains(@p1.geom, 4326).should eql([])
81
+ end
82
+
83
+ it "should find one city (first) that contains a point" do
84
+ City.contain(@p4.geom, 4326).data.should eql("City1")
85
+ end
86
+
87
+ it { @c2.should be_closed }
88
+ it { @c3.area.should be_close(1093.270089, 0.1) }
89
+ it { @c2.area.should be_close(1159.5, 0.1) }
90
+
91
+ it { @c2.dimension.should eql(2) }
92
+ it { @c2.perimeter.should be_close(219.770013855493, 0.1) }
93
+ it { @c2.perimeter3d.should be_close(219.770013855493, 0.1) }
94
+ it { @c1.contains?(@p1).should be_false }
95
+ it { @c1.contains?(@p4).should be_true }
96
+
97
+ it { @c1.should_not be_spatially_equal(@c2) }
98
+
99
+ it { @c1.covers?(@p1).should be_false }
100
+ it { @c1.covers?(@p4).should be_true }
101
+ it { @c1.should_not be_within(@c2) }
102
+
103
+ it "city overlaps point?" do
104
+ lambda { @c3.overlaps?(@c2) }.should raise_error # WHY??
105
+ end
106
+
107
+ it "should get a polygon for envelope" do
108
+ @c2.envelope.should be_instance_of(Polygon)
109
+ end
110
+
111
+ it "should get a polygon for envelope" do
112
+ @c2.envelope.rings[0].points[0].should be_instance_of(Point)
113
+ end
114
+
115
+ it "should get the center" do
116
+ @c2.centroid.x.should be_close(36.2945235015093,0.00001)
117
+ @c2.centroid.y.should be_close(48.3211154233146,0.00001)
118
+ end
119
+
120
+ it "should get the center with the correct srid" do
121
+ @c1.centroid.srid.should eql(4326)
122
+ end
123
+
124
+ it "distance from another" do
125
+ @c1.distance_to(@c3).should eql(0.0)
126
+ end
127
+
128
+ it "distance to a linestring" do
129
+ @c1.distance_to(@s1).should be_close(1.8,0.001)
130
+ end
131
+
132
+ it "should simplify me" do
133
+ @c3.simplify.should be_instance_of(Polygon)
134
+ end
135
+
136
+ it "should simplify me number of points" do
137
+ @c3.simplify[0].length.should eql(4)
138
+ end
139
+
140
+ #Strange again.... s2 s3 ... error
141
+ it { @c3.touches?(@s1).should be_false }
142
+ it { @c2.should be_simple }
143
+ it { @c2.disjoint?(@p2).should be_true }
144
+ it { @c3.polygonize.should have(2).geometries }
145
+
146
+ # weird...
147
+ # it do
148
+ # @c1.disjoint?(@s2).should be_true
149
+ # end
150
+
151
+ end
152
+
153
+ describe "LineString" do
154
+
155
+ it "should sort by size" do
156
+ Street.by_length.first.data.should == "Street1"
157
+ Street.by_length.last.data.should == "Street3"
158
+ end
159
+
160
+ it "largest" do
161
+ Street.longest.data.should == "Street3"
162
+ end
163
+
164
+ describe "Length" do
165
+ it { @s1.length.should be_close(1.4142135623731, 0.000001) }
166
+ it { @s2.length.should be_close(4.2, 0.1) }
167
+ it { @s3.length.should be_close(42.4264068, 0.001) }
168
+ it { @s1.length_spheroid.should be_close(156876.1494,0.0001) }
169
+ it { @s1.length_3d.should be_close(1.4142135623731,0.0001) }
170
+ end
171
+
172
+ it { @s1.crosses?(@s2).should be_false }
173
+ it { @s4.crosses?(@s3).should be_true }
174
+ it { @s1.touches?(@s2).should be_false }
175
+ it { @s4.touches?(@s3).should be_false }
176
+
177
+ it "should intersect with linestring" do
178
+ @s4.intersects?(@s3).should be_true
179
+ end
180
+
181
+ it "should not intersect with this linestring" do
182
+ @s4.intersects?(@s1).should be_false
183
+ end
184
+
185
+ it "intersection with a point" do
186
+ @s1.intersection(@p2).should be_instance_of(GeometryCollection)
187
+ end
188
+
189
+ describe "Self" do
190
+
191
+ it do
192
+ @s1.envelope.should be_instance_of(Polygon)
193
+ end
194
+
195
+ it "should get a polygon for envelope" do
196
+ @s1.envelope.rings[0].points[0].should be_instance_of(Point)
197
+ end
198
+
199
+ it "should get the center" do
200
+ @s1.centroid.x.should be_close(1.5,0.01)
201
+ @s1.centroid.y.should be_close(1.5,0.01)
202
+ end
203
+
204
+ it "should get the center with the correct srid" do
205
+ @s1.centroid.srid.should eql(4326)
206
+ end
207
+
208
+ it "number of points" do
209
+ @s3.num_points.should eql(6)
210
+ end
211
+
212
+ it "startpoint" do
213
+ @s3.start_point.should be_instance_of(Point)
214
+ @s3.start_point.x.should be_close(8.0, 0.1)
215
+ end
216
+
217
+ it "endpoint" do
218
+ @s2.end_point.should be_instance_of(Point)
219
+ @s2.end_point.x.should be_close(7.0, 0.1)
220
+ end
221
+
222
+ it { @s1.should_not be_envelopes_intersect(@s2) }
223
+ it { @s1.boundary.should be_instance_of(MultiPoint) }
224
+
225
+ end
226
+
227
+ describe "Distance" do
228
+
229
+ it { @s1.distance_to(@p3).should be_close(8.48528137423857,0.0001) }
230
+ it { @s1.distance_to(@p3).should be_close(8.48,0.01) }
231
+
232
+ it do
233
+ lambda { @p1.distance_spheroid_to(@c3) }.should raise_error
234
+ end
235
+
236
+ it do
237
+ lambda { @p3.distance_spheroid_to(@s1) }.should raise_error
238
+ end
239
+
240
+ end
241
+
242
+ it do @s1.locate_point(@p1).should eql(0.0) end
243
+ it do @s1.locate_point(@p2).should eql(1.0) end
244
+
245
+ it "should simplify a line" do
246
+ @s3.simplify.points.length.should eql(2)
247
+ end
248
+
249
+ it "should simplify the first correcty" do
250
+ @s3.simplify.points[0].y.should be_close(8.0, 0.1)
251
+ end
252
+
253
+ it "should simplify the last correcty" do
254
+ @s3.simplify.points[1].y.should be_close(38.0, 0.1)
255
+ end
256
+
257
+ it { @s1.overlaps?(@c2).should be_false }
258
+ it { @s1.overlaps?(@s2).should be_false }
259
+ it { @s1.convex_hull.should be_instance_of(LineString) }
260
+ it { @s1.line_substring(0.2,0.5).should be_instance_of(LineString) }
261
+
262
+ it do
263
+ @s1.interpolate_point(0.7).should be_instance_of(Point)
264
+ @s1.interpolate_point(0.7).x.should be_close(1.7,0.1)
265
+ end
266
+
267
+ it { @s1.should be_simple }
268
+ it { @s1.disjoint?(@s2).should be_true }
269
+ it { @s1.polygonize.should be_instance_of(GeometryCollection) }
270
+ it { @s3.polygonize.geometries.should be_empty }
271
+ it { @s2.locate_along_measure(1.6).should be_nil }
272
+ it { @s2.locate_between_measures(0.1,0.3).should be_nil }
273
+
274
+ it "should build area" do
275
+ @s2.build_area.should be_nil
276
+ end
277
+
278
+ end
279
+
280
+ end
281
+