GeoRuby 1.3.3 → 1.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -73,9 +73,7 @@ module GeoRuby#:nodoc:
73
73
 
74
74
  #Outputs the geometry as a HexEWKB string. It is almost the same as a WKB string, except that each byte of a WKB string is replaced by its hexadecimal 2-character representation in a HexEWKB string.
75
75
  def as_hex_ewkb(allow_srid=true,allow_z=true,allow_m=true)
76
- str = ""
77
- as_ewkb(allow_srid,allow_z,allow_m).each_byte {|char| str << sprintf("%02x",char).upcase}
78
- str
76
+ as_ewkb(allow_srid, allow_z, allow_m).unpack('H*').join('').upcase
79
77
  end
80
78
  #Outputs the geometry as a strict HexWKB string
81
79
  def as_hex_wkb
@@ -24,7 +24,7 @@ spec = Gem::Specification::new do |s|
24
24
  s.platform = Gem::Platform::RUBY
25
25
 
26
26
  s.name = 'GeoRuby'
27
- s.version = "1.3.3"
27
+ s.version = "1.3.4"
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)
@@ -7,7 +7,7 @@ include GeoRuby::SimpleFeatures
7
7
  #add a method to_yaml to the Geometry class which will transform a geometry in a form suitable to be used in a YAML file (such as in a fixture)
8
8
  GeoRuby::SimpleFeatures::Geometry.class_eval do
9
9
  def to_fixture_format
10
- "!binary | #{[(255.chr * 4) + as_wkb].pack('m')}"
10
+ "!binary | #{[(255.chr * 4) + as_wkb].pack('m').gsub(/\s+/,"")}"
11
11
  end
12
12
  end
13
13
 
@@ -100,6 +100,16 @@ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
100
100
  end
101
101
  end
102
102
 
