activerecord-spatialite-adapter 0.2.3 → 0.3.0

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.
@@ -0,0 +1,64 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Railtie for SpatiaLite 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
+
37
+ require 'rails/railtie'
38
+
39
+
40
+ # :stopdoc:
41
+
42
+ module RGeo
43
+
44
+ module ActiveRecord
45
+
46
+ module SpatialiteAdapter
47
+
48
+
49
+ class Railtie < ::Rails::Railtie
50
+
51
+ rake_tasks do
52
+ load ::File.expand_path('databases.rake', ::File.dirname(__FILE__))
53
+ end
54
+
55
+ end
56
+
57
+
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+
64
+ # :startdoc:
@@ -0,0 +1,135 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # SpatiaLite adapter for ActiveRecord
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
+
37
+ # :stopdoc:
38
+
39
+ module ActiveRecord
40
+
41
+ module ConnectionAdapters
42
+
43
+ module SpatiaLiteAdapter
44
+
45
+
46
+ class SpatialColumn < ConnectionAdapters::SQLiteColumn
47
+
48
+
49
+ def initialize(name_, default_, sql_type_=nil, null_=true)
50
+ super(name_, default_, sql_type_, null_)
51
+ @geometric_type = ::RGeo::ActiveRecord.geometric_type_from_name(sql_type_)
52
+ @srid = 0
53
+ if type == :spatial
54
+ @limit = {:srid => @srid, :type => @geometric_type.type_name.underscore}
55
+ end
56
+ @ar_class = ::ActiveRecord::Base
57
+ end
58
+
59
+
60
+ def set_ar_class(val_)
61
+ @ar_class = val_
62
+ end
63
+
64
+ def set_srid(val_)
65
+ @srid = val_
66
+ if type == :spatial
67
+ @limit[:srid] = @srid
68
+ end
69
+ end
70
+
71
+
72
+ attr_reader :srid
73
+ attr_reader :geometric_type
74
+
75
+
76
+ def spatial?
77
+ type == :spatial
78
+ end
79
+
80
+
81
+ def klass
82
+ type == :spatial ? ::RGeo::Feature::Geometry : super
83
+ end
84
+
85
+
86
+ def type_cast(value_)
87
+ type == :spatial ? SpatialColumn.convert_to_geometry(value_, @ar_class, name, @srid) : super
88
+ end
89
+
90
+
91
+ def type_cast_code(var_name_)
92
+ type == :spatial ? "::ActiveRecord::ConnectionAdapters::SpatiaLiteAdapter::SpatialColumn.convert_to_geometry(#{var_name_}, self.class, #{name.inspect}, #{@srid})" : super
93
+ end
94
+
95
+
96
+ private
97
+
98
+
99
+ def simplified_type(sql_type_)
100
+ sql_type_ =~ /geometry|point|linestring|polygon/i ? :spatial : super
101
+ end
102
+
103
+
104
+ def self.convert_to_geometry(input_, ar_class_, column_name_, column_srid_)
105
+ case input_
106
+ when ::RGeo::Feature::Geometry
107
+ factory_ = ar_class_.rgeo_factory_for_column(column_name_, :srid => column_srid_)
108
+ ::RGeo::Feature.cast(input_, factory_)
109
+ when ::String
110
+ if input_.length == 0
111
+ nil
112
+ else
113
+ factory_ = ar_class_.rgeo_factory_for_column(column_name_, :srid => column_srid_)
114
+ if input_[0,1] == "\x00"
115
+ NativeFormatParser.new(factory_).parse(input_) rescue nil
116
+ else
117
+ ::RGeo::WKRep::WKTParser.new(factory_, :support_ewkt => true).parse(input_)
118
+ end
119
+ end
120
+ else
121
+ nil
122
+ end
123
+ end
124
+
125
+
126
+ end
127
+
128
+
129
+ end
130
+
131
+ end
132
+
133
+ end
134
+
135
+ # :startdoc:
@@ -0,0 +1,102 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # SpatiaLite adapter for ActiveRecord
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
+
37
+ # :stopdoc:
38
+
39
+ module ActiveRecord
40
+
41
+ module ConnectionAdapters
42
+
43
+ module SpatiaLiteAdapter
44
+
45
+
46
+ class SpatialTableDefinition < ConnectionAdapters::TableDefinition
47
+
48
+ def column(name_, type_, options_={})
49
+ if (info_ = @base.spatial_column_constructor(type_.to_sym))
50
+ options_[:type] ||= info_[:type] || type_
51
+ type_ = :spatial
52
+ end
53
+ super(name_, type_, options_)
54
+ if type_ == :spatial
55
+ col_ = self[name_]
56
+ col_.extend(SpatialColumnDefinitionMethods) unless col_.respond_to?(:srid)
57
+ options_.merge!(col_.limit) if col_.limit.is_a?(::Hash)
58
+ col_.set_spatial_type(options_[:type])
59
+ col_.set_srid(options_[:srid].to_i)
60
+ end
61
+ self
62
+ end
63
+
64
+ def to_sql
65
+ @columns.find_all{ |c_| !c_.respond_to?(:srid) }.map{ |c_| c_.to_sql } * ', '
66
+ end
67
+
68
+ def spatial_columns
69
+ @columns.find_all{ |c_| c_.respond_to?(:srid) }
70
+ end
71
+
72
+ end
73
+
74
+
75
+ module SpatialColumnDefinitionMethods # :nodoc:
76
+
77
+ def spatial_type
78
+ defined?(@spatial_type) && @spatial_type
79
+ end
80
+
81
+ def srid
82
+ defined?(@srid) ? @srid : 4326
83
+ end
84
+
85
+ def set_spatial_type(value_)
86
+ @spatial_type = value_.to_s
87
+ end
88
+
89
+ def set_srid(value_)
90
+ @srid = value_
91
+ end
92
+
93
+ end
94
+
95
+
96
+ end
97
+
98
+ end
99
+
100
+ end
101
+
102
+ # :startdoc:
@@ -0,0 +1,63 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # SpatiaLite adapter for ActiveRecord
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
+
37
+ begin
38
+ require 'versionomy'
39
+ rescue ::LoadError
40
+ end
41
+
42
+
43
+ module ActiveRecord
44
+
45
+ module ConnectionAdapters
46
+
47
+ module SpatiaLiteAdapter
48
+
49
+
50
+ # Current version of SpatiaLiteAdapter as a frozen string
51
+ VERSION_STRING = ::File.read(::File.dirname(__FILE__)+'/../../../../Version').strip.freeze
52
+
53
+ # Current version of SpatiaLiteAdapter as a Versionomy object, if the
54
+ # Versionomy gem is available; otherwise equal to VERSION_STRING.
55
+ VERSION = defined?(::Versionomy) ? ::Versionomy.parse(VERSION_STRING) : VERSION_STRING
56
+
57
+
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+
@@ -34,25 +34,6 @@
34
34
  ;
