rgeo 0.1.22 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. data/History.rdoc +31 -0
  2. data/README.rdoc +53 -59
  3. data/Spatial_Programming_With_RGeo.rdoc +120 -67
  4. data/Version +1 -1
  5. data/ext/proj4_c_impl/extconf.rb +1 -1
  6. data/lib/rgeo.rb +80 -87
  7. data/lib/rgeo/cartesian.rb +0 -10
  8. data/lib/rgeo/coord_sys.rb +0 -7
  9. data/lib/rgeo/coord_sys/proj4.rb +2 -2
  10. data/lib/rgeo/error.rb +2 -6
  11. data/lib/rgeo/feature.rb +0 -7
  12. data/lib/rgeo/feature/curve.rb +5 -5
  13. data/lib/rgeo/feature/factory.rb +45 -5
  14. data/lib/rgeo/feature/factory_generator.rb +2 -4
  15. data/lib/rgeo/feature/geometry.rb +97 -27
  16. data/lib/rgeo/feature/geometry_collection.rb +3 -3
  17. data/lib/rgeo/feature/line_string.rb +3 -3
  18. data/lib/rgeo/feature/multi_curve.rb +2 -2
  19. data/lib/rgeo/feature/multi_surface.rb +3 -3
  20. data/lib/rgeo/feature/point.rb +6 -6
  21. data/lib/rgeo/feature/polygon.rb +4 -4
  22. data/lib/rgeo/feature/surface.rb +3 -3
  23. data/lib/rgeo/geographic.rb +0 -11
  24. data/lib/rgeo/geographic/factory.rb +5 -2
  25. data/lib/rgeo/geographic/interface.rb +1 -1
  26. data/lib/rgeo/geos.rb +0 -7
  27. data/lib/rgeo/geos/factory.rb +1 -1
  28. data/lib/rgeo/impl_helper.rb +0 -7
  29. data/lib/rgeo/wkrep.rb +0 -7
  30. data/lib/rgeo/wkrep/wkb_parser.rb +2 -2
  31. data/test/tc_oneoff.rb +0 -1
  32. metadata +7 -155
  33. data/lib/active_record/connection_adapters/mysql2spatial_adapter.rb +0 -124
  34. data/lib/active_record/connection_adapters/mysqlspatial_adapter.rb +0 -136
  35. data/lib/active_record/connection_adapters/postgis_adapter.rb +0 -426
  36. data/lib/active_record/connection_adapters/spatialite_adapter.rb +0 -488
  37. data/lib/rgeo/active_record/arel_modifications.rb +0 -78
  38. data/lib/rgeo/active_record/base_modifications.rb +0 -124
  39. data/lib/rgeo/active_record/common.rb +0 -166
  40. data/lib/rgeo/active_record/mysql_common.rb +0 -150
  41. data/lib/rgeo/all.rb +0 -49
  42. data/lib/rgeo/geo_json.rb +0 -60
  43. data/lib/rgeo/geo_json/coder.rb +0 -401
  44. data/lib/rgeo/geo_json/entities.rb +0 -285
  45. data/lib/rgeo/geo_json/interface.rb +0 -129
  46. data/lib/rgeo/shapefile.rb +0 -60
  47. data/lib/rgeo/shapefile/reader.rb +0 -898
  48. data/test/active_record/common_setup_methods.rb +0 -129
  49. data/test/active_record/readme.txt +0 -43
  50. data/test/active_record/tc_mysqlspatial.rb +0 -170
  51. data/test/active_record/tc_postgis.rb +0 -282
  52. data/test/active_record/tc_spatialite.rb +0 -198
  53. data/test/shapefile/shapelib_testcases/readme.txt +0 -11
  54. data/test/shapefile/shapelib_testcases/test.dbf +0 -0
  55. data/test/shapefile/shapelib_testcases/test.shp +0 -0
  56. data/test/shapefile/shapelib_testcases/test.shx +0 -0
  57. data/test/shapefile/shapelib_testcases/test0.shp +0 -0
  58. data/test/shapefile/shapelib_testcases/test0.shx +0 -0
  59. data/test/shapefile/shapelib_testcases/test1.shp +0 -0
  60. data/test/shapefile/shapelib_testcases/test1.shx +0 -0
  61. data/test/shapefile/shapelib_testcases/test10.shp +0 -0
  62. data/test/shapefile/shapelib_testcases/test10.shx +0 -0
  63. data/test/shapefile/shapelib_testcases/test11.shp +0 -0
  64. data/test/shapefile/shapelib_testcases/test11.shx +0 -0
  65. data/test/shapefile/shapelib_testcases/test12.shp +0 -0
  66. data/test/shapefile/shapelib_testcases/test12.shx +0 -0
  67. data/test/shapefile/shapelib_testcases/test13.shp +0 -0
  68. data/test/shapefile/shapelib_testcases/test13.shx +0 -0
  69. data/test/shapefile/shapelib_testcases/test2.shp +0 -0
  70. data/test/shapefile/shapelib_testcases/test2.shx +0 -0
  71. data/test/shapefile/shapelib_testcases/test3.shp +0 -0
  72. data/test/shapefile/shapelib_testcases/test3.shx +0 -0
  73. data/test/shapefile/shapelib_testcases/test4.shp +0 -0
  74. data/test/shapefile/shapelib_testcases/test4.shx +0 -0
  75. data/test/shapefile/shapelib_testcases/test5.shp +0 -0
  76. data/test/shapefile/shapelib_testcases/test5.shx +0 -0
  77. data/test/shapefile/shapelib_testcases/test6.shp +0 -0
  78. data/test/shapefile/shapelib_testcases/test6.shx +0 -0
  79. data/test/shapefile/shapelib_testcases/test7.shp +0 -0
  80. data/test/shapefile/shapelib_testcases/test7.shx +0 -0
  81. data/test/shapefile/shapelib_testcases/test8.shp +0 -0
  82. data/test/shapefile/shapelib_testcases/test8.shx +0 -0
  83. data/test/shapefile/shapelib_testcases/test9.shp +0 -0
  84. data/test/shapefile/shapelib_testcases/test9.shx +0 -0
  85. data/test/shapefile/tc_shapelib_tests.rb +0 -527
  86. data/test/tc_geojson.rb +0 -279