103
+ alias :original_tables :tables
104
+ def tables(name = nil) #:nodoc:
105
+ schemas = schema_search_path.split(/,/).map { |p| quote(p.strip) }.join(',')
106
+ original_tables(name) + query(<<-SQL, name).map { |row| row[0] }
107
+ SELECT viewname
108
+ FROM pg_views
109
+ WHERE schemaname IN (#{schemas})
110
+ SQL
111
+ end
112
+
103
113
  def create_table(name, options = {})
104
114
  table_definition = ActiveRecord::ConnectionAdapters::PostgreSQLTableDefinition.new(self)
105
115
  table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false
@@ -125,11 +135,12 @@ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
125
135
  end
126
136
 
127
137
  alias :original_remove_column :remove_column
128
- def remove_column(table_name,column_name)
138
+ def remove_column(table_name,column_name, options = {})
129
139
  columns(table_name).each do |col|
130
140
  if col.name == column_name.to_s
131
141
  #check if the column is geometric
132
- unless geometry_data_types[col.type].nil?
142
+ unless geometry_data_types[col.type].nil? or
143
+ (options[:remove_using_dropgeometrycolumn] == false)
133
144
  execute "SELECT DropGeometryColumn('#{table_name}','#{column_name}')"
134
145
  else
135
146
  original_remove_column(table_name,column_name)
@@ -140,7 +151,7 @@ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
140
151
 
141
152
  alias :original_add_column :add_column
142
153
  def add_column(table_name, column_name, type, options = {})
143
- unless geometry_data_types[type].nil?
154
+ unless geometry_data_types[type].nil? or (options[:create_using_addgeometrycolumn] == false)
144
155
  geom_column = ActiveRecord::ConnectionAdapters::PostgreSQLColumnDefinition.new(self,column_name, type, nil,nil,options[:null],options[:srid] || -1 , options[:with_z] || false , options[:with_m] || false)
145
156
  execute geom_column.to_sql(table_name)
146
157
  else
@@ -201,12 +212,20 @@ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
201
212
  raw_geom_infos = column_spatial_info(table_name)
202
213
 
203
214
  column_definitions(table_name).collect do |name, type, default, notnull|
204
- if type =~ /geometry/i and raw_geom_infos[name]
215
+ if type =~ /geometry/i
205
216
  raw_geom_info = raw_geom_infos[name]
206
217
  if ActiveRecord::VERSION::STRING >= "2.0.0"
207
- ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn.new(name, default,raw_geom_info.type, notnull == "f", raw_geom_info.srid, raw_geom_info.with_z, raw_geom_info.with_m)
218
+ if raw_geom_info.nil?
219
+ ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn.create_simplified(name,default,notnull == "f")
220
+ else
221
+ ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn.new(name, default,raw_geom_info.type, notnull == "f", raw_geom_info.srid, raw_geom_info.with_z, raw_geom_info.with_m)
222
+ end
208
223
  else
209
- ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn.new(name, default_value(default), raw_geom_info.type, notnull == "f", raw_geom_info.srid, raw_geom_info.with_z, raw_geom_info.with_m)
224
+ if raw_geom_info.nil?
225
+ ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn.create_simplified(name,default_value(default),notnull == "f")
226
+ else
227
+ ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn.new(name, default_value(default), raw_geom_info.type, notnull == "f", raw_geom_info.srid, raw_geom_info.with_z, raw_geom_info.with_m)
228
+ end
210
229
  end
211
230
  else
212
231
  if ActiveRecord::VERSION::STRING >= "2.0.0"
@@ -218,54 +237,41 @@ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
218
237
  end
219
238
  end
220
239
  end
221
-
222
- #Pete Deffendol's patch
223
- alias :original_disable_referential_integrity :disable_referential_integrity
224
- def disable_referential_integrity(&block) #:nodoc:
225
- ignore_tables = %w{ geometry_columns spatial_ref_sys }
226
- execute(tables.select { |name| !ignore_tables.include?(name) }.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";"))
227
- yield
228
- ensure
229
- execute(tables.select { |name| !ignore_tables.include?(name)}.collect { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER ALL" }.join(";"))
240
+
241
+ # For version of Rails where exists disable_referential_integrity
242
+ if self.instance_methods.include? "disable_referential_integrity"
243
+ #Pete Deffendol's patch
244
+ alias :original_disable_referential_integrity :disable_referential_integrity
245
+ def disable_referential_integrity(&block) #:nodoc:
246
+ ignore_tables = %w{ geometry_columns spatial_ref_sys }
247
+ execute(tables.select { |name| !ignore_tables.include?(name) }.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";"))
248
+ yield
249
+ ensure
250
+ execute(tables.select { |name| !ignore_tables.include?(name)}.collect { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER ALL" }.join(";"))
251
+ end
230
252
  end
231
253
 
232
254
  private
233
255
 
234
256
  def column_spatial_info(table_name)
235
257
  constr = query <<-end_sql
236
- SELECT pg_get_constraintdef(oid)
237
- FROM pg_constraint
238
- WHERE conrelid = '#{table_name}'::regclass
239
- AND contype = 'c'
258
+ SELECT * FROM geometry_columns WHERE f_table_name = '#{table_name}'
240
259
  end_sql
241
260
 
242
261
  raw_geom_infos = {}
243
262
  constr.each do |constr_def_a|
244
- constr_def = constr_def_a[0] #only 1 column in the result
245
- if constr_def =~ /geometrytype\(["']?([^"')]+)["']?\)\s*=\s*'([^']+)'/i
246
- column_name,type = $1,$2
247
- if type[-1] == ?M
248
- with_m = true
249
- type.chop!
250
- else
251
- with_m = false
252
- end
253
- raw_geom_info = raw_geom_infos[column_name] || ActiveRecord::ConnectionAdapters::RawGeomInfo.new
254
- raw_geom_info.type = type
255
- raw_geom_info.with_m = with_m
256
- raw_geom_infos[column_name] = raw_geom_info
257
- elsif constr_def =~ /ndims\(["']?([^"')]+)["']?\)\s*=\s*(\d+)/i
258
- column_name,dimension = $1,$2
259
- raw_geom_info = raw_geom_infos[column_name] || ActiveRecord::ConnectionAdapters::RawGeomInfo.new
260
- raw_geom_info.dimension = dimension.to_i
261
- raw_geom_infos[column_name] = raw_geom_info
262
- elsif constr_def =~ /srid\(["']?([^"')]+)["']?\)\s*=\s*(-?\d+)/i
263
- column_name,srid = $1,$2
264
- raw_geom_info = raw_geom_infos[column_name] || ActiveRecord::ConnectionAdapters::RawGeomInfo.new
265
- raw_geom_info.srid = srid.to_i
266
- raw_geom_infos[column_name] = raw_geom_info
267
- end #if constr_def
268
- end #constr.each
263
+ raw_geom_infos[constr_def_a[3]] ||= ActiveRecord::ConnectionAdapters::RawGeomInfo.new
264
+ raw_geom_infos[constr_def_a[3]].type = constr_def_a[6]
265
+ raw_geom_infos[constr_def_a[3]].dimension = constr_def_a[4].to_i
266
+ raw_geom_infos[constr_def_a[3]].srid = constr_def_a[5].to_i
267
+
268
+ if raw_geom_infos[constr_def_a[3]].type[-1] == ?M
269
+ raw_geom_infos[constr_def_a[3]].with_m = true
270
+ raw_geom_infos[constr_def_a[3]].type.chop!
271
+ else
272
+ raw_geom_infos[constr_def_a[3]].with_m = false
273
+ end
274
+ end
269
275
 
270
276
  raw_geom_infos.each_value do |raw_geom_info|
271
277
  #check the presence of z and m
@@ -311,7 +317,9 @@ module ActiveRecord
311
317
  attr_reader :geom_columns
312
318
 
313
319
  def column(name, type, options = {})
314
- unless @base.geometry_data_types[type.to_sym].nil?
320
+ unless (@base.geometry_data_types[type.to_sym].nil? or
321
+ (options[:create_using_addgeometrycolumn] == false))
322
+
315
323
  geom_column = PostgreSQLColumnDefinition.new(@base,name, type)
316
324
  geom_column.null = options[:null]
317
325
  geom_column.srid = options[:srid] || -1
@@ -392,6 +400,11 @@ module ActiveRecord
392
400
  return string unless string.is_a?(String)
393
401
  GeoRuby::SimpleFeatures::Geometry.from_hex_ewkb(string) rescue nil
394
402
  end
403
+
404
+ def self.create_simplified(name,default,null = true)
405
+ new(name,default,"geometry",null,nil,nil,nil)
406
+ end
407
+
395
408
  end
396
409
  end
397
410
  end
@@ -1,3 +1,4 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__) ,'../../gems/georuby/lib/'))
1
2
  require 'rake'
