postgis_adapter 0.7.2 → 0.7.5

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.2
1
+ 0.7.5
@@ -10,22 +10,23 @@ module PostgisFunctions
10
10
 
11
11
  module ClassMethods
12
12
 
13
- # acts_as_geom :db_field => :geom_type
13
+ # has_geom :db_field => :geom_type
14
14
  # Examples:
15
15
  #
16
- # acts_as_geom :data => :point
17
- # acts_as_geom :geom => :line_string
18
- # acts_as_geom :geom => :polygon
16
+ # has_geom :data => :point
17
+ # has_geom :geom => :line_string
18
+ # has_geom :geom => :polygon
19
19
  #
20
- def acts_as_geom(*geom)
20
+ def has_geom(*geom)
21
21
  cattr_accessor :postgis_geoms
22
22
  self.postgis_geoms = geom[0] # {:columns => column
23
23
  send :include, case geom[0].values[0]
24
24
  when :point then PointFunctions
25
25
  when :polygon then PolygonFunctions
26
- when :line_string then LineStringFunctions
26
+ when :line_string, :multi_line_string then LineStringFunctions
27
27
  end unless geom[0].kind_of? Symbol
28
28
  end
29
+ alias :acts_as_geom :has_geom
29
30
 
30
31
  def get_geom_type(column)
31
32
  self.postgis_geoms.values[0] rescue nil
@@ -329,6 +329,9 @@ module PostgisFunctions
329
329
  postgis_calculate(:touches, [self, other])
330
330
  end
331
331
 
332
+ def st_collect(other=nil)
333
+ postgis_calculate(:collect, [self, other])
334
+ end
332
335
  #
333
336
  # The convex hull of a geometry represents the minimum closed geometry that
334
337
  # encloses all geometries within the set.
@@ -506,8 +509,8 @@ module PostgisFunctions
506
509
  #
507
510
  # http://geojson.org/
508
511
  #
509
- def as_geo_json
510
- postgis_calculate(:AsGeoJSON, self)
512
+ def as_geo_json(precision=15, bbox=0)
513
+ postgis_calculate(:AsGeoJSON, self, [precision, bbox])
511
514
  end
512
515
 
513
516
 
@@ -687,9 +690,19 @@ module PostgisFunctions
687
690
  # #float ST_Max_Distance(geometry g1, geometry g2);
688
691
  # postgis_calculate(:max_distance, [self, other])
689
692
  #end
690
- end
691
693
 
694
+ #
695
+ # Returns a (set of) LineString(s) formed by sewing together a MULTILINESTRING.
696
+ #
697
+ # Only use with MULTILINESTRING/LINESTRINGs. If you feed a polygon or geometry collection into this function, it will return an empty GEOMETRYCOLLECTION
698
+ #
699
+ # Returns geometry ST_LineMerge(geometry amultilinestring);
700
+ #
701
+ def line_merge
702
+ postgis_calculate(:LineMerge, self, { :stcollect => self})
703
+ end
692
704
 
705
+ end
693
706
  #
694
707
  #
695
708
  #
@@ -50,13 +50,18 @@ module PostgisFunctions
50
50
  # Implement a better way for options?
51
51
  if options.instance_of? Hash
52
52
  transform = options.delete(:transform)
53
+ stcollect = options.delete(:stcollect)
53
54
  options = nil
54
55
  end
55
56
 
56
57
  fields = tables.map { |f| "#{f[:uid]}.#{f[:column]}" } # W1.geom
57
58
  fields << not_db.map { |g| "'#{g.as_hex_ewkb}'::geometry"} unless not_db.empty?
58
59
  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
+ fields.map! { |f| "ST_Union(#{f})" } if stcollect # ST_Transform(W1.geom,x)
61
+ conditions = tables.map do |f|
62
+ raise unless f[:uid]
63
+ "#{f[:uid]}.id = #{f[:id]}"
64
+ end # W1.id = 5
60
65
  tables.map! { |f| "#{f[:name]} #{f[:uid]}" } # streets W1
