GeoRuby 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
|
6
|
+
namespace :test do
|
7
|
+
Rake::TestTask::new(:mysql => "db:mysql" ) do |t|
|
8
|
+
t.test_files = FileList['test/*_mysql_test.rb']
|
9
|
+
t.verbose = true
|
10
|
+
end
|
11
|
+
|
12
|
+
Rake::TestTask::new(:postgis => "db:postgis" ) do |t|
|
13
|
+
t.test_files = FileList['test/*_postgis_test.rb']
|
14
|
+
t.verbose = true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
namespace :db do
|
19
|
+
task :mysql do
|
20
|
+
load('test/schema/schema_mysql.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
task :postgis do
|
24
|
+
load('test/schema/schema_postgis.rb')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Generate the documentation"
|
29
|
+
Rake::RDocTask::new do |rdoc|
|
30
|
+
rdoc.rdoc_dir = 'spatialadapter-doc/'
|
31
|
+
rdoc.title = "MySql Spatial Adapater for Rails Documentation"
|
32
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
33
|
+
rdoc.rdoc_files.include('README')
|
34
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
35
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'common/common_mysql'
|
5
|
+
require 'models/models_mysql'
|
6
|
+
|
7
|
+
|
8
|
+
class AccessMysqlTest < Test::Unit::TestCase
|
9
|
+
|
10
|
+
def test_point
|
11
|
+
pt = TablePoint.new(:data => "Test", :geom => Point.from_x_y(1.2,4.5))
|
12
|
+
assert(pt.save)
|
13
|
+
|
14
|
+
pt = TablePoint.find_first
|
15
|
+
assert(pt)
|
16
|
+
assert_equal("Test",pt.data)
|
17
|
+
assert_equal(Point.from_x_y(1.2,4.5),pt.geom)
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_line_string
|
22
|
+
ls = TableLineString.new(:value => 3, :geom => LineString.from_coordinates([[1.4,2.5],[1.5,6.7]]))
|
23
|
+
assert(ls.save)
|
24
|
+
|
25
|
+
ls = TableLineString.find_first
|
26
|
+
assert(ls)
|
27
|
+
assert_equal(3,ls.value)
|
28
|
+
assert_equal(LineString.from_coordinates([[1.4,2.5],[1.5,6.7]]),ls.geom)
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_polygon
|
33
|
+
pg = TablePolygon.new(:geom => Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]]))
|
34
|
+
assert(pg.save)
|
35
|
+
|
36
|
+
pg = TablePolygon.find_first
|
37
|
+
assert(pg)
|
38
|
+
assert_equal(Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]]),pg.geom)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_muti_point
|
42
|
+
mp = TableMultiPoint.new(:geom => MultiPoint.from_coordinates([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]]))
|
43
|
+
assert(mp.save)
|
44
|
+
|
45
|
+
mp = TableMultiPoint.find_first
|
46
|
+
assert(mp)
|
47
|
+
assert_equal(MultiPoint.from_coordinates([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]]),mp.geom)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_multi_line_string
|
51
|
+
ml = TableMultiLineString.new(:geom => MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012]]),LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012],[45.123,123.3]])]))
|
52
|
+
assert(ml.save)
|
53
|
+
|
54
|
+
ml = TableMultiLineString.find_first
|
55
|
+
assert(ml)
|
56
|
+
assert_equal(MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012]]),LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012],[45.123,123.3]])]),ml.geom)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_multi_polygon
|
60
|
+
mp = TableMultiPolygon.new( :geom => MultiPolygon.from_polygons([Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]]),Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]])]))
|
61
|
+
assert(mp.save)
|
62
|
+
|
63
|
+
mp = TableMultiPolygon.find_first
|
64
|
+
assert(mp)
|
65
|
+
assert_equal(MultiPolygon.from_polygons([Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]]),Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]])]),mp.geom)
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_geometry
|
69
|
+
gm = TableGeometry.new(:geom => LineString.from_coordinates([[12.4,-45.3],[45.4,41.6],[4.456,1.0698]]))
|
70
|
+
assert(gm.save)
|
71
|
+
|
72
|
+
gm = TableGeometry.find_first
|
73
|
+
assert(gm)
|
74
|
+
assert_equal(LineString.from_coordinates([[12.4,-45.3],[45.4,41.6],[4.456,1.0698]]),gm.geom)
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_geometry_collection
|
78
|
+
gc = TableGeometryCollection.new(:geom => GeometryCollection.from_geometries([Point.from_x_y(4.67,45.4),LineString.from_coordinates([[5.7,12.45],[67.55,54]])]))
|
79
|
+
assert(gc.save)
|
80
|
+
|
81
|
+
gc = TableGeometryCollection.find_first
|
82
|
+
assert(gc)
|
83
|
+
assert_equal(GeometryCollection.from_geometries([Point.from_x_y(4.67,45.4),LineString.from_coordinates([[5.7,12.45],[67.55,54]])]),gc.geom)
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'common/common_postgis'
|
5
|
+
require 'models/models_postgis'
|
6
|
+
|
7
|
+
|
8
|
+
class AccessPostgisTest < Test::Unit::TestCase
|
9
|
+
|
10
|
+
def test_point
|
11
|
+
pt = TablePoint.new(:data => "Test", :geom => Point.from_x_y(1.2,4.5))
|
12
|
+
assert(pt.save)
|
13
|
+
|
14
|
+
pt = TablePoint.find_first
|
15
|
+
assert(pt)
|
16
|
+
assert_equal("Test",pt.data)
|
17
|
+
assert_equal(Point.from_x_y(1.2,4.5),pt.geom)
|
18
|
+
|
19
|
+
pts = TablePoint.find_all
|
20
|
+
pts.each do |pt|
|
21
|
+
assert(pt.geom.is_a?(Point))
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_keyword_column_point
|
27
|
+
pt = TableKeywordColumnPoint.new(:location => Point.from_x_y(1.2,4.5))
|
28
|
+
assert(pt.save)
|
29
|
+
|
30
|
+
pt = TableKeywordColumnPoint.find_first
|
31
|
+
assert(pt)
|
32
|
+
assert_equal(Point.from_x_y(1.2,4.5),pt.location)
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_line_string
|
37
|
+
ls = TableLineString.new(:value => 3, :geom => LineString.from_coordinates([[1.4,2.5],[1.5,6.7]]))
|
38
|
+
assert(ls.save)
|
39
|
+
|
40
|
+
ls = TableLineString.find_first
|
41
|
+
assert(ls)
|
42
|
+
assert_equal(3,ls.value)
|
43
|
+
assert_equal(LineString.from_coordinates([[1.4,2.5],[1.5,6.7]]),ls.geom)
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_polygon
|
48
|
+
pg = TablePolygon.new(:geom => Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]]))
|
49
|
+
assert(pg.save)
|
50
|
+
|
51
|
+
pg = TablePolygon.find_first
|
52
|
+
assert(pg)
|
53
|
+
assert_equal(Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]]),pg.geom)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_muti_point
|
57
|
+
mp = TableMultiPoint.new(:geom => MultiPoint.from_coordinates([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]]))
|
58
|
+
assert(mp.save)
|
59
|
+
|
60
|
+
mp = TableMultiPoint.find_first
|
61
|
+
assert(mp)
|
62
|
+
assert_equal(MultiPoint.from_coordinates([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]]),mp.geom)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_multi_line_string
|
66
|
+
ml = TableMultiLineString.new(:geom => MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012]]),LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012],[45.123,123.3]])]))
|
67
|
+
assert(ml.save)
|
68
|
+
|
69
|
+
ml = TableMultiLineString.find_first
|
70
|
+
assert(ml)
|
71
|
+
assert_equal(MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012]]),LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012],[45.123,123.3]])]),ml.geom)
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_multi_polygon
|
75
|
+
mp = TableMultiPolygon.new( :geom => MultiPolygon.from_polygons([Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]]),Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]])]))
|
76
|
+
assert(mp.save)
|
77
|
+
|
78
|
+
mp = TableMultiPolygon.find_first
|
79
|
+
assert(mp)
|
80
|
+
assert_equal(MultiPolygon.from_polygons([Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]]),Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]])]),mp.geom)
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_geometry
|
84
|
+
gm = TableGeometry.new(:geom => LineString.from_coordinates([[12.4,-45.3],[45.4,41.6],[4.456,1.0698]]))
|
85
|
+
assert(gm.save)
|
86
|
+
|
87
|
+
gm = TableGeometry.find_first
|
88
|
+
assert(gm)
|
89
|
+
assert_equal(LineString.from_coordinates([[12.4,-45.3],[45.4,41.6],[4.456,1.0698]]),gm.geom)
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_geometry_collection
|
93
|
+
gc = TableGeometryCollection.new(:geom => GeometryCollection.from_geometries([Point.from_x_y(4.67,45.4),LineString.from_coordinates([[5.7,12.45],[67.55,54]])]))
|
94
|
+
assert(gc.save)
|
95
|
+
|
96
|
+
gc = TableGeometryCollection.find_first
|
97
|
+
assert(gc)
|
98
|
+
assert_equal(GeometryCollection.from_geometries([Point.from_x_y(4.67,45.4),LineString.from_coordinates([[5.7,12.45],[67.55,54]])]),gc.geom)
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_3dz_points
|
102
|
+
pt = Table3dzPoint.new(:data => "Hello!",:geom => Point.from_x_y_z(-1.6,2.8,-3.4))
|
103
|
+
assert(pt.save)
|
104
|
+
|
105
|
+
pt = Table3dzPoint.find_first
|
106
|
+
assert(pt)
|
107
|
+
assert_equal(Point.from_x_y_z(-1.6,2.8,-3.4),pt.geom)
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_3dm_points
|
111
|
+
pt = Table3dmPoint.new(:geom => Point.from_x_y_m(-1.6,2.8,-3.4))
|
112
|
+
assert(pt.save)
|
113
|
+
|
114
|
+
pt = Table3dmPoint.find_first
|
115
|
+
assert(pt)
|
116
|
+
assert_equal(Point.from_x_y_m(-1.6,2.8,-3.4),pt.geom)
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_4d_point
|
120
|
+
pt = Table4dPoint.new(:geom => Point.from_x_y_z_m(-1.6,2.8,-3.4,15))
|
121
|
+
assert(pt.save)
|
122
|
+
|
123
|
+
pt = Table4dPoint.find_first
|
124
|
+
assert(pt)
|
125
|
+
assert_equal(Point.from_x_y_z_m(-1.6,2.8,-3.4,15),pt.geom)
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_srid_line_string
|
129
|
+
ls = TableSridLineString.new(:geom => LineString.from_coordinates([[1.4,2.5],[1.5,6.7]],123))
|
130
|
+
assert(ls.save)
|
131
|
+
|
132
|
+
ls = TableSridLineString.find_first
|
133
|
+
assert(ls)
|
134
|
+
ls_e = LineString.from_coordinates([[1.4,2.5],[1.5,6.7]],123)
|
135
|
+
assert_equal(ls_e,ls.geom)
|
136
|
+
assert_equal(ls_e.srid,ls.geom.srid)
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_srid_4d_polygon
|
140
|
+
pg = TableSrid4dPolygon.new(:geom => Polygon.from_coordinates([[[0,0,2,-45.1],[4,0,2,5],[4,4,2,4.67],[0,4,2,1.34],[0,0,2,-45.1]],[[1,1,2,12.3],[3,1,2,123],[3,3,2,12.2],[1,3,2,12],[1,1,2,12.3]]],123,true,true))
|
141
|
+
assert(pg.save)
|
142
|
+
|
143
|
+
pg = TableSrid4dPolygon.find_first
|
144
|
+
assert(pg)
|
145
|
+
pg_e = Polygon.from_coordinates([[[0,0,2,-45.1],[4,0,2,5],[4,4,2,4.67],[0,4,2,1.34],[0,0,2,-45.1]],[[1,1,2,12.3],[3,1,2,123],[3,3,2,12.2],[1,3,2,12],[1,1,2,12.3]]],123,true,true)
|
146
|
+
assert_equal(pg_e,pg.geom)
|
147
|
+
assert_equal(pg_e.srid,pg.geom.srid)
|
148
|
+
end
|
149
|
+
|
150
|
+
|
151
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../../lib')
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require_gem 'activerecord'
|
5
|
+
|
6
|
+
ActiveRecord::Base.establish_connection(YAML.load_file(File.dirname(__FILE__) + '/../db/database_mysql.yml'))
|
7
|
+
|
8
|
+
require File.dirname(__FILE__) + '/../../init.rb'
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../../lib')
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require_gem 'activerecord'
|
5
|
+
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection(YAML.load_file(File.dirname(__FILE__) + '/../db/database_postgis.yml'))
|
8
|
+
|
9
|
+
require File.dirname(__FILE__) + '/../../init.rb'
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
@@ -0,0 +1,64 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'common/common_mysql'
|
5
|
+
|
6
|
+
class Park < ActiveRecord::Base
|
7
|
+
end
|
8
|
+
|
9
|
+
class FindMysqlTest < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def setup
|
12
|
+
ActiveRecord::Schema.define() do
|
13
|
+
create_table "parks", :options => "ENGINE=MyISAM" , :force => true do |t|
|
14
|
+
t.column "data" , :string, :limit => 100
|
15
|
+
t.column "geom", :point,:null=>false
|
16
|
+
end
|
17
|
+
add_index "parks","geom",:spatial=>true,:name => "example_spatial_index"
|
18
|
+
end
|
19
|
+
|
20
|
+
pt = Park.new(:data => "Point1", :geom => Point.from_x_y(1.2,0.75))
|
21
|
+
assert(pt.save)
|
22
|
+
|
23
|
+
pt = Park.new(:data => "Point2",:geom => Point.from_x_y(0.6,1.3))
|
24
|
+
assert(pt.save)
|
25
|
+
|
26
|
+
pt = Park.new(:data => "Point3", :geom => Point.from_x_y(2.5,2))
|
27
|
+
assert(pt.save)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_find_by_geom_column
|
31
|
+
#the linestring bbox is [0 0,2 2]
|
32
|
+
pts = Park.find_all_by_geom(LineString.from_coordinates([[0,0],[2,2]]))
|
33
|
+
assert(pts)
|
34
|
+
assert(pts.is_a?(Array))
|
35
|
+
assert_equal(2,pts.length)
|
36
|
+
assert(pts[0].data == "Point1" ||pts[1].data == "Point1" )
|
37
|
+
assert(pts[0].data == "Point2" ||pts[1].data == "Point2" )
|
38
|
+
|
39
|
+
#the linestring bbox is [2.49 1.99,2.51 2.01]
|
40
|
+
pts = Park.find_all_by_geom(LineString.from_coordinates([[2.49,1.99],[2.51,2.01]]))
|
41
|
+
assert(pts)
|
42
|
+
assert(pts.is_a?(Array))
|
43
|
+
assert_equal(1,pts.length)
|
44
|
+
assert(pts[0].data == "Point3")
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_find_by_geom_column_bbox_condition
|
49
|
+
pts = Park.find_all_by_geom([[0,0],[2,2]])
|
50
|
+
assert(pts)
|
51
|
+
assert(pts.is_a?(Array))
|
52
|
+
assert_equal(2,pts.length)
|
53
|
+
assert(pts[0].data == "Point1" ||pts[1].data == "Point1" )
|
54
|
+
assert(pts[0].data == "Point2" ||pts[1].data == "Point2" )
|
55
|
+
|
56
|
+
pts = Park.find_all_by_geom([[2.49,1.99],[2.51,2.01]])
|
57
|
+
assert(pts)
|
58
|
+
assert(pts.is_a?(Array))
|
59
|
+
assert_equal(1,pts.length)
|
60
|
+
assert(pts[0].data == "Point3")
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'common/common_postgis'
|
5
|
+
|
6
|
+
class Park < ActiveRecord::Base
|
7
|
+
end
|
8
|
+
|
9
|
+
class FindPostgisTest < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def setup
|
12
|
+
ActiveRecord::Schema.define() do
|
13
|
+
create_table "parks", :force => true do |t|
|
14
|
+
t.column "data" , :string, :limit => 100
|
15
|
+
t.column "geom", :point,:null=>false,:srid=>123
|
16
|
+
end
|
17
|
+
add_index "parks","geom",:spatial=>true,:name => "example_spatial_index"
|
18
|
+
end
|
19
|
+
|
20
|
+
pt = Park.new(:data => "Point1", :geom => Point.from_x_y(1.2,0.75,123))
|
21
|
+
assert(pt.save)
|
22
|
+
|
23
|
+
pt = Park.new(:data => "Point2",:geom => Point.from_x_y(0.6,1.3,123))
|
24
|
+
assert(pt.save)
|
25
|
+
|
26
|
+
pt = Park.new(:data => "Point3", :geom => Point.from_x_y(2.5,2,123))
|
27
|
+
assert(pt.save)
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_find_by_geom_column
|
32
|
+
|
33
|
+
pts = Park.find_all_by_geom(LineString.from_coordinates([[0,0],[2,2]],123))
|
34
|
+
assert(pts)
|
35
|
+
assert(pts.is_a?(Array))
|
36
|
+
assert_equal(2,pts.length)
|
37
|
+
assert(pts[0].data == "Point1" ||pts[1].data == "Point1" )
|
38
|
+
assert(pts[0].data == "Point2" ||pts[1].data == "Point2" )
|
39
|
+
|
40
|
+
pts = Park.find_all_by_geom(LineString.from_coordinates([[2.49,1.99],[2.51,2.01]],123))
|
41
|
+
assert(pts)
|
42
|
+
assert(pts.is_a?(Array))
|
43
|
+
assert_equal(1,pts.length)
|
44
|
+
assert(pts[0].data == "Point3")
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_find_by_geom_column_bbox_condition
|
49
|
+
pts = Park.find_all_by_geom([[0,0],[2,2],123])
|
50
|
+
assert(pts)
|
51
|
+
assert(pts.is_a?(Array))
|
52
|
+
assert_equal(2,pts.length)
|
53
|
+
assert(pts[0].data == "Point1" ||pts[1].data == "Point1" )
|
54
|
+
assert(pts[0].data == "Point2" ||pts[1].data == "Point2" )
|
55
|
+
|
56
|
+
pts = Park.find_all_by_geom([[0,0],[2,2],123])
|
57
|
+
assert(pts)
|
58
|
+
assert(pts.is_a?(Array))
|
59
|
+
assert_equal(2,pts.length)
|
60
|
+
assert(pts[0].data == "Point1" ||pts[1].data == "Point1" )
|
61
|
+
assert(pts[0].data == "Point2" ||pts[1].data == "Point2" )
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
end
|