2
3
  require 'rake/testtask'
3
4
  require 'rake/rdoctask'
@@ -11,8 +11,9 @@ class FindPostgisTest < Test::Unit::TestCase
11
11
  def setup
12
12
  ActiveRecord::Schema.define() do
13
13
  create_table "parks", :force => true do |t|
14
- t.string "data", :limit => 100
15
- t.point "geom", :null=>false,:srid=>123
14
+ t.column "data" , :string, :limit => 100
15
+ t.column "value", :integer
16
+ t.column "geom", :point,:null=>false,:srid=>123
16
17
  end
17
18
  add_index "parks","geom",:spatial=>true,:name => "example_spatial_index"
18
19
  end
@@ -0,0 +1,23 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'test/unit'
4
+ require 'common/common_mysql'
5
+
6
+
7
+ class FixtureMysqlTest < Test::Unit::TestCase
8
+ def test_long_fixture
9
+ assert(1,Polygon.from_coordinates([[[144.857742,13.598263],
10
+ [144.862362,13.589922],[144.865169,13.587336],[144.862927,13.587665],
11
+ [144.861292,13.587321],[144.857597,13.585299],[144.847845,13.573858],
12
+ [144.846225,13.571014],[144.843605,13.566047],[144.842157,13.563831],
13
+ [144.841202,13.561991],[144.838305,13.556465],[144.834645,13.549919],
14
+ [144.834352,13.549395],[144.833825,13.548454],[144.831839,13.544451],
15
+ [144.830845,13.54081],[144.821543,13.545695],[144.8097993,13.55186285],
16
+ [144.814753,13.55755],[144.816744,13.56176944],[144.818862,13.566258],
17
+ [144.819402,13.568565],[144.822373,13.572223],
18
+ [144.8242032,13.57381149],[144.82634,13.575666],[144.83416,13.590365],
19
+ [144.83514,13.595657],[144.834284,13.59652],[144.834024,13.598031],
20
+ [144.83719,13.598061],[144.857742,13.598263]]]).to_fixture_format.split(/\s+/))
21
+
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'test/unit'
4
+ require 'common/common_postgis'
5
+
6
+
7
+ class FixturePostgisTest < Test::Unit::TestCase
8
+ def test_long_fixture
9
+ assert(1,Polygon.from_coordinates([[[144.857742,13.598263],
10
+ [144.862362,13.589922],[144.865169,13.587336],[144.862927,13.587665],
11
+ [144.861292,13.587321],[144.857597,13.585299],[144.847845,13.573858],
12
+ [144.846225,13.571014],[144.843605,13.566047],[144.842157,13.563831],
13
+ [144.841202,13.561991],[144.838305,13.556465],[144.834645,13.549919],
14
+ [144.834352,13.549395],[144.833825,13.548454],[144.831839,13.544451],
15
+ [144.830845,13.54081],[144.821543,13.545695],[144.8097993,13.55186285],
16
+ [144.814753,13.55755],[144.816744,13.56176944],[144.818862,13.566258],
17
+ [144.819402,13.568565],[144.822373,13.572223],
18
+ [144.8242032,13.57381149],[144.82634,13.575666],[144.83416,13.590365],
19
+ [144.83514,13.595657],[144.834284,13.59652],[144.834024,13.598031],
20
+ [144.83719,13.598061],[144.857742,13.598263]]]).to_fixture_format.split(/\s+/))
21
+
22
+ end
23
+ end
@@ -147,7 +147,7 @@ class MigrationMysqlTest < Test::Unit::TestCase
147
147
  t.column "City", :string, :limit => 50
