rgeo 0.1.20 → 0.1.21

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.
Files changed (55) hide show
  1. data/History.rdoc +10 -0
  2. data/README.rdoc +38 -35
  3. data/Version +1 -1
  4. data/lib/active_record/connection_adapters/mysql2spatial_adapter.rb +1 -3
  5. data/lib/active_record/connection_adapters/mysqlspatial_adapter.rb +4 -4
  6. data/lib/active_record/connection_adapters/postgis_adapter.rb +426 -0
  7. data/lib/active_record/connection_adapters/spatialite_adapter.rb +488 -0
  8. data/lib/rgeo.rb +10 -29
  9. data/lib/rgeo/active_record/arel_modifications.rb +1 -0
  10. data/lib/rgeo/active_record/base_modifications.rb +27 -10
  11. data/lib/rgeo/active_record/common.rb +128 -0
  12. data/lib/rgeo/active_record/mysql_common.rb +14 -51
  13. data/lib/rgeo/cartesian/factory.rb +2 -2
  14. data/lib/rgeo/coord_sys.rb +1 -1
  15. data/lib/rgeo/coord_sys/proj4.rb +3 -2
  16. data/lib/rgeo/error.rb +0 -3
  17. data/lib/rgeo/feature.rb +1 -3
  18. data/lib/rgeo/feature/factory_generator.rb +8 -0
  19. data/lib/rgeo/geography/factory.rb +2 -2
  20. data/lib/rgeo/geography/interface.rb +3 -3
  21. data/lib/rgeo/geos/zm_factory.rb +2 -2
  22. data/lib/rgeo/wkrep/wkb_parser.rb +35 -36
  23. data/lib/rgeo/wkrep/wkt_parser.rb +36 -38
  24. data/test/active_record/common_setup_methods.rb +129 -0
  25. data/test/active_record/readme.txt +10 -0
  26. data/test/active_record/tc_mysqlspatial.rb +22 -71
  27. data/test/active_record/tc_postgis.rb +282 -0
  28. data/test/active_record/tc_spatialite.rb +198 -0
  29. data/test/coord_sys/tc_proj4.rb +12 -5
  30. data/test/projected_geography/tc_geometry_collection.rb +1 -1
  31. data/test/projected_geography/tc_line_string.rb +1 -1
  32. data/test/projected_geography/tc_multi_line_string.rb +1 -1
  33. data/test/projected_geography/tc_multi_point.rb +1 -1
  34. data/test/projected_geography/tc_multi_polygon.rb +2 -2
  35. data/test/projected_geography/tc_point.rb +4 -4
  36. data/test/projected_geography/tc_polygon.rb +1 -1
  37. data/test/simple_mercator/tc_geometry_collection.rb +1 -1
  38. data/test/simple_mercator/tc_line_string.rb +1 -1
  39. data/test/simple_mercator/tc_multi_line_string.rb +1 -1
  40. data/test/simple_mercator/tc_multi_point.rb +1 -1
  41. data/test/simple_mercator/tc_multi_polygon.rb +2 -2
  42. data/test/simple_mercator/tc_point.rb +4 -4
  43. data/test/simple_mercator/tc_polygon.rb +1 -1
  44. data/test/simple_mercator/tc_window.rb +1 -1
  45. data/test/spherical_geography/tc_geometry_collection.rb +1 -1
  46. data/test/spherical_geography/tc_line_string.rb +1 -1
  47. data/test/spherical_geography/tc_multi_line_string.rb +1 -1
  48. data/test/spherical_geography/tc_multi_point.rb +1 -1
  49. data/test/spherical_geography/tc_multi_polygon.rb +2 -2
  50. data/test/spherical_geography/tc_point.rb +4 -4
  51. data/test/spherical_geography/tc_polygon.rb +1 -1
  52. data/test/tc_oneoff.rb +3 -3
  53. data/test/wkrep/tc_wkb_parser.rb +14 -14
  54. data/test/wkrep/tc_wkt_parser.rb +37 -45
  55. metadata +10 -3
@@ -27,6 +27,16 @@ mysql2spatial:
27
27
  username: <mysql_user>
28
28
  password: <mysql_password>
29
29
  host: localhost
30
+ spatialite:
31
+ adapter: spatialite
32
+ database: /path/to/sqlite3_test_database.db
33
+ libspatialite: /path/to/libspatialite.so
34
+ postgis:
35
+ adapter: postgis
36
+ database: <postgres_test_database>
37
+ username: <postgres_user>
38
+ password: <postgres_password>
39
+ host: localhost
30
40
  ####