35
35
 
36
36
 
37
- require 'rails/railtie'
37
+ puts "WARNING: rgeo/active_record/spatialite_adapter/railtie is deprecated. Please use active_record/connection_adapters/spatialite_adapter/railtie."
38
38
 
39
-
40
- module RGeo
41
-
42
- module ActiveRecord
43
-
44
- module SpatialiteAdapter
45
-
46
- class Railtie < ::Rails::Railtie
47
-
48
- rake_tasks do
49
- load "rgeo/active_record/spatialite_adapter/databases.rake"
50
- end
51
-
52
- end
53
-
54
- end
55
-
56
- end
57
-
58
- end
39
+ require 'active_record/connection_adapters/spatialite_adapter/railtie'
data/test/tc_basic.rb CHANGED
@@ -76,6 +76,11 @@ module RGeo
76
76
  end
77
77
 
78
78
 
79
+ def test_version
80
+ assert_not_nil(::ActiveRecord::ConnectionAdapters::SpatiaLiteAdapter::VERSION)
81
+ end
82
+
83
+
79
84
  def test_meta_data_present
80
85
  result_ = DEFAULT_AR_CLASS.connection.select_value("SELECT COUNT(*) FROM spatial_ref_sys").to_i
81
86
  assert_not_equal(0, result_)
