activerecord-postgis-adapter 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Documentation.rdoc +6 -8
- data/History.rdoc +5 -0
- data/README.rdoc +22 -22
- data/lib/active_record/connection_adapters/postgis_adapter.rb +9 -14
- data/lib/active_record/connection_adapters/postgis_adapter/rails4/create_connection.rb +11 -13
- data/lib/active_record/connection_adapters/postgis_adapter/rails4/main_adapter.rb +122 -102
- data/lib/active_record/connection_adapters/postgis_adapter/rails4/postgis_database_tasks.rb +20 -44
- data/lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb +48 -69
- data/lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_table_definition.rb +52 -55
- data/lib/active_record/connection_adapters/postgis_adapter/shared/arel_tosql.rb +2 -2
- data/lib/active_record/connection_adapters/postgis_adapter/shared/common_adapter_methods.rb +9 -9
- data/lib/active_record/connection_adapters/postgis_adapter/shared/setup.rb +1 -11
- data/lib/active_record/connection_adapters/postgis_adapter/version.rb +1 -1
- data/test/basic_test.rb +190 -0
- data/test/ddl_test.rb +279 -0
- data/test/{tc_nested_class.rb → nested_class_test.rb} +7 -15
- data/test/setup_test.rb +17 -0
- data/test/spatial_queries_test.rb +105 -0
- data/test/test_helper.rb +8 -0
- data/test/test_tasks.rb +111 -0
- metadata +24 -34
- data/test/tc_basic.rb +0 -212
- data/test/tc_ddl.rb +0 -279
- data/test/tc_spatial_queries.rb +0 -126
- data/test/tc_tasks.rb +0 -112
@@ -9,8 +9,8 @@ module Arel # :nodoc:
|
|
9
9
|
|
10
10
|
include ::RGeo::ActiveRecord::SpatialToSql
|
11
11
|
|
12
|
-
def st_func(
|
13
|
-
FUNC_MAP[
|
12
|
+
def st_func(standard_name)
|
13
|
+
FUNC_MAP[standard_name.downcase] || standard_name
|
14
14
|
end
|
15
15
|
|
16
16
|
alias_method :visit_in_spatial_context, :visit
|
@@ -9,16 +9,16 @@ module ActiveRecord # :nodoc:
|
|
9
9
|
DEFAULT_SRID = 0
|
10
10
|
|
11
11
|
module CommonAdapterMethods # :nodoc:
|
12
|
-
def set_rgeo_factory_settings(
|
13
|
-
@rgeo_factory_settings =
|
12
|
+
def set_rgeo_factory_settings(factory_settings)
|
13
|
+
@rgeo_factory_settings = factory_settings
|
14
14
|
end
|
15
15
|
|
16
16
|
def adapter_name
|
17
17
|
PostGISAdapter::ADAPTER_NAME
|
18
18
|
end
|
19
19
|
|
20
|
-
def spatial_column_constructor(
|
21
|
-
PostGISAdapter::SPATIAL_COLUMN_CONSTRUCTORS[
|
20
|
+
def spatial_column_constructor(name)
|
21
|
+
PostGISAdapter::SPATIAL_COLUMN_CONSTRUCTORS[name]
|
22
22
|
end
|
23
23
|
|
24
24
|
def postgis_lib_version
|
@@ -33,11 +33,11 @@ module ActiveRecord # :nodoc:
|
|
33
33
|
{:srtext_column => 'srtext', :proj4text_column => 'proj4text', :auth_name_column => 'auth_name', :auth_srid_column => 'auth_srid'}
|
34
34
|
end
|
35
35
|
|
36
|
-
def quote(
|
37
|
-
if ::RGeo::Feature::Geometry.check_type(
|
38
|
-
"'#{::RGeo::WKRep::WKBGenerator.new(:hex_format => true, :type_format => :ewkb, :emit_ewkb_srid => true).generate(
|
39
|
-
elsif
|
40
|
-
"'#{
|
36
|
+
def quote(value, column=nil)
|
37
|
+
if ::RGeo::Feature::Geometry.check_type(value)
|
38
|
+
"'#{::RGeo::WKRep::WKBGenerator.new(:hex_format => true, :type_format => :ewkb, :emit_ewkb_srid => true).generate(value)}'"
|
39
|
+
elsif value.is_a?(::RGeo::Cartesian::BoundingBox)
|
40
|
+
"'#{value.min_x},#{value.min_y},#{value.max_x},#{value.max_y}'::box"
|
41
41
|
else
|
42
42
|
super
|
43
43
|
end
|
@@ -1,21 +1,11 @@
|
|
1
1
|
module ActiveRecord # :nodoc:
|
2
|
-
|
3
2
|
module ConnectionAdapters # :nodoc:
|
4
|
-
|
5
3
|
module PostGISAdapter # :nodoc:
|
6
4
|
|
7
|
-
|
8
5
|
def self.initial_setup
|
9
|
-
|
10
|
-
ignore_tables_ = ::ActiveRecord::SchemaDumper.ignore_tables
|
11
|
-
gis_ignore_tables_.each do |table_|
|
12
|
-
ignore_tables_ << table_ unless ignore_tables_.include?(table_)
|
13
|
-
end
|
6
|
+
::ActiveRecord::SchemaDumper.ignore_tables |= %w[geometry_columns spatial_ref_sys layer topology]
|
14
7
|
end
|
15
8
|
|
16
|
-
|
17
9
|
end
|
18
|
-
|
19
10
|
end
|
20
|
-
|
21
11
|
end
|
data/test/basic_test.rb
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module RGeo
|
4
|
+
module ActiveRecord # :nodoc:
|
5
|
+
module PostGISAdapter # :nodoc:
|
6
|
+
module Tests # :nodoc:
|
7
|
+
class BasicTest < BASE_TEST_CLASS # :nodoc:
|
8
|
+
DATABASE_CONFIG_PATH = ::File.dirname(__FILE__)+'/database.yml'
|
9
|
+
OVERRIDE_DATABASE_CONFIG_PATH = ::File.dirname(__FILE__)+'/database_local.yml'
|
10
|
+
|
11
|
+
include AdapterTestHelper
|
12
|
+
|
13
|
+
define_test_methods do
|
14
|
+
|
15
|
+
def populate_ar_class(content)
|
16
|
+
klass = create_ar_class
|
17
|
+
case content
|
18
|
+
when :mercator_point
|
19
|
+
klass.connection.create_table(:spatial_test) do |t_|
|
20
|
+
t_.column 'latlon', :point, :srid => 3785
|
21
|
+
end
|
22
|
+
when :latlon_point_geographic
|
23
|
+
klass.connection.create_table(:spatial_test) do |t_|
|
24
|
+
t_.column 'latlon', :point, :srid => 4326, :geographic => true
|
25
|
+
end
|
26
|
+
when :no_constraints
|
27
|
+
klass.connection.create_table(:spatial_test) do |t_|
|
28
|
+
t_.column 'geo', :geometry, :no_constraints => true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
klass
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_version
|
35
|
+
refute_nil ::ActiveRecord::ConnectionAdapters::PostGISAdapter::VERSION
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_postgis_available
|
39
|
+
connection = create_ar_class.connection
|
40
|
+
assert_equal 'PostGIS', connection.adapter_name
|
41
|
+
refute_nil connection.postgis_lib_version
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_set_and_get_point
|
45
|
+
klass = populate_ar_class(:mercator_point)
|
46
|
+
obj = klass.new
|
47
|
+
assert_nil obj.latlon
|
48
|
+
obj.latlon = @factory.point(1.0, 2.0)
|
49
|
+
assert_equal @factory.point(1.0, 2.0), obj.latlon
|
50
|
+
assert_equal 3785, obj.latlon.srid
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_set_and_get_point_from_wkt
|
54
|
+
klass = populate_ar_class(:mercator_point)
|
55
|
+
obj = klass.new
|
56
|
+
assert_nil obj.latlon
|
57
|
+
obj.latlon = 'POINT(1 2)'
|
58
|
+
assert_equal @factory.point(1.0, 2.0), obj.latlon
|
59
|
+
assert_equal 3785, obj.latlon.srid
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_save_and_load_point
|
63
|
+
klass = populate_ar_class(:mercator_point)
|
64
|
+
obj = klass.new
|
65
|
+
obj.latlon = @factory.point(1.0, 2.0)
|
66
|
+
obj.save!
|
67
|
+
id = obj.id
|
68
|
+
obj2 = klass.find(id)
|
69
|
+
assert_equal @factory.point(1.0, 2.0), obj2.latlon
|
70
|
+
assert_equal 3785, obj2.latlon.srid
|
71
|
+
assert_equal true, ::RGeo::Geos.is_geos?(obj2.latlon)
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_save_and_load_geographic_point
|
75
|
+
klass = populate_ar_class(:latlon_point_geographic)
|
76
|
+
obj = klass.new
|
77
|
+
obj.latlon = @factory.point(1.0, 2.0)
|
78
|
+
obj.save!
|
79
|
+
id = obj.id
|
80
|
+
obj2 = klass.find(id)
|
81
|
+
assert_equal @geographic_factory.point(1.0, 2.0), obj2.latlon
|
82
|
+
assert_equal 4326, obj2.latlon.srid
|
83
|
+
assert_equal false, ::RGeo::Geos.is_geos?(obj2.latlon)
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_save_and_load_point_from_wkt
|
87
|
+
klass = populate_ar_class(:mercator_point)
|
88
|
+
obj = klass.new
|
89
|
+
obj.latlon = 'POINT(1 2)'
|
90
|
+
obj.save!
|
91
|
+
id = obj.id
|
92
|
+
obj2 = klass.find(id)
|
93
|
+
assert_equal @factory.point(1.0, 2.0), obj2.latlon
|
94
|
+
assert_equal 3785, obj2.latlon.srid
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_set_point_bad_wkt
|
98
|
+
klass = populate_ar_class(:mercator_point)
|
99
|
+
obj = klass.create(:latlon => 'POINT (x)')
|
100
|
+
assert_nil obj.latlon
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_set_point_wkt_wrong_type
|
104
|
+
klass = populate_ar_class(:mercator_point)
|
105
|
+
assert_raises(::ActiveRecord::StatementInvalid) do
|
106
|
+
klass.create(:latlon => 'LINESTRING(1 2, 3 4, 5 6)')
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_custom_factory
|
111
|
+
klass = create_ar_class
|
112
|
+
klass.connection.create_table(:spatial_test) do |t|
|
113
|
+
t.point(:latlon, :srid => 4326)
|
114
|
+
end
|
115
|
+
factory = ::RGeo::Geographic.simple_mercator_factory
|
116
|
+
klass.class_eval do
|
117
|
+
set_rgeo_factory_for_column(:latlon, factory)
|
118
|
+
end
|
119
|
+
rec_ = klass.new
|
120
|
+
rec_.latlon = 'POINT(-122 47)'
|
121
|
+
assert_equal factory, rec_.latlon.factory
|
122
|
+
rec_.save!
|
123
|
+
assert_equal factory, rec_.latlon.factory
|
124
|
+
rec2_ = klass.find(rec_.id)
|
125
|
+
assert_equal factory, rec2_.latlon.factory
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_readme_example
|
129
|
+
klass = create_ar_class
|
130
|
+
klass.connection.create_table(:spatial_test) do |t_|
|
131
|
+
t_.column(:shape, :geometry)
|
132
|
+
t_.line_string(:path, :srid => 3785)
|
133
|
+
t_.point(:latlon, :geographic => true)
|
134
|
+
end
|
135
|
+
klass.connection.change_table(:spatial_test) do |t_|
|
136
|
+
t_.index(:latlon, :spatial => true)
|
137
|
+
end
|
138
|
+
klass.class_eval do
|
139
|
+
self.rgeo_factory_generator = ::RGeo::Geos.method(:factory)
|
140
|
+
set_rgeo_factory_for_column(:latlon, ::RGeo::Geographic.spherical_factory)
|
141
|
+
end
|
142
|
+
rec_ = klass.new
|
143
|
+
rec_.latlon = 'POINT(-122 47)'
|
144
|
+
loc_ = rec_.latlon
|
145
|
+
assert_equal 47, loc_.latitude
|
146
|
+
rec_.shape = loc_
|
147
|
+
assert_equal true, ::RGeo::Geos.is_geos?(rec_.shape)
|
148
|
+
end
|
149
|
+
|
150
|
+
# no_constraints no longer supported in PostGIS 2.0
|
151
|
+
def _test_save_and_load_no_constraints
|
152
|
+
klass = populate_ar_class(:no_constraints)
|
153
|
+
factory1_ = ::RGeo::Cartesian.preferred_factory(:srid => 3785)
|
154
|
+
factory2_ = ::RGeo::Cartesian.preferred_factory(:srid => 2000)
|
155
|
+
obj = klass.new
|
156
|
+
obj.geo = factory1_.point(1.0, 2.0)
|
157
|
+
obj.save!
|
158
|
+
id = obj.id
|
159
|
+
obj2 = klass.find(id)
|
160
|
+
assert_equal factory1_.point(1.0, 2.0), obj2.geo
|
161
|
+
assert_equal 3785, obj2.geo.srid
|
162
|
+
obj2.geo = factory2_.point(3.0, 4.0)
|
163
|
+
obj2.save!
|
164
|
+
obj3 = klass.find(id)
|
165
|
+
assert_equal factory2_.point(3.0, 4.0), obj3.geo
|
166
|
+
assert_equal 2000, obj3.geo.srid
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_point_to_json
|
170
|
+
klass = populate_ar_class(:mercator_point)
|
171
|
+
obj = klass.new
|
172
|
+
assert_match(/"latlon":null/, obj.to_json)
|
173
|
+
obj.latlon = @factory.point(1.0, 2.0)
|
174
|
+
assert_match(/"latlon":"POINT\s\(1\.0\s2\.0\)"/, obj.to_json)
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_custom_column
|
178
|
+
klass = populate_ar_class(:mercator_point)
|
179
|
+
rec = klass.new
|
180
|
+
rec.latlon = 'POINT(0 0)'
|
181
|
+
rec.save
|
182
|
+
refute_nil klass.select("CURRENT_TIMESTAMP as ts").first.ts
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
data/test/ddl_test.rb
ADDED
@@ -0,0 +1,279 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module RGeo
|
4
|
+
module ActiveRecord # :nodoc:
|
5
|
+
module PostGISAdapter # :nodoc:
|
6
|
+
module Tests # :nodoc:
|
7
|
+
class DDLTest < BASE_TEST_CLASS # :nodoc:
|
8
|
+
DATABASE_CONFIG_PATH = ::File.dirname(__FILE__) + '/database.yml'
|
9
|
+
OVERRIDE_DATABASE_CONFIG_PATH = ::File.dirname(__FILE__) + '/database_local.yml'
|
10
|
+
|
11
|
+
include AdapterTestHelper
|
12
|
+
|
13
|
+
define_test_methods do
|
14
|
+
def test_create_simple_geometry
|
15
|
+
klass = create_ar_class
|
16
|
+
klass.connection.create_table(:spatial_test) do |t|
|
17
|
+
t.column 'latlon', :geometry
|
18
|
+
end
|
19
|
+
assert_equal(1, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
20
|
+
col = klass.columns.last
|
21
|
+
assert_equal(::RGeo::Feature::Geometry, col.geometric_type)
|
22
|
+
assert_equal(true, col.has_spatial_constraints?)
|
23
|
+
assert_equal(false, col.geographic?)
|
24
|
+
assert_equal(0, col.srid)
|
25
|
+
assert(klass.cached_attributes.include?('latlon'))
|
26
|
+
klass.connection.drop_table(:spatial_test)
|
27
|
+
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
28
|
+
end
|
29
|
+
|
30
|
+
# no_constraints no longer supported in PostGIS 2.0
|
31
|
+
def _test_create_no_constraints_geometry
|
32
|
+
klass = create_ar_class
|
33
|
+
klass.connection.create_table(:spatial_test) do |t|
|
34
|
+
t.column 'geom', :geometry, :limit => {:no_constraints => true}
|
35
|
+
end
|
36
|
+
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
37
|
+
col = klass.columns.last
|
38
|
+
assert_equal(::RGeo::Feature::Geometry, col.geometric_type)
|
39
|
+
assert_equal(false, col.geographic?)
|
40
|
+
assert_equal(false, col.has_spatial_constraints?)
|
41
|
+
assert_nil(col.srid)
|
42
|
+
assert(klass.cached_attributes.include?('geom'))
|
43
|
+
klass.connection.drop_table(:spatial_test)
|
44
|
+
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_create_simple_geography
|
48
|
+
klass = create_ar_class
|
49
|
+
klass.connection.create_table(:spatial_test) do |t|
|
50
|
+
t.column 'latlon', :geometry, :geographic => true
|
51
|
+
end
|
52
|
+
col = klass.columns.last
|
53
|
+
assert_equal(::RGeo::Feature::Geometry, col.geometric_type)
|
54
|
+
assert_equal(true, col.has_spatial_constraints?)
|
55
|
+
assert_equal(true, col.geographic?)
|
56
|
+
assert_equal(4326, col.srid)
|
57
|
+
assert(klass.cached_attributes.include?('latlon'))
|
58
|
+
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_create_point_geometry
|
62
|
+
klass = create_ar_class
|
63
|
+
klass.connection.create_table(:spatial_test) do |t|
|
64
|
+
t.column 'latlon', :point
|
65
|
+
end
|
66
|
+
assert_equal(::RGeo::Feature::Point, klass.columns.last.geometric_type)
|
67
|
+
assert(klass.cached_attributes.include?('latlon'))
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_create_geometry_with_index
|
71
|
+
klass = create_ar_class
|
72
|
+
klass.connection.create_table(:spatial_test) do |t|
|
73
|
+
t.column 'latlon', :geometry
|
74
|
+
end
|
75
|
+
klass.connection.change_table(:spatial_test) do |t|
|
76
|
+
t.index([:latlon], :spatial => true)
|
77
|
+
end
|
78
|
+
assert(klass.connection.indexes(:spatial_test).last.spatial)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_add_geometry_column
|
82
|
+
klass = create_ar_class
|
83
|
+
klass.connection.create_table(:spatial_test) do |t|
|
84
|
+
t.column('latlon', :geometry)
|
85
|
+
end
|
86
|
+
klass.connection.change_table(:spatial_test) do |t|
|
87
|
+
t.column('geom2', :point, :srid => 4326)
|
88
|
+
t.column('name', :string)
|
89
|
+
end
|
90
|
+
assert_equal(2, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
91
|
+
cols_ = klass.columns
|
92
|
+
assert_equal(::RGeo::Feature::Geometry, cols_[-3].geometric_type)
|
93
|
+
assert_equal(0, cols_[-3].srid)
|
94
|
+
assert_equal(true, cols_[-3].has_spatial_constraints?)
|
95
|
+
assert_equal(::RGeo::Feature::Point, cols_[-2].geometric_type)
|
96
|
+
assert_equal(4326, cols_[-2].srid)
|
97
|
+
assert_equal(false, cols_[-2].geographic?)
|
98
|
+
assert_equal(true, cols_[-2].has_spatial_constraints?)
|
99
|
+
assert_nil(cols_[-1].geometric_type)
|
100
|
+
assert_equal(false, cols_[-1].has_spatial_constraints?)
|
101
|
+
end
|
102
|
+
|
103
|
+
# no_constraints no longer supported in PostGIS 2.0
|
104
|
+
def _test_add_no_constraints_geometry_column
|
105
|
+
klass = create_ar_class
|
106
|
+
klass.connection.create_table(:spatial_test) do |t|
|
107
|
+
t.column('latlon', :geometry)
|
108
|
+
end
|
109
|
+
klass.connection.change_table(:spatial_test) do |t|
|
110
|
+
t.column('geom2', :geometry, :no_constraints => true)
|
111
|
+
t.column('name', :string)
|
112
|
+
end
|
113
|
+
assert_equal(1, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
114
|
+
cols_ = klass.columns
|
115
|
+
assert_equal(::RGeo::Feature::Geometry, cols_[-3].geometric_type)
|
116
|
+
assert_equal(0, cols_[-3].srid)
|
117
|
+
assert_equal(true, cols_[-3].has_spatial_constraints?)
|
118
|
+
assert_equal(::RGeo::Feature::Geometry, cols_[-2].geometric_type)
|
119
|
+
assert_nil(cols_[-2].srid)
|
120
|
+
assert_equal(false, cols_[-2].geographic?)
|
121
|
+
assert_equal(false, cols_[-2].has_spatial_constraints?)
|
122
|
+
assert_nil(cols_[-1].geometric_type)
|
123
|
+
assert_equal(false, cols_[-1].has_spatial_constraints?)
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_add_geography_column
|
127
|
+
klass = create_ar_class
|
128
|
+
klass.connection.create_table(:spatial_test) do |t|
|
129
|
+
t.column('latlon', :geometry)
|
130
|
+
end
|
131
|
+
klass.connection.change_table(:spatial_test) do |t|
|
132
|
+
t.column('geom2', :point, :srid => 4326, :geographic => true)
|
133
|
+
t.column('name', :string)
|
134
|
+
end
|
135
|
+
assert_equal(1, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
136
|
+
cols_ = klass.columns
|
137
|
+
assert_equal(::RGeo::Feature::Geometry, cols_[-3].geometric_type)
|
138
|
+
assert_equal(0, cols_[-3].srid)
|
139
|
+
assert_equal(true, cols_[-3].has_spatial_constraints?)
|
140
|
+
assert_equal(::RGeo::Feature::Point, cols_[-2].geometric_type)
|
141
|
+
assert_equal(4326, cols_[-2].srid)
|
142
|
+
assert_equal(true, cols_[-2].geographic?)
|
143
|
+
assert_equal(true, cols_[-2].has_spatial_constraints?)
|
144
|
+
assert_nil(cols_[-1].geometric_type)
|
145
|
+
assert_equal(false, cols_[-1].has_spatial_constraints?)
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_drop_geometry_column
|
149
|
+
klass = create_ar_class
|
150
|
+
klass.connection.create_table(:spatial_test) do |t|
|
151
|
+
t.column('latlon', :geometry)
|
152
|
+
t.column('geom2', :point, :srid => 4326)
|
153
|
+
end
|
154
|
+
klass.connection.change_table(:spatial_test) do |t|
|
155
|
+
t.remove('geom2')
|
156
|
+
end
|
157
|
+
assert_equal(1, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
158
|
+
cols_ = klass.columns
|
159
|
+
assert_equal(::RGeo::Feature::Geometry, cols_[-1].geometric_type)
|
160
|
+
assert_equal('latlon', cols_[-1].name)
|
161
|
+
assert_equal(0, cols_[-1].srid)
|
162
|
+
assert_equal(false, cols_[-1].geographic?)
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_drop_geography_column
|
166
|
+
klass = create_ar_class
|
167
|
+
klass.connection.create_table(:spatial_test) do |t|
|
168
|
+
t.column('latlon', :geometry)
|
169
|
+
t.column('geom2', :point, :srid => 4326, :geographic => true)
|
170
|
+
t.column('geom3', :point, :srid => 4326)
|
171
|
+
end
|
172
|
+
klass.connection.change_table(:spatial_test) do |t|
|
173
|
+
t.remove('geom2')
|
174
|
+
end
|
175
|
+
assert_equal(2, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
176
|
+
cols_ = klass.columns
|
177
|
+
assert_equal(::RGeo::Feature::Point, cols_[-1].geometric_type)
|
178
|
+
assert_equal('geom3', cols_[-1].name)
|
179
|
+
assert_equal(false, cols_[-1].geographic?)
|
180
|
+
assert_equal(::RGeo::Feature::Geometry, cols_[-2].geometric_type)
|
181
|
+
assert_equal('latlon', cols_[-2].name)
|
182
|
+
assert_equal(false, cols_[-2].geographic?)
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_create_simple_geometry_using_shortcut
|
186
|
+
klass = create_ar_class
|
187
|
+
klass.connection.create_table(:spatial_test) do |t|
|
188
|
+
t.geometry 'latlon'
|
189
|
+
end
|
190
|
+
assert_equal(1, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
191
|
+
col = klass.columns.last
|
192
|
+
assert_equal(::RGeo::Feature::Geometry, col.geometric_type)
|
193
|
+
assert_equal(false, col.geographic?)
|
194
|
+
assert_equal(0, col.srid)
|
195
|
+
assert(klass.cached_attributes.include?('latlon'))
|
196
|
+
klass.connection.drop_table(:spatial_test)
|
197
|
+
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
198
|
+
end
|
199
|
+
|
200
|
+
# no_constraints no longer supported in PostGIS 2.0
|
201
|
+
def _test_create_no_constraints_geometry_using_shortcut
|
202
|
+
klass = create_ar_class
|
203
|
+
klass.connection.create_table(:spatial_test) do |t|
|
204
|
+
t.spatial 'geom', :no_constraints => true
|
205
|
+
end
|
206
|
+
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
207
|
+
col = klass.columns.last
|
208
|
+
assert_equal(::RGeo::Feature::Geometry, col.geometric_type)
|
209
|
+
assert_equal(false, col.geographic?)
|
210
|
+
assert_nil(col.srid)
|
211
|
+
assert(klass.cached_attributes.include?('geom'))
|
212
|
+
klass.connection.drop_table(:spatial_test)
|
213
|
+
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
214
|
+
end
|
215
|
+
|
216
|
+
def test_create_simple_geography_using_shortcut
|
217
|
+
klass = create_ar_class
|
218
|
+
klass.connection.create_table(:spatial_test) do |t|
|
219
|
+
t.geometry 'latlon', :geographic => true
|
220
|
+
end
|
221
|
+
col = klass.columns.last
|
222
|
+
assert_equal(::RGeo::Feature::Geometry, col.geometric_type)
|
223
|
+
assert_equal(true, col.geographic?)
|
224
|
+
assert_equal(4326, col.srid)
|
225
|
+
assert(klass.cached_attributes.include?('latlon'))
|
226
|
+
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
227
|
+
end
|
228
|
+
|
229
|
+
def test_create_point_geometry_using_shortcut
|
230
|
+
klass = create_ar_class
|
231
|
+
klass.connection.create_table(:spatial_test) do |t|
|
232
|
+
t.point 'latlon'
|
233
|
+
end
|
234
|
+
assert_equal(::RGeo::Feature::Point, klass.columns.last.geometric_type)
|
235
|
+
assert(klass.cached_attributes.include?('latlon'))
|
236
|
+
end
|
237
|
+
|
238
|
+
def test_create_geometry_with_options
|
239
|
+
klass = create_ar_class
|
240
|
+
klass.connection.create_table(:spatial_test) do |t|
|
241
|
+
t.column 'region', :polygon, :has_m => true, :srid => 3785
|
242
|
+
end
|
243
|
+
assert_equal(1, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
244
|
+
col = klass.columns.last
|
245
|
+
assert_equal(::RGeo::Feature::Polygon, col.geometric_type)
|
246
|
+
assert_equal(false, col.geographic?)
|
247
|
+
assert_equal(false, col.has_z?)
|
248
|
+
assert_equal(true, col.has_m?)
|
249
|
+
assert_equal(3785, col.srid)
|
250
|
+
assert_equal({:has_m => true, :type => 'polygon', :srid => 3785}, col.limit)
|
251
|
+
assert(klass.cached_attributes.include?('region'))
|
252
|
+
klass.connection.drop_table(:spatial_test)
|
253
|
+
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_create_geometry_using_limit
|
257
|
+
klass = create_ar_class
|
258
|
+
klass.connection.create_table(:spatial_test) do |t|
|
259
|
+
t.spatial 'region', :limit => {:has_m => true, :srid => 3785, :type => :polygon}
|
260
|
+
end
|
261
|
+
assert_equal(1, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
262
|
+
col = klass.columns.last
|
263
|
+
assert_equal(::RGeo::Feature::Polygon, col.geometric_type)
|
264
|
+
assert_equal(false, col.geographic?)
|
265
|
+
assert_equal(false, col.has_z)
|
266
|
+
assert_equal(true, col.has_m)
|
267
|
+
assert_equal(3785, col.srid)
|
268
|
+
assert_equal({:has_m => true, :type => 'polygon', :srid => 3785}, col.limit)
|
269
|
+
assert(klass.cached_attributes.include?('region'))
|
270
|
+
klass.connection.drop_table(:spatial_test)
|
271
|
+
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
272
|
+
end
|
273
|
+
|
274
|
+
end
|
275
|
+
end
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|