activerecord-spatialite-adapter 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,15 +1,15 @@
1
1
  # -----------------------------------------------------------------------------
2
- #
2
+ #
3
3
  # SpatiaLite adapter for ActiveRecord
4
- #
4
+ #
5
5
  # -----------------------------------------------------------------------------
6
6
  # Copyright 2010 Daniel Azuma
7
- #
7
+ #
8
8
  # All rights reserved.
9
- #
9
+ #
10
10
  # Redistribution and use in source and binary forms, with or without
11
11
  # modification, are permitted provided that the following conditions are met:
12
- #
12
+ #
13
13
  # * Redistributions of source code must retain the above copyright notice,
14
14
  # this list of conditions and the following disclaimer.
15
15
  # * Redistributions in binary form must reproduce the above copyright notice,
@@ -18,7 +18,7 @@
18
18
  # * Neither the name of the copyright holder, nor the names of any other
19
19
  # contributors to this software, may be used to endorse or promote products
20
20
  # derived from this software without specific prior written permission.
21
- #
21
+ #
22
22
  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
23
  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
24
  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -35,29 +35,29 @@
35
35
 
36
36
 
37
37
  module ActiveRecord
38
-
38
+
39
39
  module ConnectionAdapters
40
-
40
+
41
41
  module SpatiaLiteAdapter
42
-
43
-
42
+
43
+
44
44
  # A utility class that parses the native (internal) SpatiaLite
45
45
  # format. This is used to read and return an attribute value as an
46
46
  # RGeo object.
47
-
47
+
48
48
  class NativeFormatParser
49
-
50
-
49
+
50
+
51
51
  # Create a parser that generates features using the given factory.
52
-
52
+
53
53
  def initialize(factory_)
54
54
  @factory = factory_
55
55
  end
56
-
57
-
56
+
57
+
58
58
  # Parse the given binary data and return an object.
59
59
  # Raises ::RGeo::Error::ParseError on failure.
60
-
60
+
61
61
  def parse(data_)
62
62
  if data_[0,1] =~ /[0-9a-fA-F]/
63
63
  data_ = [data_].pack('H*')
@@ -73,8 +73,8 @@ module ActiveRecord
73
73
  end
74
74
  obj_
75
75
  end
76
-
77
-
76
+
77
+
78
78
  def _parse_object(contained_) # :nodoc:
79
79
  _get_byte(contained_ ? 0x69 : 0x7c)
80
80
  type_code_ = _get_integer
@@ -100,27 +100,27 @@ module ActiveRecord
100
100
  raise ::RGeo::Error::ParseError, "Unknown type value: #{type_code_}."
101
101
  end
102
102
  end
103
-
104
-
103
+
104
+
105
105
  def _parse_line_string # :nodoc:
106
106
  count_ = _get_integer
107
107
  coords_ = _get_doubles(2 * count_)
108
108
  @factory.line_string((0...count_).map{ |i_| @factory.point(*coords_[2*i_,2]) })
109
109
  end
110
-
111
-
110
+
111
+
112
112
  def _start_scanner(data_) # :nodoc:
113
113
  @_data = data_
114
114
  @_len = data_.length
115
115
  @_pos = 38
116
116
  end
117
-
118
-
117
+
118
+
119
119
  def _clean_scanner # :nodoc:
120
120
  @_data = nil
121
121
  end
122
-
123
-
122
+
123
+
124
124
  def _get_byte(expect_=nil) # :nodoc:
125
125
  if @_pos + 1 > @_len
126
126
  raise ::RGeo::Error::ParseError, "Not enough bytes left to fulfill 1 byte"
@@ -133,8 +133,8 @@ module ActiveRecord
133
133
  end
134
134
  val_
135
135
  end
136
-
137
-
136
+
137
+
138
138
  def _get_integer # :nodoc:
139
139
  if @_pos + 4 > @_len
140
140
  raise ::RGeo::Error::ParseError, "Not enough bytes left to fulfill 1 integer"
@@ -143,8 +143,8 @@ module ActiveRecord
143
143
  @_pos += 4
144
144
  str_.unpack("#{@little_endian ? 'V' : 'N'}").first
145
145
  end
146
-
147
-
146
+
147
+
148
148
  def _get_doubles(count_) # :nodoc:
149
149
  len_ = 8 * count_
150
150
  if @_pos + len_ > @_len
@@ -154,13 +154,13 @@ module ActiveRecord
154
154
  @_pos += len_
155
155
  str_.unpack("#{@little_endian ? 'E' : 'G'}*")
156
156
  end
157
-
158
-
157
+
158
+
159
159
  end
160
-
161
-
160
+
161
+
162
162
  end
163
-
163
+
164
164
  end