61
66
 
62
67
  #
@@ -73,6 +78,7 @@ module PostgisFunctions
73
78
  fields = fields.join(" #{options} ")
74
79
  end
75
80
 
81
+
76
82
  sql = "SELECT #{opcode}(#{fields}) "
77
83
  sql << "FROM #{tables.join(",")} " unless tables.empty?
78
84
  sql << "WHERE #{conditions.join(" AND ")}" unless conditions.empty?
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{postgis_adapter}
8
- s.version = "0.7.2"
8
+ s.version = "0.7.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marcos Piccinini"]
12
- s.date = %q{2009-10-18}
12
+ s.date = %q{2010-01-29}
13
13
  s.description = %q{Execute PostGIS functions on Active Record}
14
14
  s.email = %q{x@nofxx.com}
15
15
  s.extra_rdoc_files = [
@@ -55,12 +55,12 @@ Gem::Specification.new do |s|
55
55
  "spec/db/schema_postgis.rb",
56
56
  "spec/postgis_adapter/acts_as_geom_spec.rb",
57
57
  "spec/postgis_adapter/common_spatial_adapter_spec.rb",
58
- "spec/postgis_functions_spec.rb",
59
- "spec/spec_helper.rb",
60
58
  "spec/postgis_adapter_spec.rb",
59
+ "spec/postgis_functions/bbox_spec.rb",
61
60
  "spec/postgis_functions/class_spec.rb",
62
61
  "spec/postgis_functions/common_spec.rb",
63
- "spec/postgis_functions/bbox_spec.rb"
62
+ "spec/postgis_functions_spec.rb",
63
+ "spec/spec_helper.rb"
64
64
  ]
65
65
 
66
66
  if s.respond_to? :specification_version then
@@ -73,3 +73,4 @@ Gem::Specification.new do |s|
73
73
  else
74
74
  end
75
75
  end
76
+
@@ -52,6 +52,10 @@ class Street < ActiveRecord::Base
52
52
  acts_as_geom :geom => :line_string
53
53
  end
54
54
 
55
+ class Road < ActiveRecord::Base
56
+ acts_as_geom :geom => :multi_line_string
57
+ end
58
+
55
59
  class CommonGeo < ActiveRecord::Base
56
60
  acts_as_geom :geom => :point
57
61
  end
@@ -3,53 +3,53 @@ ActiveRecord::Schema.define() do
3
3
 
4
4
  create_table "table_points", :force => true do |t|
5
5
  t.string "data"
6
- t.point "geom", :null=>false
6
+ t.point "geom", :null=>false, :srid => 4326
7
7
  end
8
8
 
9
9
  create_table "table_keyword_column_points", :force => true do |t|
10
- t.point "location", :null => false
10
+ t.point "location", :null => false, :srid => 4326
11
11
  end
12
12
 
13
13
  create_table "table_line_strings", :force => true do |t|
14
14
  t.integer "value"
15
- t.line_string "geom", :null=>false
15
+ t.line_string "geom", :null=>false, :srid => 4326
16
16
  end
17
17
 
18
18
  create_table "table_polygons", :force => true do |t|
19
- t.polygon "geom", :null=>false
19
+ t.polygon "geom", :null=>false, :srid => 4326
20
20
  end
21
21
 
22
22
  create_table "table_multi_points", :force => true do |t|
23
- t.multi_point "geom", :null=>false
23
+ t.multi_point "geom", :null=>false, :srid => 4326
24
24
  end
25
25
 
26
26
  create_table "table_multi_line_strings", :force => true do |t|
27
- t.multi_line_string "geom", :null=>false
27
+ t.multi_line_string "geom", :null=>false, :srid => 4326
28
28
  end
29
29
 
30
30
  create_table "table_multi_polygons", :force => true do |t|
31
- t.multi_polygon "geom", :null=>false
31
+ t.multi_polygon "geom", :null=>false, :srid => 4326
32
32
  end
33
33
 
34
34
  create_table "table_geometries", :force => true do |t|