@@ -113,7 +118,7 @@ module RGeo
113
118
  klass_.connection.change_table(:spatial_test) do |t_|
114
119
  t_.index([:latlon], :spatial => true)
115
120
  end
116
- assert(klass_.connection.spatial_indexes(:spatial_test).last.spatial)
121
+ assert(klass_.connection.indexes(:spatial_test).last.spatial)
117
122
  assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='idx_spatial_test_latlon'").to_i)
118
123
  klass_.connection.drop_table(:spatial_test)
119
124
  assert_equal(0, klass_.connection.select_value("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='idx_spatial_test_latlon'").to_i)
@@ -206,29 +211,58 @@ module RGeo
206
211
  end
207
212
 
208
213
 
209
- def test_query_point
210
- klass_ = populate_ar_class(:latlon_point)
211
- obj_ = klass_.new
212
- obj_.latlon = @factory.point(1, 2)
213
- obj_.save!
214
- id_ = obj_.id
215
- obj2_ = klass_.where(:latlon => @factory.multi_point([@factory.point(1, 2)])).first
216
- assert_equal(id_, obj2_.id)
217
- obj3_ = klass_.where(:latlon => @factory.multi_point([@factory.point(2, 2)])).first
218
- assert_nil(obj3_)
214
+ def test_create_simple_geometry_using_shortcut
215
+ klass_ = create_ar_class
216
+ klass_.connection.create_table(:spatial_test) do |t_|
217
+ t_.geometry 'latlon'
218
+ end
219
+ assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
220
+ assert_equal(::RGeo::Feature::Geometry, klass_.columns.last.geometric_type)
221
+ assert(klass_.cached_attributes.include?('latlon'))
222
+ klass_.connection.drop_table(:spatial_test)
223
+ assert_equal(0, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
219
224
  end
220
225
 
221
226
 
222
- def test_query_point_wkt
223
- klass_ = populate_ar_class(:latlon_point)
224
- obj_ = klass_.new
225
- obj_.latlon = @factory.point(1, 2)
226
- obj_.save!
227
- id_ = obj_.id
228
- obj2_ = klass_.where(:latlon => 'POINT(1 2)').first
229
- assert_equal(id_, obj2_.id)
230
- obj3_ = klass_.where(:latlon => 'POINT(2 2)').first
231
- assert_nil(obj3_)
227
+ def test_create_point_geometry_using_shortcut
228
+ klass_ = create_ar_class
229
+ klass_.connection.create_table(:spatial_test) do |t_|
230
+ t_.point 'latlon'
231
+ end
232
+ assert_equal(::RGeo::Feature::Point, klass_.columns.last.geometric_type)
233
+ assert(klass_.cached_attributes.include?('latlon'))
234
+ end
235
+
236
+
237
+ def test_create_geometry_with_options
238
+ klass_ = create_ar_class
239
+ klass_.connection.create_table(:spatial_test) do |t_|
240
+ t_.column 'region', :polygon, :srid => 3785
241
+ end
242
+ assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
243
+ col_ = klass_.columns.last
244
+ assert_equal(::RGeo::Feature::Polygon, col_.geometric_type)
245
+ assert_equal(3785, col_.srid)
246
+ assert_equal({:srid => 3785, :type => 'polygon'}, col_.limit)
247
+ assert(klass_.cached_attributes.include?('region'))
248
+ klass_.connection.drop_table(:spatial_test)
249
+ assert_equal(0, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
250
+ end
251
+
252
+
253
+ def test_create_geometry_using_limit
254
+ klass_ = create_ar_class
255
+ klass_.connection.create_table(:spatial_test) do |t_|
256
+ t_.spatial 'region', :limit => {:srid => 3785, :type => :polygon}
257
+ end
258
+ assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
259
+ col_ = klass_.columns.last
260
+ assert_equal(::RGeo::Feature::Polygon, col_.geometric_type)
261
+ assert_equal(3785, col_.srid)
262
+ assert_equal({:srid => 3785, :type => 'polygon'}, col_.limit)
263
+ assert(klass_.cached_attributes.include?('region'))
264
+ klass_.connection.drop_table(:spatial_test)
265
+ assert_equal(0, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
232
266
  end
233
267
 
234
268