ppe-postgis-adapter 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/History.txt +6 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +347 -0
- data/Rakefile +70 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/postgis_adapter/acts_as_geom.rb +39 -0
- data/lib/postgis_adapter/common_spatial_adapter.rb +184 -0
- data/lib/postgis_adapter.rb +423 -0
- data/lib/postgis_functions/bbox.rb +128 -0
- data/lib/postgis_functions/class.rb +63 -0
- data/lib/postgis_functions/common.rb +886 -0
- data/lib/postgis_functions.rb +169 -0
- data/postgis_adapter.gemspec +75 -0
- data/rails/init.rb +9 -0
- data/spec/db/models_postgis.rb +61 -0
- data/spec/db/schema_postgis.rb +92 -0
- data/spec/postgis_adapter/acts_as_geom_spec.rb +30 -0
- data/spec/postgis_adapter/common_spatial_adapter_spec.rb +254 -0
- data/spec/postgis_adapter_spec.rb +224 -0
- data/spec/postgis_functions/bbox_spec.rb +45 -0
- data/spec/postgis_functions/class_spec.rb +65 -0
- data/spec/postgis_functions/common_spec.rb +374 -0
- data/spec/postgis_functions_spec.rb +53 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +26 -0
- metadata +90 -0
@@ -0,0 +1,169 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# PostGIS Adapter - http://github.com/nofxx/postgis_adapter
|
4
|
+
#
|
5
|
+
# Hope you enjoy this plugin.
|
6
|
+
#
|
7
|
+
#
|
8
|
+
# Post any bugs/suggestions to GitHub issues tracker:
|
9
|
+
# http://github.com/nofxx/postgis_adapter/issues
|
10
|
+
#
|
11
|
+
#
|
12
|
+
# Some links:
|
13
|
+
#
|
14
|
+
# PostGis Manual - http://postgis.refractions.net/documentation/manual-svn/ch07.html
|
15
|
+
# Earth Spheroid - http://en.wikipedia.org/wiki/Figure_of_the_Earth
|
16
|
+
#
|
17
|
+
|
18
|
+
module PostgisFunctions
|
19
|
+
# WGS84 Spheroid
|
20
|
+
EARTH_SPHEROID = "'SPHEROID[\"GRS-80\",6378137,298.257222101]'" # SRID => 4326
|
21
|
+
|
22
|
+
def postgis_calculate(operation, subjects, options = {})
|
23
|
+
subjects = [subjects] unless subjects.respond_to?(:map)
|
24
|
+
execute_geometrical_calculation(operation, subjects, options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def geo_columns
|
28
|
+
@geo_columns ||= postgis_geoms[:columns]
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
#
|
34
|
+
# Construct the PostGIS SQL query
|
35
|
+
#
|
36
|
+
# Returns:
|
37
|
+
# Area/Distance/DWithin/Length/Perimeter => projected units
|
38
|
+
# DistanceSphere/Spheroid => meters
|
39
|
+
#
|
40
|
+
def construct_geometric_sql(type,geoms,options)
|
41
|
+
not_db, on_db = geoms.partition { |g| g.is_a? Geometry }
|
42
|
+
|
43
|
+
tables = on_db.map do |t| {
|
44
|
+
:name => t.class.table_name,
|
45
|
+
:column => t.postgis_geoms.keys[0],
|
46
|
+
:uid => unique_identifier,
|
47
|
+
:id => t[:id] }
|
48
|
+
end
|
49
|
+
|
50
|
+
# Implement a better way for options?
|
51
|
+
if options.instance_of? Hash
|
52
|
+
transform = options.delete(:transform)
|
53
|
+
options = nil
|
54
|
+
end
|
55
|
+
|
56
|
+
fields = tables.map { |f| "#{f[:uid]}.#{f[:column]}" } # W1.geom
|
57
|
+
fields << not_db.map { |g| "'#{g.as_hex_ewkb}'::geometry"} unless not_db.empty?
|
58
|
+
fields.map! { |f| "ST_Transform(#{f}, #{transform})" } if transform # ST_Transform(W1.geom,x)
|
59
|
+
conditions = tables.map { |f| "#{f[:uid]}.id = #{f[:id]}" } # W1.id = 5
|
60
|
+
tables.map! { |f| "#{f[:name]} #{f[:uid]}" } # streets W1
|
61
|
+
|
62
|
+
#
|
63
|
+
# Data => SELECT Func(A,B)
|
64
|
+
# BBox => SELECT (A <=> B)
|
65
|
+
# Func => SELECT Func(Func(A))
|
66
|
+
#
|
67
|
+
if type != :bbox
|
68
|
+
opcode = type.to_s
|
69
|
+
opcode = "ST_#{opcode}" unless opcode =~ /th3d|pesinter/
|
70
|
+
fields << options if options
|
71
|
+
fields = fields.join(",")
|
72
|
+
else
|
73
|
+
fields = fields.join(" #{options} ")
|
74
|
+
end
|
75
|
+
|
76
|
+
sql = "SELECT #{opcode}(#{fields}) "
|
77
|
+
sql << "FROM #{tables.join(",")} " unless tables.empty?
|
78
|
+
sql << "WHERE #{conditions.join(" AND ")}" unless conditions.empty?
|
79
|
+
sql
|
80
|
+
end
|
81
|
+
|
82
|
+
#
|
83
|
+
# Execute the query and parse the return.
|
84
|
+
# We may receive:
|
85
|
+
#
|
86
|
+
# "t" or "f" for boolean queries
|
87
|
+
# BIGHASH for geometries
|
88
|
+
# HASH for ST_Relate
|
89
|
+
# Rescue a float
|
90
|
+
#
|
91
|
+
def execute_geometrical_calculation(operation, subject, options) #:nodoc:
|
92
|
+
value = connection.select_value(construct_geometric_sql(operation, subject, options))
|
93
|
+
return nil unless value
|
94
|
+
# TODO: bench case vs if here
|
95
|
+
if value =~ /^[tf]$/
|
96
|
+
{"f" => false, "t" => true}[value]
|
97
|
+
elsif value =~ /^\{/
|
98
|
+
value
|
99
|
+
else
|
100
|
+
GeoRuby::SimpleFeatures::Geometry.from_hex_ewkb(value) rescue value
|
101
|
+
end
|
102
|
+
rescue Exception => e
|
103
|
+
raise StandardError, "#{e}"
|
104
|
+
end
|
105
|
+
|
106
|
+
# Get a unique ID for tables
|
107
|
+
def unique_identifier
|
108
|
+
@u_id ||= "T1"
|
109
|
+
@u_id = @u_id.succ
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
#
|
115
|
+
# POINT(0 0)
|
116
|
+
# LINESTRING(0 0,1 1,1 2)
|
117
|
+
# POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))
|
118
|
+
# MULTIPOINT(0 0,1 2)
|
119
|
+
# MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))
|
120
|
+
# MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ..)
|
121
|
+
# GEOMETRYCOLLECTION(POINT(2 3),LINESTRING((2 3,3 4)))
|
122
|
+
#
|
123
|
+
#Accessors
|
124
|
+
#
|
125
|
+
#ST_Dump
|
126
|
+
#ST_ExteriorRing
|
127
|
+
#ST_GeometryN
|
128
|
+
#ST_GeometryType
|
129
|
+
#ST_InteriorRingN
|
130
|
+
#ST_IsEmpty
|
131
|
+
#ST_IsRing
|
132
|
+
#ST_IsSimple
|
133
|
+
#ST_IsValid
|
134
|
+
#ST_mem_size
|
135
|
+
#ST_M
|
136
|
+
#ST_NumGeometries
|
137
|
+
#ST_NumInteriorRings
|
138
|
+
#ST_PointN
|
139
|
+
#ST_SetSRID
|
140
|
+
#ST_Summary1
|
141
|
+
#ST_X
|
142
|
+
#ST_XMin,ST_XMax
|
143
|
+
#ST_Y
|
144
|
+
#YMin,YMax
|
145
|
+
#ST_Z
|
146
|
+
#ZMin,ZMax
|
147
|
+
|
148
|
+
#OUTPUT
|
149
|
+
|
150
|
+
#ST_AsBinary
|
151
|
+
#ST_AsText
|
152
|
+
#ST_AsEWKB
|
153
|
+
#ST_AsEWKT
|
154
|
+
#ST_AsHEXEWKB
|
155
|
+
#ST_AsGML
|
156
|
+
#ST_AsKML
|
157
|
+
#ST_AsSVG
|
158
|
+
# #EARTH_SPHEROID = "'SPHEROID[\"IERS_2003\",6378136.6,298.25642]'" # SRID =>
|
159
|
+
# def distance_convert(value, unit, from = nil)
|
160
|
+
# factor = case unit
|
161
|
+
# when :km, :kilo then 1
|
162
|
+
# when :miles,:mile then 0.62137119
|
163
|
+
# when :cm, :cent then 0.1
|
164
|
+
# when :nmi, :nmile then 0.5399568
|
165
|
+
# end
|
166
|
+
# factor *= 1e3 if from
|
167
|
+
# value * factor
|
168
|
+
# end #use all commands in lowcase form
|
169
|
+
#opcode = opcode.camelize unless opcode =~ /spher|max|npoints/
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{ppe-postgis-adapter}
|
8
|
+
s.version = "0.7.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Marcos Piccinini"]
|
12
|
+
s.date = %q{2009-10-18}
|
13
|
+
s.description = %q{Execute PostGIS functions on Active Record}
|
14
|
+
s.email = %q{x@nofxx.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"History.txt",
|
21
|
+
"MIT-LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"init.rb",
|
26
|
+
"lib/postgis_adapter.rb",
|
27
|
+
"lib/postgis_adapter/acts_as_geom.rb",
|
28
|
+
"lib/postgis_adapter/common_spatial_adapter.rb",
|
29
|
+
"lib/postgis_functions.rb",
|
30
|
+
"lib/postgis_functions/bbox.rb",
|
31
|
+
"lib/postgis_functions/class.rb",
|
32
|
+
"lib/postgis_functions/common.rb",
|
33
|
+
"postgis_adapter.gemspec",
|
34
|
+
"rails/init.rb",
|
35
|
+
"spec/db/models_postgis.rb",
|
36
|
+
"spec/db/schema_postgis.rb",
|
37
|
+
"spec/postgis_adapter/acts_as_geom_spec.rb",
|
38
|
+
"spec/postgis_adapter/common_spatial_adapter_spec.rb",
|
39
|
+
"spec/postgis_adapter_spec.rb",
|
40
|
+
"spec/postgis_functions/bbox_spec.rb",
|
41
|
+
"spec/postgis_functions/class_spec.rb",
|
42
|
+
"spec/postgis_functions/common_spec.rb",
|
43
|
+
"spec/postgis_functions_spec.rb",
|
44
|
+
"spec/spec.opts",
|
45
|
+
"spec/spec_helper.rb"
|
46
|
+
]
|
47
|
+
s.homepage = %q{http://github.com/nofxx/postgis_adapter}
|
48
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
49
|
+
s.require_paths = ["lib"]
|
50
|
+
s.rubyforge_project = %q{postgis_adapter}
|
51
|
+
s.rubygems_version = %q{1.3.5}
|
52
|
+
s.summary = %q{PostGIS Adapter for Active Record}
|
53
|
+
s.test_files = [
|
54
|
+
"spec/db/models_postgis.rb",
|
55
|
+
"spec/db/schema_postgis.rb",
|
56
|
+
"spec/postgis_adapter/acts_as_geom_spec.rb",
|
57
|
+
"spec/postgis_adapter/common_spatial_adapter_spec.rb",
|
58
|
+
"spec/postgis_functions_spec.rb",
|
59
|
+
"spec/spec_helper.rb",
|
60
|
+
"spec/postgis_adapter_spec.rb",
|
61
|
+
"spec/postgis_functions/class_spec.rb",
|
62
|
+
"spec/postgis_functions/common_spec.rb",
|
63
|
+
"spec/postgis_functions/bbox_spec.rb"
|
64
|
+
]
|
65
|
+
|
66
|
+
if s.respond_to? :specification_version then
|
67
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
68
|
+
s.specification_version = 3
|
69
|
+
|
70
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
71
|
+
else
|
72
|
+
end
|
73
|
+
else
|
74
|
+
end
|
75
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
class SpatialAdapterNotCompatibleError < StandardError
|
2
|
+
end
|
3
|
+
|
4
|
+
unless ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
|
5
|
+
raise SpatialAdapterNotCompatibleError.
|
6
|
+
new("Database config file not set or it does not map to PostgreSQL\n" +
|
7
|
+
"Only PostgreSQL with PostGIS is supported by postgis_adapter.")
|
8
|
+
end
|
9
|
+
require 'postgis_adapter'
|
@@ -0,0 +1,61 @@
|
|
1
|
+
class TablePoint < ActiveRecord::Base
|
2
|
+
end
|
3
|
+
|
4
|
+
class TableKeywordColumnPoint < ActiveRecord::Base
|
5
|
+
end
|
6
|
+
|
7
|
+
class TableLineString < ActiveRecord::Base
|
8
|
+
end
|
9
|
+
|
10
|
+
class TablePolygon < ActiveRecord::Base
|
11
|
+
end
|
12
|
+
|
13
|
+
class TableMultiPoint < ActiveRecord::Base
|
14
|
+
end
|
15
|
+
|
16
|
+
class TableMultiLineString < ActiveRecord::Base
|
17
|
+
end
|
18
|
+
|
19
|
+
class TableMultiPolygon < ActiveRecord::Base
|
20
|
+
end
|
21
|
+
|
22
|
+
class TableGeometry < ActiveRecord::Base
|
23
|
+
end
|
24
|
+
|
25
|
+
class TableGeometryCollection < ActiveRecord::Base
|
26
|
+
end
|
27
|
+
|
28
|
+
class Table3dzPoint < ActiveRecord::Base
|
29
|
+
end
|
30
|
+
|
31
|
+
class Table3dmPoint < ActiveRecord::Base
|
32
|
+
end
|
33
|
+
|
34
|
+
class Table4dPoint < ActiveRecord::Base
|
35
|
+
end
|
36
|
+
|
37
|
+
class TableSridLineString < ActiveRecord::Base
|
38
|
+
end
|
39
|
+
|
40
|
+
class TableSrid4dPolygon < ActiveRecord::Base
|
41
|
+
end
|
42
|
+
|
43
|
+
class City < ActiveRecord::Base
|
44
|
+
acts_as_geom :geom => :polygon
|
45
|
+
end
|
46
|
+
|
47
|
+
class Position < ActiveRecord::Base
|
48
|
+
acts_as_geom :geom => :point
|
49
|
+
end
|
50
|
+
|
51
|
+
class Street < ActiveRecord::Base
|
52
|
+
acts_as_geom :geom => :line_string
|
53
|
+
end
|
54
|
+
|
55
|
+
class CommonGeo < ActiveRecord::Base
|
56
|
+
acts_as_geom :geom => :point
|
57
|
+
end
|
58
|
+
|
59
|
+
class DiffName < ActiveRecord::Base
|
60
|
+
acts_as_geom :the_geom => :point
|
61
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
#add some postgis specific tables
|
2
|
+
ActiveRecord::Schema.define() do
|
3
|
+
|
4
|
+
create_table "table_points", :force => true do |t|
|
5
|
+
t.string "data"
|
6
|
+
t.point "geom", :null=>false
|
7
|
+
end
|
8
|
+
|
9
|
+
create_table "table_keyword_column_points", :force => true do |t|
|
10
|
+
t.point "location", :null => false
|
11
|
+
end
|
12
|
+
|
13
|
+
create_table "table_line_strings", :force => true do |t|
|
14
|
+
t.integer "value"
|
15
|
+
t.line_string "geom", :null=>false
|
16
|
+
end
|
17
|
+
|
18
|
+
create_table "table_polygons", :force => true do |t|
|
19
|
+
t.polygon "geom", :null=>false
|
20
|
+
end
|
21
|
+
|
22
|
+
create_table "table_multi_points", :force => true do |t|
|
23
|
+
t.multi_point "geom", :null=>false
|
24
|
+
end
|
25
|
+
|
26
|
+
create_table "table_multi_line_strings", :force => true do |t|
|
27
|
+
t.multi_line_string "geom", :null=>false
|
28
|
+
end
|
29
|
+
|
30
|
+
create_table "table_multi_polygons", :force => true do |t|
|
31
|
+
t.multi_polygon "geom", :null=>false
|
32
|
+
end
|
33
|
+
|
34
|
+
create_table "table_geometries", :force => true do |t|
|
35
|
+
t.geometry "geom", :null=>false
|
36
|
+
end
|
37
|
+
|
38
|
+
create_table "table_geometry_collections", :force => true do |t|
|
39
|
+
t.geometry_collection "geom", :null=>false
|
40
|
+
end
|
41
|
+
|
42
|
+
create_table "table3dz_points", :force => true do |t|
|
43
|
+
t.column "data", :string
|
44
|
+
t.point "geom", :null => false , :with_z => true
|
45
|
+
end
|
46
|
+
|
47
|
+
create_table "table3dm_points", :force => true do |t|
|
48
|
+
t.point "geom", :null => false , :with_m => true
|
49
|
+
end
|
50
|
+
|
51
|
+
create_table "table4d_points", :force => true do |t|
|
52
|
+
t.point "geom", :null => false, :with_m => true, :with_z => true
|
53
|
+
end
|
54
|
+
|
55
|
+
create_table "table_srid_line_strings", :force => true do |t|
|
56
|
+
t.line_string "geom", :null => false , :srid => 4326
|
57
|
+
end
|
58
|
+
|
59
|
+
create_table "table_srid4d_polygons", :force => true do |t|
|
60
|
+
t.polygon "geom", :with_m => true, :with_z => true, :srid => 4326
|
61
|
+
end
|
62
|
+
|
63
|
+
create_table :cities, :force => true do |t|
|
64
|
+
t.string :data, :limit => 100
|
65
|
+
t.integer :value
|
66
|
+
t.polygon :geom, :null => false, :srid => 4326
|
67
|
+
end
|
68
|
+
|
69
|
+
create_table :positions, :force => true do |t|
|
70
|
+
t.string :data, :limit => 100
|
71
|
+
t.integer :value
|
72
|
+
t.point :geom, :null => false, :srid => 4326
|
73
|
+
end
|
74
|
+
|
75
|
+
create_table :streets, :force => true do |t|
|
76
|
+
t.string :data, :limit => 100
|
77
|
+
t.integer :value
|
78
|
+
t.line_string :geom, :null => false, :srid => 4326
|
79
|
+
end
|
80
|
+
|
81
|
+
create_table :common_geos, :force => true do |t|
|
82
|
+
t.string :data, :limit => 100
|
83
|
+
t.integer :value
|
84
|
+
t.point :geom, :null => false, :srid => 4326
|
85
|
+
end
|
86
|
+
|
87
|
+
create_table :diff_names, :force => true do |t|
|
88
|
+
t.string :data
|
89
|
+
t.point :the_geom, :null => false, :srid => 4326
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
class DiffColumn < ActiveRecord::Base
|
4
|
+
acts_as_geom :ponto => :point
|
5
|
+
end
|
6
|
+
|
7
|
+
class NotInDb < ActiveRecord::Base
|
8
|
+
acts_as_geom :geom
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "ActsAsGeom" do
|
12
|
+
|
13
|
+
it "should get the geom type" do
|
14
|
+
City.connection.columns("cities").select { |c| c.name == "geom" }[0]
|
15
|
+
City.get_geom_type(:geom).should eql(:polygon)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should get the geom type" do
|
19
|
+
Position.get_geom_type(:geom).should eql(:point)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should not interfere with migrations" do
|
23
|
+
NotInDb.get_geom_type(:geom).should be_nil
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should query a diff column name" do
|
27
|
+
# DiffColumn
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,254 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
describe "CommonSpatialAdapter" do
|
4
|
+
|
5
|
+
class Park < ActiveRecord::Base; end
|
6
|
+
class Viewpark < ActiveRecord::Base; end
|
7
|
+
|
8
|
+
describe "Migration" do
|
9
|
+
|
10
|
+
before(:all) do
|
11
|
+
@connection = ActiveRecord::Base.connection
|
12
|
+
ActiveRecord::Schema.define do
|
13
|
+
create_table "parks", :force => true do |t|
|
14
|
+
t.string "data", :limit => 100
|
15
|
+
t.integer "value"
|
16
|
+
t.polygon "geom", :null => false, :srid => 4326 , :with_z => true, :with_m => true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should test_creation_modification" do
|
22
|
+
@connection.columns("parks").length.should eql(4) # the 3 defined + id
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should test columns" do
|
26
|
+
@connection.columns("parks").each do |col|
|
27
|
+
if col.name == "geom"
|
28
|
+
col.class.should eql(ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn)
|
29
|
+
col.geometry_type.should eql(:polygon)
|
30
|
+
col.type.should eql(:geometry)
|
31
|
+
col.null.should be_false
|
32
|
+
col.srid.should eql(4326)
|
33
|
+
col.with_z.should be_true
|
34
|
+
col.with_m.should be_true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "Add" do
|
40
|
+
|
41
|
+
before(:all)do
|
42
|
+
ActiveRecord::Schema.define do
|
43
|
+
add_column "parks","geom2", :multi_point
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should test_creation_modification" do
|
48
|
+
@connection.columns("parks").length.should eql(5) # the 3 defined + id
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should test columns" do
|
52
|
+
@connection.columns("parks").each do |col|
|
53
|
+
if col.name == "geom2"
|
54
|
+
col.class.should eql(ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn)
|
55
|
+
col.geometry_type.should eql(:multi_point)
|
56
|
+
col.type.should eql(:geometry)
|
57
|
+
col.null.should be_true
|
58
|
+
col.srid.should eql(-1)
|
59
|
+
col.with_z.should be_false
|
60
|
+
col.with_m.should be_false
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "remove" do
|
67
|
+
before(:all) do
|
68
|
+
ActiveRecord::Schema.define do
|
69
|
+
remove_column "parks","geom2"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should test_creation_modification" do
|
74
|
+
@connection.columns("parks").length.should eql(4) # the 3 defined + id
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should get rid of the right one" do
|
78
|
+
@connection.columns("parks").each do |col|
|
79
|
+
violated if col.name == "geom2"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "indexes" do
|
85
|
+
|
86
|
+
it "should have 0 indexes" do
|
87
|
+
@connection.indexes("parks").length.should eql(0)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should create one" do
|
91
|
+
ActiveRecord::Schema.define do
|
92
|
+
add_index "parks","geom",:spatial=>true
|
93
|
+
end
|
94
|
+
@connection.indexes("parks").length.should eql(1)
|
95
|
+
@connection.indexes("parks")[0].spatial.should be_true
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should remove too" do
|
99
|
+
ActiveRecord::Schema.define do
|
100
|
+
remove_index "parks", "geom"
|
101
|
+
end
|
102
|
+
@connection.indexes("parks").length.should eql(0)
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should work with points" do
|
106
|
+
ActiveRecord::Schema.define do
|
107
|
+
remove_column "parks","geom2"
|
108
|
+
add_column "parks","geom2", :point
|
109
|
+
add_index "parks","geom2",:spatial=>true,:name => "example_spatial_index"
|
110
|
+
end
|
111
|
+
@connection.indexes("parks").length.should eql(1)
|
112
|
+
@connection.indexes("parks")[0].spatial.should be_true
|
113
|
+
@connection.indexes("parks")[0].name.should eql("example_spatial_index")
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
describe "Keywords" do
|
121
|
+
|
122
|
+
before(:all) do
|
123
|
+
ActiveRecord::Schema.define do
|
124
|
+
create_table "parks", :force => true do |t|
|
125
|
+
t.string "data", :limit => 100
|
126
|
+
t.integer "value"
|
127
|
+
#location is a postgreSQL keyword and is surrounded by double-quotes ("") when appearing in constraint descriptions ; tests a bug corrected in version 39
|
128
|
+
t.point "location", :null=>false,:srid => -1, :with_m => true, :with_z => true
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
@connection = ActiveRecord::Base.connection
|
133
|
+
@columns = @connection.columns("parks")
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should get the columsn length" do
|
137
|
+
@connection.indexes("parks").length.should eql(0) # the 3 defined + id
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should get the columns too" do
|
141
|
+
@connection.columns("parks").each do |col|
|
142
|
+
if col.name == "geom2"
|
143
|
+
col.class.should eql(ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn)
|
144
|
+
col.geometry_type.should eql(:point)
|
145
|
+
col.type.should eql(:geometry)
|
146
|
+
col.null.should be_true
|
147
|
+
col.srid.should eql(-1)
|
148
|
+
col.with_z.should be_false
|
149
|
+
col.with_m.should be_false
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe "Views" do
|
156
|
+
|
157
|
+
before(:all) do
|
158
|
+
ActiveRecord::Schema.define do
|
159
|
+
create_table "parks", :force => true do |t|
|
160
|
+
t.column "data" , :string, :limit => 100
|
161
|
+
t.column "value", :integer
|
162
|
+
t.column "geom", :point,:null=>false
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
Park.create!(:data => "Test", :geom => Point.from_x_y(1.2,4.5))
|
167
|
+
|
168
|
+
ActiveRecord::Base.connection.execute('CREATE VIEW viewparks as SELECT * from parks')
|
169
|
+
#if not ActiveRecord::Base.connection.execute("select * from geometry_columns where f_table_name = 'viewparks' and f_geometry_column = 'geom'") #do not add if already there
|
170
|
+
#mark the geom column in the view as geometric
|
171
|
+
#ActiveRecord::Base.connection.execute("insert into geometry_columns values ('','public','viewparks','geom',2,-1,'POINT')")
|
172
|
+
#end
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should works" do
|
176
|
+
pt = Viewpark.find(:first)
|
177
|
+
pt.data.should eql("Test")
|
178
|
+
pt.geom.should == Point.from_x_y(1.2,4.5)
|
179
|
+
end
|
180
|
+
|
181
|
+
after(:all) do
|
182
|
+
ActiveRecord::Base.connection.execute('DROP VIEW viewparks')
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe "Dump" do
|
187
|
+
before(:all) do
|
188
|
+
#Force the creation of a table
|
189
|
+
ActiveRecord::Schema.define do
|
190
|
+
create_table "parks", :force => true do |t|
|
191
|
+
t.string "data" , :limit => 100
|
192
|
+
t.integer "value"
|
193
|
+
t.multi_polygon "geom", :null=>false,:srid => -1, :with_m => true, :with_z => true
|
194
|
+
end
|
195
|
+
add_index "parks","geom",:spatial=>true,:name => "example_spatial_index"
|
196
|
+
end
|
197
|
+
#dump it : tables from other tests will be dumped too but not a problem
|
198
|
+
File.open('schema.rb', "w") do |file|
|
199
|
+
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
200
|
+
end
|
201
|
+
#load it again
|
202
|
+
load('schema.rb')
|
203
|
+
#delete the schema file
|
204
|
+
File.delete('schema.rb')
|
205
|
+
@connection = ActiveRecord::Base.connection
|
206
|
+
@columns = @connection.columns("parks")
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should create" do
|
210
|
+
@columns.length.should eql(4) # the 3 defined + id
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should get the same stuff bakc" do
|
214
|
+
@columns.each do |col|
|
215
|
+
if col.name == "geom"
|
216
|
+
col.class.should eql(ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn)
|
217
|
+
col.geometry_type.should eql(:multi_polygon)
|
218
|
+
col.type.should eql(:geometry)
|
219
|
+
col.null.should be_false
|
220
|
+
col.srid.should eql(-1)
|
221
|
+
col.with_z.should be_true
|
222
|
+
col.with_m.should be_true
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should get the indexes back too" do
|
228
|
+
@connection.indexes("parks").length.should eql(1)
|
229
|
+
@connection.indexes("parks")[0].spatial.should be_true
|
230
|
+
@connection.indexes("parks")[0].name.should eql("example_spatial_index")
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
|
235
|
+
describe "Fixtures" do
|
236
|
+
|
237
|
+
it "should test_long_fixture" do
|
238
|
+
Polygon.from_coordinates([[[144.857742,13.598263],
|
239
|
+
[144.862362,13.589922],[144.865169,13.587336],[144.862927,13.587665],
|
240
|
+
[144.861292,13.587321],[144.857597,13.585299],[144.847845,13.573858],
|
241
|
+
[144.846225,13.571014],[144.843605,13.566047],[144.842157,13.563831],
|
242
|
+
[144.841202,13.561991],[144.838305,13.556465],[144.834645,13.549919],
|
243
|
+
[144.834352,13.549395],[144.833825,13.548454],[144.831839,13.544451],
|
244
|
+
[144.830845,13.54081],[144.821543,13.545695],[144.8097993,13.55186285],
|
245
|
+
[144.814753,13.55755],[144.816744,13.56176944],[144.818862,13.566258],
|
246
|
+
[144.819402,13.568565],[144.822373,13.572223],[144.8242032,13.57381149],
|
247
|
+
[144.82634,13.575666],[144.83416,13.590365],[144.83514,13.595657],
|
248
|
+
[144.834284,13.59652],[144.834024,13.598031],[144.83719,13.598061],
|
249
|
+
[144.857742,13.598263]]]).to_fixture_format.split(/\s+/).should eql(["0103000020FFFFFFFF0100000020000000FBCC599F721B62404ED026874F322B40056A3178981B6240BF61A2410A2E2B406B10E676AF1B6240E486DF4DB72C2B40BC7A15199D1B6240F701486DE22C2B40CE893DB48F1B62400E828E56B52C2B40BA84436F711B624054C37E4FAC2B2B407862D68B211B62408F183DB7D0252B40D8817346141B6240C51D6FF25B242B40BFB7E9CFFE1A624071FF91E9D0212B401EA33CF3F21A624024F1F274AE202B408EEA7420EB1A6240ED4ACB48BD1F2B4058E20165D31A6240BEBC00FBE81C2B40A3586E69B51A6240E6E5B0FB8E192B40452BF702B31A6240FE2B2B4D4A192B40CA32C4B1AE1A624084B872F6CE182B403291D26C9E1A62408B8C0E48C2162B4072E14048961A624014B35E0CE5142B403FA88B144A1A6240732EC55565172B405CBA38E0E9196240344179C48D1A2B402C2AE274121A624005C58F31771D2B40892650C4221A62406D62793EA01F2B40FDBD141E341A62405D328E91EC212B40DD088B8A381A6240EC4CA1F31A232B40A1832EE1501A6240BB09BE69FA242B4046A863DF5F1A6240F23C9F9ECA252B400D6C9560711A624099D6A6B1BD262B4034F44F70B11A6240F5673F52442E2B409B728577B91A624056444DF4F9302B406FF25B74B21A6240E1D1C6116B312B4088821953B01A624005FD851E31322B4039D1AE42CA1A6240AF06280D35322B40FBCC599F721B62404ED026874F322B40"])
|
250
|
+
end
|
251
|
+
|
252
|
+
end
|
253
|
+
|
254
|
+
end
|