@@ -1,78 +0,0 @@
1
- # -----------------------------------------------------------------------------
2
- #
3
- # Mysqlgeo 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
- require 'arel'
38
-
39
-
40
- # :stopdoc:
41
-
42
- module Arel
43
-
44
- module Attributes
45
-
46
- class Geometry < Attribute; end
47
-
48
- class << self
49
- alias_method :for_without_geometry, :for
50
- def for(column_)
51
- column_.type == :geometry ? Geometry : for_without_geometry(column_)
52
- end
53
- end
54
-
55
- end
56
-
57
- module Visitors
58
-
59
- class Dot
60
- alias :visit_Arel_Attributes_Geometry :visit_Arel_Attribute
61
- alias :visit_RGeo_Feature_Geometry :visit_String
62
- end
63
-
64
- class ToSql
65
- alias :visit_Arel_Attributes_Geometry :visit_Arel_Attributes_Attribute
66
- alias :visit_RGeo_Feature_Geometry :visit_String
67
- end
68
-
69
- VISITORS['postgis'] = ::Arel::Visitors::PostgreSQL
70
- VISITORS['mysqlspatial'] = ::Arel::Visitors::MySQL
71
- VISITORS['mysql2spatial'] = ::Arel::Visitors::MySQL
72
- VISITORS['spatialite'] = ::Arel::Visitors::SQLite
73
-
74
- end
75
-
76
- end
77
-
78
- # :startdoc:
@@ -1,124 +0,0 @@
1
- # -----------------------------------------------------------------------------
2
- #
3
- # Mysqlgeo 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
- require 'active_record'
38
-
39
-
40
- # RGeo extensions to ActiveRecord are installed when one of the spatial
41
- # connection adapters is needed. These modifications require ActiveRecord
42
- # 3.0.3 or later.
43
-
44
- module ActiveRecord
45
-
46
-
47
- # RGeo extends ActiveRecord::Base to include the following new class
48
- # attributes. These attributes are inherited by subclasses, and can
49
- # be overridden in subclasses.
50
- #
51
- # === ActiveRecord::Base::rgeo_factory_generator
52
- #
53
- # The value of this attribute is a RGeo::Feature::FactoryGenerator
54
- # that is used to generate the proper factory when loading geometry
55
- # objects from the database. For example, if the data being loaded
56
- # has M but not Z coordinates, and an embedded SRID, then this
57
- # FactoryGenerator is called with the appropriate configuration to
58
- # obtain a factory with those properties. This factory is the one
59
- # associated with the actual geometry properties of the ActiveRecord
60
- # object.
61
-
62
- class Base
63
-
64
-
65
- self.attribute_types_cached_by_default << :geometry
66
-
67
-
68
- class_attribute :rgeo_factory_generator, :instance_writer => false
69
- self.rgeo_factory_generator = ::Proc.new do |config_|
70
- if config_.delete(:geographic)
71
- ::RGeo::Geographic.spherical_factory(config_)
72
- else
73
- ::RGeo::Cartesian.preferred_factory(config_)
74
- end
75
- end
76
-
77
-
78
- # This is a convenient way to set the rgeo_factory_generator by
79
- # passing a block.
80
-
81
- def self.to_generate_rgeo_factory(&block_)
82
- self.rgeo_factory_generator = block_
83
- end
84
-
85
-
86
- class << self
87
-
88
- # :stopdoc:
89
- alias_method :columns_without_rgeo_modification, :columns
90
- def columns
91
- unless defined?(@columns) && @columns
92
- columns_without_rgeo_modification.each do |column_|
93
- column_.set_ar_class(self) if column_.respond_to?(:set_ar_class)
94
- end
95
- end
96
- @columns
97
- end
98
- # :startdoc:
99
-
100
- end
101
-
102
- end
103
-
104
-
105
- module ConnectionAdapters # :nodoc:
106
-
107
- class TableDefinition # :nodoc:
108
-
109
- ::RGeo::ActiveRecord::GEOMETRY_TYPES.each do |type_|
110
- method_ = <<-END_METHOD
111
- def #{type_}(*args_)
112
- opts_ = args_.extract_options!
113
- args_.each{ |name_| column(name_, '#{type_}', opts_) }
114
- end
115
- END_METHOD
116
- class_eval(method_, __FILE__, __LINE__-5)
117
- end
118
-
119
- end
120
-
121
- end
122
-
123
-
124
- end
@@ -1,166 +0,0 @@
1
- # -----------------------------------------------------------------------------
2
- #
3
- # Common tools for spatial adapters 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
- require 'rgeo/feature'
38
- require 'rgeo/cartesian'
39
- require 'rgeo/geography'
40
-
41
-
42
- module RGeo
43
-
44
-
45
- # RGeo provides ActiveRecord connection adapters for common spatial
46
- # databases. They cause ActiveRecord to make spatial database fields
47
- # available as RGeo feature objects instead of internally-coded data.
48
- # They also provide extensions to the schema management APIs for
49
- # creating spatial columns and spatial indexes.
50
- #
51
- # You can specify and use these connection adapters in the same way you
52
- # use any other connection adapter, for example by specifying the
53
- # adapter name in a Rails application's database.yml file. You do not
54
- # need to require any files to gain access to these adapters. RGeo
55
- # makes them available to ActiveRecord automatically.
56
- #
57
- # The RGeo::ActiveRecord module itself is a namespace for the internal
58
- # adapter implementation. You generally do not need to interact with
59
- # this module yourself.
60
- #
61
- # Following is a list of the adapters supported by RGeo.
62
- #
63
- # === mysqlspatial
64
- #
65
- # An adapter based on the standard mysql adapter. It extends the stock
66
- # adapter to provide support for spatial columns in MySQL, mapping the
67
- # values properly to RGeo spatial objects. Like the standard mysql
68
- # adapter, this requires the mysql gem (version 2.8 or later).
69
- #
70
- # In a database.yml configuration, mysqlspatial uses the same config
71
- # parameters as the stock mysql adapter.
72
- #
73
- # STATUS: The mysqlspatial adapter is fairly complete, and there are no
74
- # known functionality holes at this time. However, it is not yet
75
- # well-tested.
76
- #
77
- # === mysql2spatial
78
- #
79
- # An adapter for MySQL spatial based on the mysql2 adapter. It requires
80
- # the mysql2 gem (version 0.2.6 or later).
81
- #
82
- # In a database.yml configuration, mysql2spatial uses the same config
83
- # parameters as the stock mysql2 adapter.
84
- #
85
- # STATUS: The mysql2spatial adapter is fairly complete, and there are
86
- # no known functionality holes at this time. However, it is not yet
87
- # well-tested.
88
- #
89
- # === spatialite
90
- #
91
- # An adapter for the SpatiaLite extension to Sqlite3. It is based on
92
- # the stock sqlite3 adapter, and requires the sqlite3-ruby gem.
93
- #
94
- # In a database.yml configuration, in addition to the config parameters
95
- # used by the stock sqlite3 adapter, spatialite recognizes the
96
- # following parameters:
97
- #
98
- # <tt>libspatialite</tt>::
99
- # The path to the libspatialite shared library. By default, the
100
- # adapter tries to look for this library in several usual places,
101
- # including /usr/local, /usr/local/spatialite, /opt/local, /usr,
102
- # and a few others. If it does not find libspatialite installed in
103
- # one of these locations, it will raise an exception. If your
104
- # library is installed in a different place, you can explicitly
105
- # provide the path using this configuration key.
106
- #
107
- # STATUS: The spatialite adapter works in principle, but there are a
108
- # few known holes in the functionality. Notably, things that require
109
- # the alter_table mechanism may not function properly, because the
110
- # current sqlite3 implementation doesn't properly preserve triggers.
111
- # This includes, among other things, removing columns. However, most
112
- # simple things work, including creating tables with geometric columns,
113
- # adding geometric columns to existing tables, and creating and
114
- # removing spatial R*tree indexes. Note that this adapter is not yet
115
- # well-tested.
116
- #
117
- # === postgis
118
- #
119
- # An adapter for the PostGIS extension to Postgresql. It is based on
120
- # the stock postgresql adapter, and requires the pg gem.
121
- #
122
- # In a database.yml configuration, postgis uses the same config
123
- # parameters as the stock postgresql adapter.
124
- #
125
- # STATUS: The postgis adapter works in principle, but there are a
126
- # few known holes in the functionality. Notably, getting index info
127
- # doesn't recognize spatial (GiST) indexes. Also be aware that this
128
- # adapter is not yet well-tested.
129
-
130
- module ActiveRecord
131
-
132
- # Additional column types for geometries.
133
- GEOMETRY_TYPES = [:geometry, :point, :line_string, :polygon, :geometry_collection, :multi_line_string, :multi_point, :multi_polygon].freeze
134
-
135
- module Common # :nodoc:
136
-
137
- class IndexDefinition < ::Struct.new(:table, :name, :unique, :columns, :lengths, :spatial) # :nodoc:
138
- end
139
-
140
- class << self
141
-
142
- def geometric_type_from_name(name_)
143
- case name_.downcase
144
- when 'geometry' then ::RGeo::Feature::Geometry
145
- when 'point' then ::RGeo::Feature::Point
146
- when 'linestring' then ::RGeo::Feature::LineString
147
- when 'polygon' then ::RGeo::Feature::Polygon
148
- when 'geometrycollection' then ::RGeo::Feature::GeometryCollection
149
- when 'multipoint' then ::RGeo::Feature::MultiPoint
150
- when 'multilinestring' then ::RGeo::Feature::MultiLineString
151
- when 'multipolygon' then ::RGeo::Feature::MultiPolygon
152
- else nil
153
- end
154
- end
155
-
156
- end
157
-
158
- end
159
-
160
- end
161
-
162
- end
163
-
164
-
165
- require 'rgeo/active_record/arel_modifications'
166
- require 'rgeo/active_record/base_modifications'
@@ -1,150 +0,0 @@
1
- # -----------------------------------------------------------------------------
2
- #
3
- # MysqlSpatial adapter common tools 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
- require 'rgeo/feature'
38
- require 'rgeo/cartesian'
39
- require 'rgeo/wkrep'
40
- require 'rgeo/active_record/common'
41
-
42
-
43
- module RGeo
44
-
45
- module ActiveRecord
46
-
47
-
48
- module MysqlCommon # :nodoc:
49
-
50
-
51
- module AdapterMethods # :nodoc:
52
-
53
-
54
- def quote(value_, column_=nil)
55
- if ::RGeo::Feature::Geometry.check_type(value_)
56
- "GeomFromWKB(0x#{::RGeo::WKRep::WKBGenerator.new(:hex_format => true).generate(value_)},#{value_.srid})"
57
- else
58
- super
59
- end
60
- end
61
-
62
-
63
- def add_index(table_name_, column_name_, options_={})
64
- if options_[:spatial]
65
- index_name_ = index_name(table_name_, :column => Array(column_name_))
66
- if ::Hash === options_
67
- index_name_ = options_[:name] || index_name_
68
- end
69
- execute "CREATE SPATIAL INDEX #{index_name_} ON #{table_name_} (#{Array(column_name_).join(", ")})"
70
- else
71
- super
72
- end
73
- end
74
-
75
-
76
- end
77
-
78
-
79
- module ColumnMethods # :nodoc:
80
-
81
-
82
- def initialize(name_, default_, sql_type_=nil, null_=true)
83
- super(name_, default_,sql_type_, null_)
84
- @geometric_type = ::RGeo::ActiveRecord::Common.geometric_type_from_name(sql_type_)
85
- @ar_class = ::ActiveRecord::Base
86
- end
87
-
88
-
89
- def set_ar_class(val_)
90
- @ar_class = val_
91
- end
92
-
93
-
94
- attr_reader :geometric_type
95
-
96
-
97
- def spatial?
98
- type == :geometry
99
- end
100
-
101
-
102
- def klass
103
- type == :geometry ? ::RGeo::Feature::Geometry : super
104
- end
105
-
106
-
107
- def type_cast(value_)
108
- type == :geometry ? ColumnMethods.string_to_geometry(value_, @ar_class) : super
109
- end
110
-
111
-
112
- def type_cast_code(var_name_)
113
- type == :geometry ? "::RGeo::ActiveRecord::MysqlCommon::ColumnMethods.string_to_geometry(#{var_name_}, self.class)" : super
114
- end
115
-
116
-
117
- private
118
-
119
- def simplified_type(sql_type_)
120
- sql_type_ =~ /geometry|point|linestring|polygon/i ? :geometry : super
121
- end
122
-
123
-
124
- def self.string_to_geometry(str_, ar_class_)
125
- case str_
126
- when ::RGeo::Feature::Geometry
127
- str_
128
- when ::String
129
- marker_ = str_[4,1]
130
- factory_generator_ = ar_class_.rgeo_factory_generator
131
- if marker_ == "\x00" || marker_ == "\x01"
132
- ::RGeo::WKRep::WKBParser.new(factory_generator_, :default_srid => str_[0,4].unpack(marker_ == "\x01" ? 'V' : 'N').first).parse(str_[4..-1])
133
- else
134
- ::RGeo::WKRep::WKTParser.new(factory_generator_, :support_ewkt => true).parse(str_)
135
- end
136
- else
137
- nil
138
- end
139
- end
140
-
141
-
142
- end
143
-
144
-
145
- end
146
-
147
-
148
- end
149
-
150
- end