148
148
  t.column "StateProvince", :string, :limit => 50
149
149
  t.column "PostalCode", :string, :limit => 30
150
- t.column "Geocode", :point, :null => false
150
+ t.column "Geocode", :geometry, :null => false #:geometry ok too : col.geometry_type test to change below
151
151
  end
152
152
  end
153
153
 
@@ -166,7 +166,7 @@ class MigrationMysqlTest < Test::Unit::TestCase
166
166
  ActiveRecord::Schema.define() do
167
167
  add_index "cx_geographiclocation", ["addressline1"], :name => "ix_cx_geographiclocation_addressline1"
168
168
  add_index "cx_geographiclocation", ["countryid"], :name => "ix_cx_geographiclocation_countryid"
169
- add_index "cx_geographiclocation", "Geocode", :spatial=>true
169
+ add_index "cx_geographiclocation", ["Geocode"], :spatial=>true
170
170
  end
171
171
 
172
172
  #test index
@@ -174,7 +174,7 @@ class MigrationMysqlTest < Test::Unit::TestCase
174
174
  assert(connection.indexes("cx_geographiclocation")[2].spatial)
175
175
 
176
176
  #insertion points
177
- 1.upto(1000) do |i|
177
+ 1.upto(10000) do |i|
178
178
  pt = CxGeographiclocation.new("CountryID" => i, "AddressLine1" =>"Bouyoul", "Geocode" => Point.from_x_y(-180 + rand(360) + rand(),-90 + rand(180) + rand())) #insert floats
179
179
  assert(pt.save)
180
180
  end
@@ -193,7 +193,7 @@ class MigrationMysqlTest < Test::Unit::TestCase
193
193
  pts = CxGeographiclocation.find_all_by_Geocode([[-181 + rand(),-91 + rand()],[181 + rand(),91 + rand()]]) #selects all : range limits are float
194
194
  assert(pts)
195
195
  assert(pts.is_a?(Array))
196
- assert_equal(1000,pts.length)
196
+ assert_equal(10000,pts.length)
197
197
  assert_equal("Bouyoul",pts[0].attributes["AddressLine1"])
198
198
 
199
199
  end
@@ -6,6 +6,9 @@ require 'common/common_postgis'
6
6
  class Park < ActiveRecord::Base
7
7
  end
8
8
 
9
+ class Viewpark < ActiveRecord::Base
10
+ end
11
+
9
12
 
10
13
  class MigrationPostgisTest < Test::Unit::TestCase
11
14
 
@@ -140,6 +143,34 @@ class MigrationPostgisTest < Test::Unit::TestCase
140
143
  end
141
144
 
142
145
 
146
+ def test_view
147
+ ActiveRecord::Schema.define do
148
+ create_table "parks", :force => true do |t|
149
+ t.column "data" , :string, :limit => 100
150
+ t.column "value", :integer
151
+ t.column "geom", :point,:null=>false
152
+ end
153
+ end
154
+
155
+ pt = Park.new(:data => "Test", :geom => Point.from_x_y(1.2,4.5))
156
+ assert(pt.save)
157
+
158
+ ActiveRecord::Base.connection.execute('CREATE VIEW viewparks as SELECT * from parks')
159
+ #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
160
+ #mark the geom column in the view as geometric
161
+ #ActiveRecord::Base.connection.execute("insert into geometry_columns values ('','public','viewparks','geom',2,-1,'POINT')")
162
+ #end
163
+
164
+ pt = Viewpark.find(:first)
165
+ assert(pt)
166
+ assert_equal("Test",pt.data)
167
+ assert_equal(Point.from_x_y(1.2,4.5),pt.geom)
168
+
169
+ ActiveRecord::Base.connection.execute('DROP VIEW viewparks')
170
+
171
+ end
172
+
173
+
143
174
  def test_dump
144
175
  #Force the creation of a table
145
176
  ActiveRecord::Schema.define do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: GeoRuby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 1.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guilhem Vellut
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-02 00:00:00 +01:00
12
+ date: 2009-03-20 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -75,6 +75,8 @@ files:
75
75
  - tools/lib/spatial_adapter/test/common/common_postgis.rb
76
76
  - tools/lib/spatial_adapter/test/find_mysql_test.rb
77
77
  - tools/lib/spatial_adapter/test/find_postgis_test.rb
78
+ - tools/lib/spatial_adapter/test/fixture_mysql_test.rb
79
+ - tools/lib/spatial_adapter/test/fixture_postgis_test.rb
78
80
  - tools/lib/spatial_adapter/test/migration_mysql_test.rb
79
81
  - tools/lib/spatial_adapter/test/migration_postgis_test.rb
80
82
  - tools/lib/spatial_adapter/test/models/models_mysql.rb