35
- t.geometry "geom", :null=>false
35
+ t.geometry "geom", :null=>false, :srid => 4326
36
36
  end
37
37
 
38
38
  create_table "table_geometry_collections", :force => true do |t|
39
- t.geometry_collection "geom", :null=>false
39
+ t.geometry_collection "geom", :null=>false, :srid => 4326
40
40
  end
41
41
 
42
42
  create_table "table3dz_points", :force => true do |t|
43
43
  t.column "data", :string
44
- t.point "geom", :null => false , :with_z => true
44
+ t.point "geom", :null => false , :with_z => true, :srid => 4326
45
45
  end
46
46
 
47
47
  create_table "table3dm_points", :force => true do |t|
48
- t.point "geom", :null => false , :with_m => true
48
+ t.point "geom", :null => false , :with_m => true, :srid => 4326
49
49
  end
50
50
 
51
51
  create_table "table4d_points", :force => true do |t|
52
- t.point "geom", :null => false, :with_m => true, :with_z => true
52
+ t.point "geom", :null => false, :with_m => true, :with_z => true, :srid => 4326
53
53
  end
54
54
 
55
55
  create_table "table_srid_line_strings", :force => true do |t|
@@ -78,6 +78,12 @@ ActiveRecord::Schema.define() do
78
78
  t.line_string :geom, :null => false, :srid => 4326
79
79
  end
80
80
 
81
+ create_table :roads, :force => true do |t|
82
+ t.string :data, :limit => 100
83
+ t.integer :value
84
+ t.multi_line_string :geom, :null => false, :srid => 4326
85
+ end
86
+
81
87
  create_table :common_geos, :force => true do |t|
82
88
  t.string :data, :limit => 100
83
89
  t.integer :value
@@ -40,7 +40,7 @@ describe "CommonSpatialAdapter" do
40
40
 
41
41
  before(:all)do
42
42
  ActiveRecord::Schema.define do
43
- add_column "parks","geom2", :multi_point
43
+ add_column "parks","geom2", :multi_point, :srid => 4326
44
44
  end
45
45
  end
46
46
 
@@ -55,7 +55,7 @@ describe "CommonSpatialAdapter" do
55
55
  col.geometry_type.should eql(:multi_point)
56
56
  col.type.should eql(:geometry)
57
57
  col.null.should be_true
58
- col.srid.should eql(-1)
58
+ col.srid.should eql(4326)
59
59
  col.with_z.should be_false
60
60
  col.with_m.should be_false
61
61
  end
@@ -159,7 +159,7 @@ describe "CommonSpatialAdapter" do
159
159
  create_table "parks", :force => true do |t|
160
160
  t.column "data" , :string, :limit => 100
161
161
  t.column "value", :integer
162
- t.column "geom", :point,:null=>false
162
+ t.column "geom", :point,:null=>false, :srid => 4326
163
163
  end
164
164
  end
165
165
 
@@ -246,7 +246,7 @@ describe "CommonSpatialAdapter" do
246
246
  [144.819402,13.568565],[144.822373,13.572223],[144.8242032,13.57381149],
247
247
  [144.82634,13.575666],[144.83416,13.590365],[144.83514,13.595657],
