GeoRuby 1.1.2 → 1.2.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.
- data/README +16 -13
- data/lib/geo_ruby/shp4r/dbf.rb +234 -0
- data/lib/geo_ruby/shp4r/shp.rb +301 -0
- data/lib/geo_ruby/simple_features/geometry.rb +1 -0
- data/lib/geo_ruby.rb +1 -1
- data/rakefile.rb +4 -4
- data/test/data/point.dbf +0 -0
- data/test/data/point.shp +0 -0
- data/test/data/point.shx +0 -0
- data/test/data/polygon.dbf +0 -0
- data/test/data/polygon.shp +0 -0
- data/test/data/polygon.shx +0 -0
- data/test/data/polyline.dbf +0 -0
- data/test/data/polyline.shp +0 -0
- data/test/data/polyline.shx +0 -0
- data/test/test_shp.rb +77 -0
- data/tools/db.yml +6 -0
- data/tools/lib/spatial_adapter/MIT-LICENSE +7 -0
- data/tools/lib/spatial_adapter/README +116 -0
- data/tools/lib/spatial_adapter/init.rb +10 -0
- data/tools/lib/spatial_adapter/lib/common_spatial_adapter.rb +175 -0
- data/tools/lib/spatial_adapter/lib/mysql_spatial_adapter.rb +143 -0
- data/tools/lib/spatial_adapter/lib/post_gis_adapter.rb +333 -0
- data/tools/lib/spatial_adapter/rakefile.rb +35 -0
- data/tools/lib/spatial_adapter/test/access_mysql_test.rb +87 -0
- data/tools/lib/spatial_adapter/test/access_postgis_test.rb +151 -0
- data/tools/lib/spatial_adapter/test/common/common_mysql.rb +18 -0
- data/tools/lib/spatial_adapter/test/common/common_postgis.rb +19 -0
- data/tools/lib/spatial_adapter/test/db/database_mysql.yml +5 -0
- data/tools/lib/spatial_adapter/test/db/database_postgis.yml +4 -0
- data/tools/lib/spatial_adapter/test/find_mysql_test.rb +64 -0
- data/tools/lib/spatial_adapter/test/find_postgis_test.rb +65 -0
- data/tools/lib/spatial_adapter/test/migration_mysql_test.rb +136 -0
- data/tools/lib/spatial_adapter/test/migration_postgis_test.rb +170 -0
- data/tools/lib/spatial_adapter/test/models/models_mysql.rb +25 -0
- data/tools/lib/spatial_adapter/test/models/models_postgis.rb +41 -0
- data/tools/lib/spatial_adapter/test/schema/schema_mysql.rb +40 -0
- data/tools/lib/spatial_adapter/test/schema/schema_postgis.rb +69 -0
- data/tools/shp2sql.rb +91 -0
- metadata +47 -4
@@ -88,6 +88,7 @@ module GeoRuby#:nodoc:
|
|
88
88
|
|
89
89
|
#Outputs the geometry in georss format.
|
90
90
|
#Assumes the geometries are in latlon format, with x as lon and y as lat.
|
91
|
+
#Pass the <tt>:dialect</tt> option to swhit format. Possible values are: <tt>:simple</tt> (default), <tt>:w3cgeo</tt> and <tt>:gml</tt>.
|
91
92
|
def as_georss(options = {})
|
92
93
|
dialect= options[:dialect] || :simple
|
93
94
|
case(dialect)
|
data/lib/geo_ruby.rb
CHANGED
data/rakefile.rb
CHANGED
@@ -24,18 +24,18 @@ spec = Gem::Specification::new do |s|
|
|
24
24
|
s.platform = Gem::Platform::RUBY
|
25
25
|
|
26
26
|
s.name = 'GeoRuby'
|
27
|
-
s.version = "1.
|
27
|
+
s.version = "1.2.0"
|
28
28
|
s.summary = "Ruby data holder for OGC Simple Features"
|
29
29
|
s.description = <<EOF
|
30
30
|
GeoRuby is intended as a holder for data returned from PostGIS and MySQL Spatial queries. The data model roughly follows the OGC "Simple Features for SQL" specification (see www.opengis.org/docs/99-049.pdf), although without any kind of advanced functionalities (such as geometric operators or reprojections)
|
31
31
|
EOF
|
32
32
|
s.author = 'Guilhem Vellut'
|
33
|
-
s.email = 'guilhem.vellut
|
34
|
-
s.homepage = "http://thepochisuperstarmegashow.com/projects"
|
33
|
+
s.email = 'guilhem.vellut@gmail.com'
|
34
|
+
s.homepage = "http://thepochisuperstarmegashow.com/projects/"
|
35
35
|
|
36
36
|
s.requirements << 'none'
|
37
37
|
s.require_path = 'lib'
|
38
|
-
s.files = FileList["lib/**/*.rb", "test/**/*.rb", "README","MIT-LICENSE","rakefile.rb"]
|
38
|
+
s.files = FileList["lib/**/*.rb", "test/**/*.rb", "README","MIT-LICENSE","rakefile.rb","test/data/*.shp","test/data/*.dbf","test/data/*.shx","tools/**/*.yml","tools/**/*.rb","tools/lib/**/*"]
|
39
39
|
s.test_files = FileList['test/test*.rb']
|
40
40
|
|
41
41
|
s.has_rdoc = true
|
data/test/data/point.dbf
ADDED
Binary file
|
data/test/data/point.shp
ADDED
Binary file
|
data/test/data/point.shx
ADDED
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/test/test_shp.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
|
3
|
+
require 'geo_ruby'
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
include GeoRuby::SimpleFeatures
|
7
|
+
include GeoRuby::Shp4r
|
8
|
+
|
9
|
+
class TestShp < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def test_point
|
12
|
+
shpfile = ShpFile.open(File.dirname(__FILE__) + '/data/point.shp')
|
13
|
+
|
14
|
+
assert_equal(2,shpfile.record_count)
|
15
|
+
assert_equal(1,shpfile.fields.length)
|
16
|
+
assert_equal(ShpType::POINT,shpfile.shp_type)
|
17
|
+
field = shpfile.fields[0]
|
18
|
+
assert_equal("Hoyoyo",field.name)
|
19
|
+
assert_equal("N",field.type)
|
20
|
+
|
21
|
+
record1 = shpfile[0]
|
22
|
+
assert(record1.geometry.kind_of?(Point))
|
23
|
+
assert_in_delta(-90.08375,record1.geometry.x,0.00001)
|
24
|
+
assert_in_delta(34.39996,record1.geometry.y,0.00001)
|
25
|
+
assert_equal(6,record1.data['Hoyoyo'])
|
26
|
+
|
27
|
+
record2 = shpfile[1]
|
28
|
+
assert(record1.geometry.kind_of?(Point))
|
29
|
+
assert_in_delta(-87.82580,record2.geometry.x,0.00001)
|
30
|
+
assert_in_delta(33.36417,record2.geometry.y,0.00001)
|
31
|
+
assert_equal(9,record2.data['Hoyoyo'])
|
32
|
+
|
33
|
+
shpfile.close
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_polyline
|
37
|
+
shpfile = ShpFile.open(File.dirname(__FILE__) + '/data/polyline.shp')
|
38
|
+
|
39
|
+
assert_equal(1,shpfile.record_count)
|
40
|
+
assert_equal(1,shpfile.fields.length)
|
41
|
+
assert_equal(ShpType::POLYLINE,shpfile.shp_type)
|
42
|
+
field = shpfile.fields[0]
|
43
|
+
assert_equal("Chipoto",field.name)
|
44
|
+
assert_equal("F",field.type)
|
45
|
+
|
46
|
+
record1 = shpfile[0]
|
47
|
+
#a SHP polyline can have multiple parts so they are in fact multilinestrings
|
48
|
+
assert(record1.geometry.kind_of?(MultiLineString))
|
49
|
+
assert_equal(1,record1.geometry.length)
|
50
|
+
assert_equal(6,record1.geometry[0].length)
|
51
|
+
assert_equal(5.678,record1.data['Chipoto'])
|
52
|
+
|
53
|
+
shpfile.close
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_polygon
|
57
|
+
shpfile = ShpFile.open(File.dirname(__FILE__) + '/data/polygon.shp')
|
58
|
+
|
59
|
+
assert_equal(1,shpfile.record_count)
|
60
|
+
assert_equal(1,shpfile.fields.length)
|
61
|
+
assert_equal(ShpType::POLYGON,shpfile.shp_type)
|
62
|
+
field = shpfile.fields[0]
|
63
|
+
assert_equal("Hello",field.name)
|
64
|
+
assert_equal("C",field.type)
|
65
|
+
|
66
|
+
record1 = shpfile[0]
|
67
|
+
#a SHP polygon can have multiple outer loops (although not supported currently) so they are in fact multipolygons
|
68
|
+
assert(record1.geometry.kind_of?(MultiPolygon))
|
69
|
+
assert_equal(1,record1.geometry.length)
|
70
|
+
assert_equal(1,record1.geometry[0].length)
|
71
|
+
assert_equal(7,record1.geometry[0][0].length)
|
72
|
+
assert_equal("Bouyoul!",record1.data['Hello'])
|
73
|
+
|
74
|
+
shpfile.close
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
data/tools/db.yml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2006 Guilhem Vellut <guilhem.vellut+georuby@gmail.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,116 @@
|
|
1
|
+
=Spatial Adapter for Rails
|
2
|
+
This is the Spatial Adapter for Rails 0.1.1. It is a plugin for Rails which manages the MySql Spatial and PostGIS geometric columns in a transparent way (that is like the other base data type columns). It also provides a way to manage these columns in migrations. It replaces both the "PostGIS Adapter for Rails" and "MySql Spatial Adapter for Rails" plugins.
|
3
|
+
|
4
|
+
===Dependencies
|
5
|
+
You need to install a version >= 0.1.1 of the GeoRuby gem (http://rubyforge.org/projects/georuby/) :
|
6
|
+
gem install georuby
|
7
|
+
|
8
|
+
|
9
|
+
===Installation
|
10
|
+
At the root of your Rails project, type :
|
11
|
+
script\plugin install svn://rubyforge.org/var/svn/georuby/SpatialAdapter/trunk/spatial_adapter
|
12
|
+
You need to have SVN installed.
|
13
|
+
|
14
|
+
|
15
|
+
===Operations
|
16
|
+
Geometric columns in your ActiveRecord models now appear just like any other column of other basic data types. They can also be dumped in ruby schema mode and loaded in migrations the same way as columns of basic types.
|
17
|
+
|
18
|
+
==== Migrations
|
19
|
+
|
20
|
+
Here is an example of code for the creation of a table with a geometric column in PostGIS, along with the addition of a spatial index on the column :
|
21
|
+
ActiveRecord::Schema.define do
|
22
|
+
create_table "table_points", :force => true do |t|
|
23
|
+
t.column "data", :string
|
24
|
+
t.column "geom", :point, :null=>false, :srid => 123, :with_z => true
|
25
|
+
end
|
26
|
+
|
27
|
+
add_index "table_points", "geom", :spatial=>true
|
28
|
+
end
|
29
|
+
|
30
|
+
Here is a related statement valid for MySql version <= 5.0.16 :
|
31
|
+
ActiveRecord::Schema.define do
|
32
|
+
create_table "table_points", :options=>"ENGINE=MyISAM", :force => true do |t|
|
33
|
+
t.column "data", :string
|
34
|
+
t.column "geom", :point, :null=>false
|
35
|
+
end
|
36
|
+
|
37
|
+
add_index "table_points", "geom", :spatial=>true
|
38
|
+
end
|
39
|
+
The differences with the PostGIS version are because of the following reasons :
|
40
|
+
- On all version of MySql, the :srid and :with_z would be ignored, since they are not supported.
|
41
|
+
- On MySql versions <= 5.0.16, you have to add <tt>:options => "ENGINE=MyISAM"</tt> to the create_table statetement, since only MyISAM tables can have geometric columns.
|
42
|
+
|
43
|
+
==== Model
|
44
|
+
|
45
|
+
Here is the model you would use, in both MySql and PostGIS:
|
46
|
+
class TablePoint < ActiveRecord::Base
|
47
|
+
end
|
48
|
+
That was easy! As you see, there is no need to declare a column as geometric. The plugin will get this information by itself.
|
49
|
+
|
50
|
+
==== Access
|
51
|
+
|
52
|
+
Here is an example of PostGIS row creation and access, using the model and the table defined above :
|
53
|
+
pt = TablePoint.new(:data => "Hello!",:geom => Point.from_x_y_z(-1.6,2.8,-3.4,123))
|
54
|
+
pt.save
|
55
|
+
pt = TablePoint.find_first
|
56
|
+
puts pt.geom.x #access the geom column like any other
|
57
|
+
|
58
|
+
For MySQL, it is slightly different since it does not support Z dimension or SRID :
|
59
|
+
pt = TablePoint.new(:data => "Hello!",:geom => Point.from_x_y(-1.6,2.8))
|
60
|
+
pt.save
|
61
|
+
pt = TablePoint.find_first
|
62
|
+
puts pt.geom.x #access the geom column like any other
|
63
|
+
|
64
|
+
==== Fixtures
|
65
|
+
|
66
|
+
If you use fixtures for your unit tests, at some point, you will want to input a geometry. You could transform your geometries to a form suitable for YAML yourself everytime but the spatial adapter provides a method to do it for you: +to_fixture_format+. It works for both MySQL and PostGIS (although the string returned is different for each database). You would use it like this, if the geometric column is a point:
|
67
|
+
fixture:
|
68
|
+
id: 1
|
69
|
+
data: HELLO
|
70
|
+
geom: <%= Point.from_x_y(123.5,321.9).to_fixture_format %>
|
71
|
+
|
72
|
+
==== Find_by
|
73
|
+
|
74
|
+
find_by_[column] has been redefined when column is of a geometric type. Instead of using the Rails default '=' operator, for which I can't see a definition for MySql spatial datatypes and which performs a bounding box equality test in PostGIS, it uses a bounding box intersection: && in PostGIS and MBRIntersects in MySQL, which can both make use of a spatial index if one is present to speed up the queries. You could use this query, for example, if you need to display data from the database: You would want only the geometries which are in the screen rectangle and you could use a bounding box query for that. Since this is a common case, it is the default. You have 2 ways to use the find_by_[geom_column]: Either by passing a geometric object directly, or passing an array with the 2 opposite corners of a bounding box (with 2 or 3 coordinates depending of the dimension of the data).
|
75
|
+
Park.find_by_geom(LineString.from_coordinates([[1.4,5.6],[2.7,8.9],[1.6,5.6]]))
|
76
|
+
or
|
77
|
+
Park.find_by_geom([[3,5.6],[19.98,5.9]])
|
78
|
+
In PostGIS, since you can only use operations with geometries with the same SRID, you can add a third element representing the SRID of the bounding box to the array. It is by default set to -1:
|
79
|
+
Park.find_by_geom([[3,5.6],[19.98,5.9],123])
|
80
|
+
|
81
|
+
==== Geometric data types
|
82
|
+
|
83
|
+
Ruby geometric datatypes are currently made available only through the GeoRuby library (http://thepochisuperstarmegashow.com/ProjectsDoc/georuby-doc/index.html): This is where the <tt>Point.from_x_y</tt> in the example above comes from. It is a goal of a future release of the Spatial Adapter to support additional geometric datatype libraries, such as Ruby/GEOS, as long as they can support reading and writing of EWKB.
|
84
|
+
|
85
|
+
|
86
|
+
===Warning
|
87
|
+
- If you use a version of MySQL before 5.0.16, only tables using the MyISAM engine can support geometric columns. After MySQL 5.0.16, any engine (incuding INNODB) is supported.
|
88
|
+
|
89
|
+
- Since ActiveRecord seems to keep only the string values directly returned from the database, it translates from these to the correct types everytime an attribute is read, which is probably ok for simple types, but might be less than efficient for geometries, since the EWKB string has to be parsed everytime. Also it means you cannot modify the geometry object returned from an attribute directly :
|
90
|
+
place = Place.find_first
|
91
|
+
place.the_geom.y=123456.7
|
92
|
+
|
93
|
+
- Since the translation to a geometry is performed everytime the_geom is read, the change to y will not be saved! You would have to do something like this :
|
94
|
+
place = Place.find_first
|
95
|
+
the_geom = place.the_geom
|
96
|
+
the_geom.y=123456.7
|
97
|
+
place.the_geom = the_geom
|
98
|
+
|
99
|
+
|
100
|
+
===Changes since last version
|
101
|
+
- The PostGIS adapter and the MySql Spatial adapter have been merged into one plugin. The correct files to load is determined using the type of connection defined in the environment.
|
102
|
+
- Geometric columns can now be dumped just like other base data types. This means you can use the ruby schema mode, even if you use the plugin.
|
103
|
+
- Support of M dimensions in migrations. The <tt>:dimension</tt> key in the column definition has disappeared and has been replaced by <tt>:with_z</tt> and <tt>:with_m</tt>.
|
104
|
+
- Addition of unit tests. At the plugin root, Run <tt>rake test:mysql</tt> to run the mysql tests and <tt>rake test:postgis</tt> for the postgis ones. You will need to configure your connection in <tt>test/db/database_mysql.yml</tt> and <tt>test/db/database_postgis.yml</tt>. If you get errors on your platform, please report to mailto:guilhem.vellut+georuby@gmail.com.
|
105
|
+
- Addition of a find_by methods with a special behaviour for geometries
|
106
|
+
- Addition of a to_fixture_format method to use inside a YAML fixture
|
107
|
+
|
108
|
+
===TODO
|
109
|
+
- Support of other geometric datatype libraries in addition to GeoRuby
|
110
|
+
- Tutorials
|
111
|
+
|
112
|
+
===License
|
113
|
+
The Spatial Adapter for Rails is released under the MIT license.
|
114
|
+
|
115
|
+
===Support
|
116
|
+
Any questions, enhancement proposals, bug notifications or corrections can be sent to mailto:guilhem.vellut+georuby@gmail.com.
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class SpatialAdapterNotCompatibleError < StandardError
|
2
|
+
end
|
3
|
+
|
4
|
+
if ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::MysqlAdapter)
|
5
|
+
require 'mysql_spatial_adapter'
|
6
|
+
elsif ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
|
7
|
+
require 'post_gis_adapter'
|
8
|
+
else
|
9
|
+
raise SpatialAdapterNotCompatibleError.new("Only MySQL and PostgreSQL are currently supported by the spatial adapter plugin.")
|
10
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
|
2
|
+
#Addition of a flag indicating if the index is spatial
|
3
|
+
ActiveRecord::ConnectionAdapters::IndexDefinition.class_eval do
|
4
|
+
attr_accessor :spatial
|
5
|
+
|
6
|
+
def initialize(table, name, unique, spatial,columns)
|
7
|
+
super(table,name,unique,columns)
|
8
|
+
@spatial = spatial
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveRecord::SchemaDumper.class_eval do
|
14
|
+
def table(table, stream)
|
15
|
+
|
16
|
+
columns = @connection.columns(table)
|
17
|
+
begin
|
18
|
+
tbl = StringIO.new
|
19
|
+
|
20
|
+
if @connection.respond_to?(:pk_and_sequence_for)
|
21
|
+
pk, pk_seq = @connection.pk_and_sequence_for(table)
|
22
|
+
end
|
23
|
+
pk ||= 'id'
|
24
|
+
|
25
|
+
tbl.print " create_table #{table.inspect}"
|
26
|
+
if columns.detect { |c| c.name == pk }
|
27
|
+
if pk != 'id'
|
28
|
+
tbl.print %Q(, :primary_key => "#{pk}")
|
29
|
+
end
|
30
|
+
else
|
31
|
+
tbl.print ", :id => false"
|
32
|
+
end
|
33
|
+
|
34
|
+
if @connection.respond_to?(:options_for)
|
35
|
+
res = @connection.options_for(table)
|
36
|
+
tbl.print ", :options=>'#{res}'" if res
|
37
|
+
end
|
38
|
+
|
39
|
+
tbl.print ", :force => true"
|
40
|
+
tbl.puts " do |t|"
|
41
|
+
|
42
|
+
columns.each do |column|
|
43
|
+
|
44
|
+
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" if @types[column.type].nil?
|
45
|
+
next if column.name == pk
|
46
|
+
#need to use less_simplified_type here or have each specific geometry type be simplified to a specific simplified type in Column and each one treated separately in the Column methods
|
47
|
+
if column.is_a?(SpatialColumn)
|
48
|
+
tbl.print " t.column #{column.name.inspect}, #{column.geometry_type.inspect}"
|
49
|
+
tbl.print ", :srid => #{column.srid.inspect}" if column.srid != -1
|
50
|
+
tbl.print ", :with_z => #{column.with_z.inspect}" if column.with_z
|
51
|
+
tbl.print ", :with_m => #{column.with_m.inspect}" if column.with_m
|
52
|
+
else
|
53
|
+
tbl.print " t.column #{column.name.inspect}, #{column.type.inspect}"
|
54
|
+
end
|
55
|
+
tbl.print ", :limit => #{column.limit.inspect}" if column.limit != @types[column.type][:limit]
|
56
|
+
tbl.print ", :default => #{column.default.inspect}" if !column.default.nil?
|
57
|
+
tbl.print ", :null => false" if !column.null
|
58
|
+
tbl.puts
|
59
|
+
end
|
60
|
+
|
61
|
+
tbl.puts " end"
|
62
|
+
tbl.puts
|
63
|
+
|
64
|
+
indexes(table, tbl)
|
65
|
+
|
66
|
+
tbl.rewind
|
67
|
+
stream.print tbl.read
|
68
|
+
rescue => e
|
69
|
+
stream.puts "# Could not dump table #{table.inspect} because of following #{e.class}"
|
70
|
+
stream.puts "# #{e.message} #{e.backtrace}"
|
71
|
+
stream.puts
|
72
|
+
end
|
73
|
+
|
74
|
+
stream
|
75
|
+
end
|
76
|
+
|
77
|
+
def indexes(table, stream)
|
78
|
+
indexes = @connection.indexes(table)
|
79
|
+
indexes.each do |index|
|
80
|
+
stream.print " add_index #{index.table.inspect}, #{index.columns.inspect}, :name => #{index.name.inspect}"
|
81
|
+
stream.print ", :unique => true" if index.unique
|
82
|
+
stream.print ", :spatial=> true " if index.spatial
|
83
|
+
stream.puts
|
84
|
+
end
|
85
|
+
|
86
|
+
stream.puts unless indexes.empty?
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
module SpatialAdapter
|
92
|
+
#Translation of geometric data types
|
93
|
+
def geometry_data_types
|
94
|
+
{
|
95
|
+
:point => { :name => "POINT" },
|
96
|
+
:line_string => { :name => "LINESTRING" },
|
97
|
+
:polygon => { :name => "POLYGON" },
|
98
|
+
:geometry_collection => { :name => "GEOMETRYCOLLECTION" },
|
99
|
+
:multi_point => { :name => "MULTIPOINT" },
|
100
|
+
:multi_line_string => { :name => "MULTILINESTRING" },
|
101
|
+
:multi_polygon => { :name => "MULTIPOLYGON" },
|
102
|
+
:geometry => { :name => "GEOMETRY"}
|
103
|
+
}
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
#using a mixin instead of subclassing Column since each adapter can have a specific subclass of Column
|
110
|
+
module SpatialColumn
|
111
|
+
attr_reader :geometry_type, :srid, :with_z, :with_m
|
112
|
+
|
113
|
+
def initialize(name, default, sql_type = nil, null = true,srid=-1,with_z=false,with_m=false)
|
114
|
+
super(name,default,sql_type,null)
|
115
|
+
@geometry_type = geometry_simplified_type(@sql_type)
|
116
|
+
@srid = srid
|
117
|
+
@with_z = with_z
|
118
|
+
@with_m = with_m
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
#Redefines type_cast to add support for geometries
|
123
|
+
def type_cast(value)
|
124
|
+
return nil if value.nil?
|
125
|
+
case type
|
126
|
+
when :geometry then self.class.string_to_geometry(value)
|
127
|
+
else super
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
#Redefines type_cast_code to add support for geometries.
|
132
|
+
#
|
133
|
+
#WARNING : Since ActiveRecord keeps only the string values directly returned from the database, it translates from these to the correct types everytime an attribute is read (using the code returned by this method), which is probably ok for simple types, but might be less than efficient for geometries. Also you cannot modify the geometry object returned directly or your change will not be saved.
|
134
|
+
def type_cast_code(var_name)
|
135
|
+
case type
|
136
|
+
when :geometry then "#{self.class.name}.string_to_geometry(#{var_name})"
|
137
|
+
else super
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
|
142
|
+
#Redefines klass to add support for geometries
|
143
|
+
def klass
|
144
|
+
case type
|
145
|
+
when :geometry then GeoRuby::SimpleFeatures::Geometry
|
146
|
+
else super
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
private
|
151
|
+
|
152
|
+
#Redefines the simplified_type method to add behabiour for when a column is of type geometry
|
153
|
+
def simplified_type(field_type)
|
154
|
+
case field_type
|
155
|
+
when /geometry|point|linestring|polygon|multipoint|multilinestring|multipolygon|geometrycollection/i then :geometry
|
156
|
+
else super
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
#less simlpified geometric type to be use in migrations
|
161
|
+
def geometry_simplified_type(field_type)
|
162
|
+
case field_type
|
163
|
+
when /^point$/i then :point
|
164
|
+
when /^linestring$/i then :line_string
|
165
|
+
when /^polygon$/i then :polygon
|
166
|
+
when /^geometry$/i then :geometry
|
167
|
+
when /multipoint/i then :multi_point
|
168
|
+
when /multilinestring/i then :multi_line_string
|
169
|
+
when /multipolygon/i then :multi_polygon
|
170
|
+
when /geometrycollection/i then :geometry_collection
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
|
175
|
+
end
|