31
41
 
32
42
  Note that the tests assume they "own" these databases, and they may
@@ -33,76 +33,29 @@
33
33
  # -----------------------------------------------------------------------------
34
34
  ;
35
35
 
36
+ require 'test/unit'
37
+ require ::File.expand_path('common_setup_methods.rb', ::File.dirname(__FILE__))
36
38
 
37
- if ::File.exists?(::File.dirname(__FILE__)+'/database.yml')
38
-
39
- require 'test/unit'
40
- require 'rgeo'
41
- require 'yaml'
42
- require 'active_record'
43
-
44
-
45
- module RGeo
46
- module Tests # :nodoc:
47
- module ActiveRecord # :nodoc:
48
-
49
- ALL_DATABASES_CONFIG = ::YAML.load_file(::File.dirname(__FILE__)+'/database.yml')
50
-
51
-
52
- module Classes # :nodoc:
53
-
54
- @class_num = 0
55
-
56
- def self.new_class(config_)
57
- klass_ = ::Class.new(::ActiveRecord::Base)
58
- @class_num += 1
59
- self.const_set("Klass#{@class_num}".to_sym, klass_)
60
- klass_.class_eval do
61
- establish_connection(config_)
62
- set_table_name(:spatial_test)
63
- end
64
- klass_
65
- end
66
-
67
- end
39
+
40
+ module RGeo
41
+ module Tests # :nodoc:
42
+ module ActiveRecord # :nodoc:
43
+
44
+ if TESTS_AVAILABLE
68
45
 
69
46
 
70
- module CommonTestMethods # :nodoc:
47
+ module CommonMysqlTestMethods # :nodoc:
71
48
 
72
- def self.included(klass_)
73
- database_config_ = ALL_DATABASES_CONFIG[klass_.const_get(:ADAPTER_NAME)]
74
- klass_.const_set(:DATABASE_CONFIG, database_config_)
75
- klass_.const_set(:DEFAULT_AR_CLASS, Classes.new_class(database_config_))
76
- end
77
49
 
78
-
79
- def setup
80
- @factory = ::RGeo::Cartesian.preferred_factory(:srid => 4326)
81
- cleanup_tables
82
- end
83
-
84
-
85
- def teardown
86
- cleanup_tables
87
- end
88
-
89
-
90
- def cleanup_tables
91
- klass_ = self.class.const_get(:DEFAULT_AR_CLASS)
92
- tables_ = klass_.connection.select_values('SHOW TABLES')
93
- tables_.each{ |table_| klass_.connection.drop_table(table_) }
94
- end
95
-
96
-
97
- def create_ar_class(opts_={})
98
- klass_ = Classes.new_class(self.class.const_get(:DATABASE_CONFIG))
99
- case opts_[:content]
50
+ def populate_ar_class(content_)
51
+ klass_ = create_ar_class
52
+ case content_
100
53
  when :latlon_point
101
54
  klass_.connection.create_table(:spatial_test) do |t_|
102
55
  t_.column 'latlon', :point
103
56
  end
104
57
  end
105
- @ar_class = klass_
58
+ klass_
106
59
  end
107
60
 
108
61
 
@@ -139,7 +92,7 @@ if ::File.exists?(::File.dirname(__FILE__)+'/database.yml')
139
92
 
140
93
 
141
94
  def test_set_and_get_point
142
- klass_ = create_ar_class(:content => :latlon_point)
95
+ klass_ = populate_ar_class(:latlon_point)
143
96
  obj_ = klass_.new
144
97
  assert_nil(obj_.latlon)
145
98
  obj_.latlon = @factory.point(1, 2)
@@ -149,7 +102,7 @@ if ::File.exists?(::File.dirname(__FILE__)+'/database.yml')
149
102
 
150
103
 
151
104
  def test_set_and_get_point_from_wkt
152
- klass_ = create_ar_class(:content => :latlon_point)
105
+ klass_ = populate_ar_class(:latlon_point)
153
106
  obj_ = klass_.new
154
107
  assert_nil(obj_.latlon)
155
108
  obj_.latlon = 'SRID=1000;POINT(1 2)'
@@ -159,7 +112,7 @@ if ::File.exists?(::File.dirname(__FILE__)+'/database.yml')
159
112
 
160
113
 
161
114
  def test_save_and_load_point