248
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"])
249
+ [144.857742,13.598263]]]).to_fixture_format.split(/\s+/).should eql(["0103000020E61000000100000020000000FBCC599F721B62404ED026874F322B40056A3178981B6240BF61A2410A2E2B406B10E676AF1B6240E486DF4DB72C2B40BC7A15199D1B6240F701486DE22C2B40CE893DB48F1B62400E828E56B52C2B40BA84436F711B624054C37E4FAC2B2B407862D68B211B62408F183DB7D0252B40D8817346141B6240C51D6FF25B242B40BFB7E9CFFE1A624071FF91E9D0212B401EA33CF3F21A624024F1F274AE202B408EEA7420EB1A6240ED4ACB48BD1F2B4058E20165D31A6240BEBC00FBE81C2B40A3586E69B51A6240E6E5B0FB8E192B40452BF702B31A6240FE2B2B4D4A192B40CA32C4B1AE1A624084B872F6CE182B403291D26C9E1A62408B8C0E48C2162B4072E14048961A624014B35E0CE5142B403FA88B144A1A6240732EC55565172B405CBA38E0E9196240344179C48D1A2B402C2AE274121A624005C58F31771D2B40892650C4221A62406D62793EA01F2B40FDBD141E341A62405D328E91EC212B40DD088B8A381A6240EC4CA1F31A232B40A1832EE1501A6240BB09BE69FA242B4046A863DF5F1A6240F23C9F9ECA252B400D6C9560711A624099D6A6B1BD262B4034F44F70B11A6240F5673F52442E2B409B728577B91A624056444DF4F9302B406FF25B74B21A6240E1D1C6116B312B4088821953B01A624005FD851E31322B4039D1AE42CA1A6240AF06280D35322B40FBCC599F721B62404ED026874F322B40"])
250
250
  end
251
251
 
252
252
  end
@@ -8,6 +8,7 @@ describe "Common Functions" do
8
8
  @c2 ||= City.create!(:data => "City1", :geom => Polygon.from_coordinates([[[22,66],[65,65],[20,10],[22,66]],[[10,15],[15,11],[34,14],[10,15]]],4326))
9
9
  @c3 ||= City.create!(:data => "City3", :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]]],4326))
10
10
  @s1 ||= Street.create!(:data => "Street1", :geom => LineString.from_coordinates([[1,1],[2,2]],4326))
11
+ @sx ||= Street.create!(:data => "Street1", :geom => LineString.from_coordinates([[2,2],[4,4]],4326))
11
12
  @s2 ||= Street.create!(:data => "Street2", :geom => LineString.from_coordinates([[4,4],[7,7]],4326))
12
13
  @s3 ||= Street.create!(:data => "Street3", :geom => LineString.from_coordinates([[8,8],[18,18],[20,20],[25,25],[30,30],[38,38]],4326))
13
14
  @s4 ||= Street.create!(:data => "Street4", :geom => LineString.from_coordinates([[10,8],[15,18]],4326))
@@ -16,6 +17,9 @@ describe "Common Functions" do
16
17
  @p3 ||= Position.create!(:data => "Point3", :geom => Point.from_x_y(8,8,4326))
17
18
  @p4 ||= Position.create!(:data => "Point4", :geom => Point.from_x_y(18.1,18,4326))
18
19
  @p5 ||= Position.create!(:data => "Point5", :geom => Point.from_x_y(30,30,4326))
20
+ @m1 ||= Road.create(:data => "MG-050", :geom => MultiLineString.from_line_strings([@s1.geom, @sx.geom]))
21
+ @m2 ||= Road.create(:data => "MG-050", :geom => MultiLineString.from_line_strings([@s3.geom, @s4.geom]))
22
+ @p6 ||= Position.create!(:data => "Point6", :geom => Point.from_x_y(30.9999,30.9999,4326))
19
23
  end
20
24
 
21
25
  describe "Point" do
@@ -102,9 +106,18 @@ describe "Common Functions" do
102
106
  it "should export as GeoJSON" do
103
107
  @p1.as_geo_json.should eql("{\"type\":\"Point\",\"coordinates\":[1,1]}")
104
108
  end
109
+
110
+ it "should export as GeoJSON with variable precision" do
111
+ @p6.as_geo_json(1).should eql("{\"type\":\"Point\",\"coordinates\":[31,31]}")
112
+ end
113
+
114
+ it "should export as GeoJSON with variable precision and bounding box" do
115
+ @p6.as_geo_json(1,1).should eql("{\"type\":\"Point\",\"bbox\":[31.0,31.0,31.0,31.0],\"coordinates\":[31,31]}")
116
+ end
105
117
  end
106
118
 
107
119
 
120
+
108
121
  # it { @p3.x.should be_close(8.0, 0.1) }
109
122
  # it { @p3.y.should be_close(8.0, 0.1) }
110
123
  # it { @p3.z.should be_close(0.0, 0.1) }
@@ -239,6 +252,10 @@ describe "Common Functions" do
239
252
  end
240
253
  end
241
254
 
255
+ it "should have 1 geometry" do
256
+ @s1.should_not respond_to(:geometries)
257
+ end
258
+
242
259
  it "should intersect with linestring" do
243
260
  @s4.intersects?(@s3).should be_true
244
261
  end
@@ -293,7 +310,6 @@ describe "Common Functions" do
293
310
  @s1.as_geo_json.should eql("{\"type\":\"LineString\",\"coordinates\":[[1,1],[2,2]]}")
294
311
  end
295
312
  end
296
-
297
313
  end
298
314
 
299
315
  describe "Distance" do
@@ -369,6 +385,32 @@ describe "Common Functions" do
369
385
  str.geom[1].x.should be_close(7,0.0000001)
370
386
  str.geom[1].y.should be_close(7,0.0000001)
371
387
  end
388
+
389
+ describe "MultiLineString" do
390
+
391
+ it "should write nicely" do
392
+ @m1.geom.should be_instance_of(MultiLineString)
393
+ end
394
+
395
+ it "should have 2 geometries" do
396
+ @m1.geom.should have(2).geometries
397
+ end
398
+
399
+ it "should have 2 points on the geometry" do
400
+ @m1.geom.geometries[0].length.should eql(2)
401
+ end
402
+
403
+ it "should line merge!" do
404
+ merged = @m1.line_merge
405
+ merged.should be_instance_of(LineString)
406
+ merged.length.should eql(3)
407
+ end
408
+
409
+ it "should line merge collect" do
410
+ co = @m2.line_merge
411
+ co.should be_instance_of(LineString)
412
+ end
413
+ end
372
414
  end
373
415
 
374
416
  end
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,7 @@ require 'spec'
3
3
  require 'pg'
4
4
  require 'activerecord'
5
5
  $:.unshift((File.join(File.dirname(__FILE__), '..', 'lib')))
6
- gem 'activerecord', "=2.3.4"
6
+ gem 'activerecord', "=2.3.5"
7
7
  gem 'nofxx-georuby'
8
8
  require 'postgis_adapter'
9
9
 
@@ -12,11 +12,11 @@ $logger = Logger.new(StringIO.new)
12
12
  def $logger.write(d); self.info(d); end
13
13
  # $stdout = $logger
14
14
 
15
- GeoRuby::SimpleFeatures.srid = -1
15
+ # GeoRuby::SimpleFeatures.srid = -1
16
16
 
17
17
  ActiveRecord::Base.logger = $logger
18
18
  ActiveRecord::Base.establish_connection({ :adapter => "postgresql", :database => "postgis_adapter",
19
- :username => "postgres", :password => "" })
19
+ :username => "nofxx", :password => "" })
20
20
 
21
21
  PG_VERSION = ActiveRecord::Base.connection.select_value("SELECT version()").scan(/PostgreSQL ([\d\.]*)/)[0][0]
22
22
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postgis_adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcos Piccinini
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-18 01:00:00 -02:00
12
+ date: 2010-01-29 00:00:00 -02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -82,9 +82,9 @@ test_files:
82
82
  - spec/db/schema_postgis.rb
83
83
  - spec/postgis_adapter/acts_as_geom_spec.rb
84
84
  - spec/postgis_adapter/common_spatial_adapter_spec.rb
85
- - spec/postgis_functions_spec.rb
86
- - spec/spec_helper.rb
87
85
  - spec/postgis_adapter_spec.rb
86
+ - spec/postgis_functions/bbox_spec.rb
88
87
  - spec/postgis_functions/class_spec.rb
89
88
  - spec/postgis_functions/common_spec.rb
90
- - spec/postgis_functions/bbox_spec.rb
89
+ - spec/postgis_functions_spec.rb
90
+ - spec/spec_helper.rb