165
-
165
+
166
166
  end
@@ -1,15 +1,15 @@
1
1
  # -----------------------------------------------------------------------------
2
- #
2
+ #
3
3
  # Railtie for SpatiaLite adapter
4
- #
4
+ #
5
5
  # -----------------------------------------------------------------------------
6
6
  # Copyright 2010 Daniel Azuma
7
- #
7
+ #
8
8
  # All rights reserved.
9
- #
9
+ #
10
10
  # Redistribution and use in source and binary forms, with or without
11
11
  # modification, are permitted provided that the following conditions are met:
12
- #
12
+ #
13
13
  # * Redistributions of source code must retain the above copyright notice,
14
14
  # this list of conditions and the following disclaimer.
15
15
  # * Redistributions in binary form must reproduce the above copyright notice,
@@ -18,7 +18,7 @@
18
18
  # * Neither the name of the copyright holder, nor the names of any other
19
19
  # contributors to this software, may be used to endorse or promote products
20
20
  # derived from this software without specific prior written permission.
21
- #
21
+ #
22
22
  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
23
  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
24
  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -40,25 +40,25 @@ require 'rails/railtie'
40
40
  # :stopdoc:
41
41
 
42
42
  module RGeo
43
-
43
+
44
44
  module ActiveRecord
45
-
45
+
46
46
  module SpatialiteAdapter
47
-
48
-
47
+
48
+
49
49
  class Railtie < ::Rails::Railtie
50
-
50
+
51
51
  rake_tasks do
52
52
  load ::File.expand_path('databases.rake', ::File.dirname(__FILE__))
53
53
  end
54
-
54
+
55
55
  end
56
-
57
-
56
+
57
+
58
58
  end
59
-
59
+
60
60
  end
61
-
61
+
62
62
  end
63
63
 
64
64
  # :startdoc:
@@ -1,15 +1,15 @@
1
1
  # -----------------------------------------------------------------------------
2
- #
2
+ #
3
3
  # SpatiaLite adapter for ActiveRecord
4
- #
4
+ #
5
5
  # -----------------------------------------------------------------------------
6
6
  # Copyright 2010 Daniel Azuma
7
- #
7
+ #
8
8
  # All rights reserved.
9
- #
9
+ #
10
10
  # Redistribution and use in source and binary forms, with or without
11
11
  # modification, are permitted provided that the following conditions are met:
12
- #
12
+ #
13
13
  # * Redistributions of source code must retain the above copyright notice,
14
14
  # this list of conditions and the following disclaimer.
15
15
  # * Redistributions in binary form must reproduce the above copyright notice,
@@ -18,7 +18,7 @@
18
18
  # * Neither the name of the copyright holder, nor the names of any other
19
19
  # contributors to this software, may be used to endorse or promote products
20
20
  # derived from this software without specific prior written permission.
21
- #
21
+ #
22
22
  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
23
  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
24
  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -37,15 +37,19 @@
37
37
  # :stopdoc:
38
38
 
39
39
  module ActiveRecord
40
-
40
+
41
41
  module ConnectionAdapters
42
-
42
+
43
43
  module SpatiaLiteAdapter
44
-
45
-
44
+
45
+
46
46
  class SpatialColumn < ConnectionAdapters::SQLiteColumn
47
-
48
-
47
+
48
+
49
+ FACTORY_SETTINGS_CACHE = {}
50
+
51
+
52
+
49
53
  def initialize(factory_settings_, table_name_, name_, default_, sql_type_=nil, null_=true)
50
54
  @factory_settings = factory_settings_
51
55
  @table_name = table_name_
@@ -55,31 +59,32 @@ module ActiveRecord
55
59
  if type == :spatial
56
60
  @limit = {:srid => @srid, :type => @geometric_type.type_name.underscore}
57
61
  end
62
+ FACTORY_SETTINGS_CACHE[factory_settings_.object_id] = factory_settings_
58
63
  end
59
-
60
-
64
+
65
+
61
66
  def set_srid(val_)
62
67
  @srid = val_
63
68
  if type == :spatial
64
69
  @limit[:srid] = @srid
65
70
  end
66
71
  end
67
-
68
-
72
+
73
+
69
74
  attr_reader :srid
70
75
  attr_reader :geometric_type
71
-
72
-
76
+
77
+
73
78
  def spatial?
74
79
  type == :spatial
75
80
  end
76
-
77
-
81
+
82
+
78
83
  def klass
79
84
  type == :spatial ? ::RGeo::Feature::Geometry : super
80
85
  end
81
-
82
-
86
+
87
+
83
88
  def type_cast(value_)
84
89
  if type == :spatial
85
90
  SpatialColumn.convert_to_geometry(value_, @factory_settings, @table_name, name, @srid)