162
- klass_ = create_ar_class(:content => :latlon_point)
115
+ klass_ = populate_ar_class(:latlon_point)
163
116
  obj_ = klass_.new
164
117
  obj_.latlon = @factory.point(1, 2)
165
118
  obj_.save!
@@ -171,7 +124,7 @@ if ::File.exists?(::File.dirname(__FILE__)+'/database.yml')
171
124
 
172
125
 
173
126
  def test_save_and_load_point_from_wkt
174
- klass_ = create_ar_class(:content => :latlon_point)
127
+ klass_ = populate_ar_class(:latlon_point)
175
128
  obj_ = klass_.new
176
129
  obj_.latlon = 'SRID=1000;POINT(1 2)'
177
130
  obj_.save!
@@ -188,7 +141,8 @@ if ::File.exists?(::File.dirname(__FILE__)+'/database.yml')
188
141
  if ALL_DATABASES_CONFIG.include?('mysqlspatial')
189
142
  class TestMysqlSpatial < ::Test::Unit::TestCase # :nodoc:
190
143
  ADAPTER_NAME = 'mysqlspatial'
191
- include CommonTestMethods
144
+ include CommonSetupMethods
145
+ include CommonMysqlTestMethods
192
146
  end
193
147
  else
194
148
  puts "WARNING: Couldn't find mysqlspatial in database.yml. Skipping those tests."
@@ -199,7 +153,8 @@ if ::File.exists?(::File.dirname(__FILE__)+'/database.yml')
199
153
  if ALL_DATABASES_CONFIG.include?('mysql2spatial')
200
154
  class TestMysql2Spatial < ::Test::Unit::TestCase # :nodoc:
201
155
  ADAPTER_NAME = 'mysql2spatial'
202
- include CommonTestMethods
156
+ include CommonSetupMethods
157
+ include CommonMysqlTestMethods
203
158
  end
204
159
  else
205
160
  puts "WARNING: Couldn't find mysql2spatial in database.yml. Skipping those tests."
@@ -210,10 +165,6 @@ if ::File.exists?(::File.dirname(__FILE__)+'/database.yml')
210
165
  end
211
166
 
212
167
  end
168
+
213
169
  end
214
-
215
-
216
- else
217
- puts "WARNING: database.yml not found. Skipping ActiveRecord tests."
218
- puts " See tests/active_record/readme.txt for more info."
219
170
  end
@@ -0,0 +1,282 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Tests for the PostGIS ActiveRecord adapter
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2010 Daniel Azuma
7
+ #
8
+ # All rights reserved.
9
+ #
10
+ # Redistribution and use in source and binary forms, with or without
11
+ # modification, are permitted provided that the following conditions are met:
12
+ #
13
+ # * Redistributions of source code must retain the above copyright notice,
14
+ # this list of conditions and the following disclaimer.
15
+ # * Redistributions in binary form must reproduce the above copyright notice,
16
+ # this list of conditions and the following disclaimer in the documentation
17
+ # and/or other materials provided with the distribution.
18
+ # * Neither the name of the copyright holder, nor the names of any other
19
+ # contributors to this software, may be used to endorse or promote products
20
+ # derived from this software without specific prior written permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+ # -----------------------------------------------------------------------------
34
+ ;
35
+
36
+ require 'fileutils'
37
+ require 'test/unit'
38
+ require ::File.expand_path('common_setup_methods.rb', ::File.dirname(__FILE__))
39
+
40
+
41
+ module RGeo
42
+ module Tests # :nodoc:
43
+ module ActiveRecord # :nodoc:
44
+
45
+ if TESTS_AVAILABLE
46
+
47
+
48
+ if ALL_DATABASES_CONFIG.include?('postgis')
49
+
50
+ class TestPostGis < ::Test::Unit::TestCase # :nodoc:
51
+
52
+
53
+ ADAPTER_NAME = 'postgis'
54
+
55
+ include CommonSetupMethods
56
+
57
+
58
+ def populate_ar_class(content_)
59
+ klass_ = create_ar_class
60
+ case content_
61
+ when :latlon_point
62
+ klass_.connection.create_table(:spatial_test) do |t_|
63
+ t_.column 'latlon', :point, :srid => 4326
64
+ end
65
+ when :latlon_point_geographic
66
+ klass_.connection.create_table(:spatial_test) do |t_|
67
+ t_.column 'latlon', :point, :srid => 4326, :geographic => true
68
+ end
69
+ end
70
+ klass_
71
+ end
72
+
73
+
74
+ def test_postgis_available
75
+ connection_ = create_ar_class.connection
76
+ assert_equal('PostGIS', connection_.adapter_name)
77
+ assert_not_nil(connection_.postgis_lib_version)
78
+ end
79
+
80
+
81
+ def test_create_simple_geometry
82
+ klass_ = create_ar_class
83
+ klass_.connection.create_table(:spatial_test) do |t_|
84
+ t_.column 'latlon', :geometry
85
+ end
86
+ assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
87
+ col_ = klass_.columns.last
88
+ assert_equal(::RGeo::Feature::Geometry, col_.geometric_type)
89
+ assert_equal(false, col_.geographic?)
90
+ assert_equal(4326, col_.srid)
91
+ assert(klass_.cached_attributes.include?('latlon'))
92
+ klass_.connection.drop_table(:spatial_test)
93
+ assert_equal(0, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
94
+ end
95
+
96
+
97
+ def test_create_simple_geography
98
+ klass_ = create_ar_class
99
+ klass_.connection.create_table(:spatial_test) do |t_|
100
+ t_.column 'latlon', :geometry, :geographic => true
101
+ end
102
+ col_ = klass_.columns.last
103
+ assert_equal(::RGeo::Feature::Geometry, col_.geometric_type)
104
+ assert_equal(true, col_.geographic?)
105
+ assert_equal(4326, col_.srid)
106
+ assert(klass_.cached_attributes.include?('latlon'))
107
+ assert_equal(0, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
108
+ end
109
+
110
+
111
+ def test_create_point_geometry
112
+ klass_ = create_ar_class
113
+ klass_.connection.create_table(:spatial_test) do |t_|
114
+ t_.column 'latlon', :point
115
+ end
116
+ assert_equal(::RGeo::Feature::Point, klass_.columns.last.geometric_type)
117
+ assert(klass_.cached_attributes.include?('latlon'))
118
+ end
119
+
120
+
121
+ def test_create_geometry_with_index
122
+ klass_ = create_ar_class
123
+ klass_.connection.create_table(:spatial_test) do |t_|
124
+ t_.column 'latlon', :geometry
125
+ end
126
+ klass_.connection.change_table(:spatial_test) do |t_|
127
+ t_.index([:latlon], :spatial => true)
128
+ end
129
+ end
130
+
131
+
132
+ def test_set_and_get_point
133
+ klass_ = populate_ar_class(:latlon_point)
134
+ obj_ = klass_.new
135
+ assert_nil(obj_.latlon)
136
+ obj_.latlon = @factory.point(1, 2)
137
+ assert_equal(@factory.point(1, 2), obj_.latlon)
138
+ assert_equal(4326, obj_.latlon.srid)
139
+ end
140
+
141
+
142
+ def test_set_and_get_point_from_wkt
143
+ klass_ = populate_ar_class(:latlon_point)
144
+ obj_ = klass_.new
145
+ assert_nil(obj_.latlon)
146
+ obj_.latlon = 'POINT(1 2)'
147
+ assert_equal(@factory.point(1, 2), obj_.latlon)
148
+ assert_equal(4326, obj_.latlon.srid)
149
+ end
150
+
151
+
152
+ def test_save_and_load_point
153
+ klass_ = populate_ar_class(:latlon_point)
154
+ obj_ = klass_.new
155
+ obj_.latlon = @factory.point(1, 2)
156
+ obj_.save!
157
+ id_ = obj_.id
158
+ obj2_ = klass_.find(id_)
159
+ assert_equal(@factory.point(1, 2), obj2_.latlon)
160
+ assert_equal(4326, obj2_.latlon.srid)
161
+ assert_equal(true, ::RGeo::Geos.is_geos?(obj2_.latlon))
162
+ end
163
+
164
+
165
+ def test_save_and_load_geographic_point
166
+ klass_ = populate_ar_class(:latlon_point_geographic)
167
+ obj_ = klass_.new
168
+ obj_.latlon = @factory.point(1, 2)
169
+ obj_.save!
170
+ id_ = obj_.id
171
+ obj2_ = klass_.find(id_)
172
+ assert_equal(@geographic_factory.point(1, 2), obj2_.latlon)
173
+ assert_equal(4326, obj2_.latlon.srid)
174
+ assert_equal(false, ::RGeo::Geos.is_geos?(obj2_.latlon))
175
+ end
176
+
177
+
178
+ def test_save_and_load_point_from_wkt
179
+ klass_ = populate_ar_class(:latlon_point)
180
+ obj_ = klass_.new
181
+ obj_.latlon = 'POINT(1 2)'
182
+ obj_.save!
183
+ id_ = obj_.id
184
+ obj2_ = klass_.find(id_)
185
+ assert_equal(@factory.point(1, 2), obj2_.latlon)
186
+ assert_equal(4326, obj2_.latlon.srid)
187
+ end
188
+
189
+
190
+ def test_add_geometry_column
191
+ klass_ = create_ar_class
192
+ klass_.connection.create_table(:spatial_test) do |t_|
193
+ t_.column('latlon', :geometry)
194
+ end
195
+ klass_.connection.change_table(:spatial_test) do |t_|
196
+ t_.column('geom2', :point, :srid => 4326)
197
+ t_.column('name', :string)
198
+ end
199
+ assert_equal(2, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
200
+ cols_ = klass_.columns
201
+ assert_equal(::RGeo::Feature::Geometry, cols_[-3].geometric_type)
202
+ assert_equal(4326, cols_[-3].srid)
203
+ assert_equal(::RGeo::Feature::Point, cols_[-2].geometric_type)
204
+ assert_equal(4326, cols_[-2].srid)
205
+ assert_equal(false, cols_[-2].geographic?)
206
+ assert_nil(cols_[-1].geometric_type)
207
+ end
208
+
209
+
210
+ def test_add_geography_column
211
+ klass_ = create_ar_class
212
+ klass_.connection.create_table(:spatial_test) do |t_|
213
+ t_.column('latlon', :geometry)
214
+ end
215
+ klass_.connection.change_table(:spatial_test) do |t_|
216
+ t_.column('geom2', :point, :srid => 4326, :geographic => true)
217
+ t_.column('name', :string)
218
+ end
219
+ assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
220
+ cols_ = klass_.columns
221
+ assert_equal(::RGeo::Feature::Geometry, cols_[-3].geometric_type)
222
+ assert_equal(4326, cols_[-3].srid)
223
+ assert_equal(::RGeo::Feature::Point, cols_[-2].geometric_type)
224
+ assert_equal(4326, cols_[-2].srid)
225
+ assert_equal(true, cols_[-2].geographic?)
226
+ assert_nil(cols_[-1].geometric_type)
227
+ end
228
+
229
+
230
+ def test_drop_geometry_column
231
+ klass_ = create_ar_class
232
+ klass_.connection.create_table(:spatial_test) do |t_|
233
+ t_.column('latlon', :geometry)
234
+ t_.column('geom2', :point, :srid => 4326)
235
+ end
236
+ klass_.connection.change_table(:spatial_test) do |t_|
237
+ t_.remove('geom2')
238
+ end
239
+ assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
240
+ cols_ = klass_.columns
241
+ assert_equal(::RGeo::Feature::Geometry, cols_[-1].geometric_type)
242
+ assert_equal('latlon', cols_[-1].name)
243
+ assert_equal(4326, cols_[-1].srid)
244
+ assert_equal(false, cols_[-1].geographic?)
245
+ end
246
+
247
+
248
+ def test_drop_geography_column
249
+ klass_ = create_ar_class
250
+ klass_.connection.create_table(:spatial_test) do |t_|
251
+ t_.column('latlon', :geometry)
252
+ t_.column('geom2', :point, :srid => 4326, :geographic => true)
253
+ t_.column('geom3', :point, :srid => 4326)
254
+ end
255
+ klass_.connection.change_table(:spatial_test) do |t_|
256
+ t_.remove('geom2')
257
+ end
258
+ assert_equal(2, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
259
+ cols_ = klass_.columns
260
+ assert_equal(::RGeo::Feature::Point, cols_[-1].geometric_type)
261
+ assert_equal('geom3', cols_[-1].name)
262
+ assert_equal(false, cols_[-1].geographic?)
263
+ assert_equal(::RGeo::Feature::Geometry, cols_[-2].geometric_type)
264
+ assert_equal('latlon', cols_[-2].name)
265
+ assert_equal(false, cols_[-2].geographic?)
266
+ end
267
+
268
+
269
+ end
270
+
271
+ else
272
+ puts "WARNING: Couldn't find postgis in database.yml. Skipping those tests."
273
+ puts " See tests/active_record/readme.txt for more info."
274
+ end
275
+
276
+
277
+ end
278
+
279
+ end
280
+
281
+ end
282
+ end