@@ -87,27 +92,28 @@ module ActiveRecord
87
92
  super
88
93
  end
89
94
  end
90
-
91
-
95
+
96
+
92
97
  def type_cast_code(var_name_)
93
98
  if type == :spatial
94
99
  "::ActiveRecord::ConnectionAdapters::SpatiaLiteAdapter::SpatialColumn.convert_to_geometry("+
95
- "#{var_name_}, self.class.rgeo_factory_settings, self.class.table_name, "+
100
+ "#{var_name_}, ::ActiveRecord::ConnectionAdapters::SpatiaLiteAdapter::SpatialColumn::"+
101
+ "FACTORY_SETTINGS_CACHE[#{@factory_settings.object_id}], #{@table_name.inspect}, "+
96
102
  "#{name.inspect}, #{@srid})"
97
103
  else
98
104
  super
99
105
  end
100
106
  end
101
-
102
-
107
+
108
+
103
109
  private
104
-
105
-
110
+
111
+
106
112
  def simplified_type(sql_type_)
107
113
  sql_type_ =~ /geometry|point|linestring|polygon/i ? :spatial : super
108
114
  end
109
-
110
-
115
+
116
+
111
117
  def self.convert_to_geometry(input_, factory_settings_, table_name_, column_name_, column_srid_)
112
118
  case input_
113
119
  when ::RGeo::Feature::Geometry
@@ -128,15 +134,15 @@ module ActiveRecord
128
134
  nil
129
135
  end
130
136
  end
131
-
132
-
137
+
138
+
133
139
  end
134
-
135
-
140
+
141
+
136
142
  end
137
-
143
+
138
144
  end
139
-
145
+
140
146
  end
141
147
 
142
148
  # :startdoc:
@@ -1,15 +1,15 @@
1
1
  # -----------------------------------------------------------------------------
2
- #
2
+ #
3
3
  # SpatiaLite adapter for ActiveRecord
4
- #
4
+ #
5
5
  # -----------------------------------------------------------------------------
6
6
  # Copyright 2010 Daniel Azuma
7
- #
7
+ #
8
8
  # All rights reserved.
9
- #
9
+ #
10
10
  # Redistribution and use in source and binary forms, with or without
11
11
  # modification, are permitted provided that the following conditions are met:
12
- #
12
+ #
13
13
  # * Redistributions of source code must retain the above copyright notice,
14
14
  # this list of conditions and the following disclaimer.
15
15
  # * Redistributions in binary form must reproduce the above copyright notice,
@@ -18,7 +18,7 @@
18
18
  # * Neither the name of the copyright holder, nor the names of any other
19
19
  # contributors to this software, may be used to endorse or promote products
20
20
  # derived from this software without specific prior written permission.
21
- #
21
+ #
22
22
  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
23
  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
24
  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -37,14 +37,14 @@
37
37
  # :stopdoc:
38
38
 
39
39
  module ActiveRecord
40
-
40
+
41
41
  module ConnectionAdapters
42
-
42
+
43
43
  module SpatiaLiteAdapter
44
-
45
-
44
+
45
+
46
46
  class SpatialTableDefinition < ConnectionAdapters::TableDefinition
47
-
47
+
48
48
  def column(name_, type_, options_={})
49
49
  if (info_ = @base.spatial_column_constructor(type_.to_sym))
50
50
  options_[:type] ||= info_[:type] || type_
@@ -60,43 +60,43 @@ module ActiveRecord
60
60
  end
61
61
  self
62
62
  end
63
-
63
+
64
64
  def to_sql
65
65
  @columns.find_all{ |c_| !c_.respond_to?(:srid) }.map{ |c_| c_.to_sql } * ', '
66
66
  end
67
-
67
+
68
68
  def spatial_columns
69
69
  @columns.find_all{ |c_| c_.respond_to?(:srid) }
70
70
  end
71
-
71
+
72
72
  end
73
-
74
-
73
+
74
+
75
75
  module SpatialColumnDefinitionMethods # :nodoc:
76
-
76
+
77
77
  def spatial_type
78
78
  defined?(@spatial_type) && @spatial_type
79
79
  end
80
-
80
+
81
81
  def srid
82
82
  defined?(@srid) ? @srid : 4326
83
83
  end
84
-
84
+
85
85
  def set_spatial_type(value_)
86
86
  @spatial_type = value_.to_s
87
87
  end
88
-
88
+
89
89
  def set_srid(value_)
90
90
  @srid = value_
91
91
  end
92
-
92
+
93
93
  end
94
-
95
-
94
+
95
+
96
96
  end
97
-
97
+
98
98
  end
99
-
99
+
100
100
  end
101
101
 
102
102
  # :startdoc: