geos-extensions 0.1.5 → 0.1.6

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/Rakefile CHANGED
@@ -2,6 +2,9 @@
2
2
  # -*- ruby -*-
3
3
 
4
4
  require 'rubygems'
5
+
6
+ gem 'rdoc', '~> 3.12'
7
+
5
8
  require 'rubygems/package_task'
6
9
  require 'rake/testtask'
7
10
  require 'rdoc/task'
@@ -41,7 +44,6 @@ end
41
44
 
42
45
  desc 'Build docs'
43
46
  Rake::RDocTask.new do |t|
44
- require 'rdoc'
45
47
  t.main = 'README.rdoc'
46
48
  t.title = "Geos Extensions #{version}"
47
49
  t.rdoc_dir = 'doc'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.1.6
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "geos-extensions"
8
- s.version = "0.1.5"
8
+ s.version = "0.1.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["J Smith"]
12
- s.date = "2011-12-02"
12
+ s.date = "2012-02-28"
13
13
  s.description = "Extensions for the GEOS library."
14
14
  s.email = "code@zoocasa.com"
15
15
  s.extra_rdoc_files = [
@@ -37,6 +37,7 @@ Gem::Specification.new do |s|
37
37
  "lib/geos_extensions.rb",
38
38
  "lib/tasks/test.rake",
39
39
  "test/adapter_tests.rb",
40
+ "test/database.yml",
40
41
  "test/fixtures/foos.yml",
41
42
  "test/geometry_columns_tests.rb",
42
43
  "test/geospatial_scopes_tests.rb",
@@ -50,7 +51,7 @@ Gem::Specification.new do |s|
50
51
  ]
51
52
  s.homepage = "http://github.com/zoocasa/geos-extensions"
52
53
  s.require_paths = ["lib"]
53
- s.rubygems_version = "1.8.11"
54
+ s.rubygems_version = "1.8.16"
54
55
  s.summary = "Extensions for the GEOS library."
55
56
 
56
57
  if s.respond_to? :specification_version then
@@ -11,7 +11,7 @@ module ActiveRecord
11
11
  end
12
12
  end
13
13
 
14
- class PostgreSQLColumn < Column
14
+ class PostgreSQLColumn
15
15
  def simplified_type_with_geometry_type(field_type)
16
16
  if field_type == 'geometry'
17
17
  :geometry
@@ -22,27 +22,22 @@ module ActiveRecord
22
22
  alias_method_chain :simplified_type, :geometry_type
23
23
  end
24
24
 
25
- class PostgreSQLAdapter < AbstractAdapter
25
+ class PostgreSQLAdapter
26
26
  # Returns the geometry columns for the table.
27
27
  def geometry_columns(table_name, name = nil)
28
28
  return [] if !table_exists?(table_name)
29
29
 
30
30
  columns(table_name, name).select { |c| c.sql_type == 'geometry' }.collect do |c|
31
- res = execute(
32
- "SELECT * FROM geometry_columns WHERE f_table_name = #{quote(table_name)} AND f_geometry_column = #{quote(c.name)}",
31
+ res = select_rows(
32
+ "SELECT srid, coord_dimension FROM geometry_columns WHERE f_table_name = #{quote(table_name)} AND f_geometry_column = #{quote(c.name)}",
33
33
  "Geometry column load for #{table_name}"
34
34
  )
35
35
 
36
36
  PostgreSQLGeometryColumn.new(c.name).tap do |g|
37
37
  # since we're too stupid at the moment to understand
38
38
  # PostgreSQL schemas, let's just go with this:
39
- if res.ntuples == 1
40
- coord_dimension_idx, srid_idx =
41
- res.fields.index('coord_dimension'),
42
- res.fields.index('srid')
43
-
44
- g.srid = res.getvalue(0, srid_idx).to_i
45
- g.coord_dimension = res.getvalue(0, coord_dimension_idx).to_i
39
+ if res.length == 1
40
+ g.srid, g.coord_dimension = res.first.collect(&:to_i)
46
41
  end
47
42
  end
48
43
  end
@@ -17,7 +17,7 @@ module Geos
17
17
  autoload :ActiveRecord, File.join(GEOS_EXTENSIONS_BASE, *%w{ geos active_record_extensions })
18
18
  autoload :GoogleMaps, File.join(GEOS_EXTENSIONS_BASE, *%w{ geos google_maps })
19
19
 
20
- REGEXP_FLOAT = /(-?\d+(?:\.\d+)?)/
20
+ REGEXP_FLOAT = /(-?\d*(?:\.\d+)?|-?\d*(?:\.\d+?)[eE][-+]?\d+)/
21
21
  REGEXP_LAT_LNG = /#{REGEXP_FLOAT}\s*,\s*#{REGEXP_FLOAT}/
22
22
 
23
23
  REGEXP_WKT = /^\s*(?:SRID=(-?[0-9]+);)?(\s*[PLMCG].+)/im
@@ -329,6 +329,10 @@ module Geos
329
329
  (self.north * precision).ceil / precision
330
330
  ].join(',')
331
331
  end
332
+
333
+ def to_geojson(options = {})
334
+ self.to_geojsonable(options).to_json
335
+ end
332
336
  end
333
337
 
334
338
 
@@ -406,6 +410,17 @@ module Geos
406
410
  }
407
411
  end
408
412
  end
413
+
414
+ def to_geojsonable(options = {})
415
+ {
416
+ :type => 'LineString',
417
+ :coordinates => self.to_a
418
+ }
419
+ end
420
+
421
+ def to_geojson(options = {})
422
+ self.to_geojsonable(options).to_json
423
+ end
409
424
  end
410
425
 
411
426
 
@@ -509,6 +524,13 @@ module Geos
509
524
  { :type => 'point', :lat => cs.get_y(0), :lng => cs.get_x(0) }
510
525
  end
511
526
  end
527
+
528
+ def to_geojsonable(options = {})
529
+ {
530
+ :type => 'Point',
531
+ :coordinates => self.to_a
532
+ }
533
+ end
512
534
  end
513
535
 
514
536
 
@@ -516,6 +538,10 @@ module Geos
516
538
  def to_jsonable(options = {})
517
539
  self.coord_seq.to_jsonable(options)
518
540
  end
541
+
542
+ def to_geojsonable(options = {})
543
+ self.coord_seq.to_geojsonable(options)
544
+ end
519
545
  end
520
546
 
521
547
  class Polygon
@@ -646,6 +672,29 @@ module Geos
646
672
  ret
647
673
  end
648
674
  end
675
+
676
+ # Options:
677
+ #
678
+ # * :interior_rings - whether to include any interior rings in the output.
679
+ # The default is true.
680
+ def to_geojsonable(options = {})
681
+ options = {
682
+ :interior_rings => true
683
+ }.merge(options)
684
+
685
+ ret = {
686
+ :type => 'Polygon',
687
+ :coordinates => [ self.exterior_ring.coord_seq.to_a ]
688
+ }
689
+
690
+ if options[:interior_rings] && self.num_interior_rings > 0
691
+ ret[:coordinates].concat self.interior_rings.collect { |r|
692
+ r.coord_seq.to_a
693
+ }
694
+ end
695
+
696
+ ret
697
+ end
649
698
  end
650
699
 
651
700
 
@@ -695,5 +744,57 @@ module Geos
695
744
  def to_georss *args
696
745
  self.exterior_ring.to_georss(*args)
697
746
  end
747
+
748
+ def to_geojsonable(options = {})
749
+ {
750
+ :type => 'GeometryCollection',
751
+ :geometries => self.to_a.collect { |g| g.to_geojsonable(options) }
752
+ }
753
+ end
754
+ end
755
+
756
+ class MultiPolygon < GeometryCollection
757
+ def to_geojsonable(options = {})
758
+ options = {
759
+ :interior_rings => true
760
+ }.merge(options)
761
+
762
+ {
763
+ :type => 'MultiPolygon',
764
+ :coordinates => self.to_a.collect { |polygon|
765
+ coords = [ polygon.exterior_ring.coord_seq.to_a ]
766
+
767
+ if options[:interior_rings] && polygon.num_interior_rings > 0
768
+ coords.concat polygon.interior_rings.collect { |r|
769
+ r.coord_seq.to_a
770
+ }
771
+ end
772
+
773
+ coords
774
+ }
775
+ }
776
+ end
777
+ end
778
+
779
+ class MultiLineString < GeometryCollection
780
+ def to_geojsonable(options = {})
781
+ {
782
+ :type => 'MultiLineString',
783
+ :coordinates => self.to_a.collect { |linestring|
784
+ linestring.coord_seq.to_a
785
+ }
786
+ }
787
+ end
788
+ end
789
+
790
+ class MultiPoint < GeometryCollection
791
+ def to_geojsonable(options = {})
792
+ {
793
+ :type => 'MultiPoint',
794
+ :coordinates => self.to_a.collect { |point|
795
+ point.to_a
796
+ }
797
+ }
798
+ end
698
799
  end
699
800
  end
data/test/database.yml ADDED
@@ -0,0 +1,17 @@
1
+ ---
2
+ # To modify the test database parameters, create a new file called
3
+ # local_database.yml and go nuts with settings. The "jdbc" settings
4
+ # are merged into the "arunit" settings as JDBC works over a TCP
5
+ # socket and those sorts of connections generally require some user
6
+ # credentials.
7
+
8
+ arunit:
9
+ adapter: "postgresql"
10
+ database: "geos_extensions_unit_tests"
11
+ min_messages: "warning"
12
+ schema_search_path: "public"
13
+
14
+ jdbc:
15
+ host: "localhost"
16
+ adapter: "jdbcpostgresql"
17
+
@@ -56,22 +56,41 @@ class GoogleMapsApi3Tests < Test::Unit::TestCase
56
56
  end
57
57
 
58
58
  if defined?(JSON)
59
- def test_to_g_polygon
60
- assert_equal(
61
- %{new google.maps.Polygon({"paths": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(1.0, 1.0), new google.maps.LatLng(2.5, 2.5), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})},
62
- @polygon.to_g_polygon
63
- )
59
+ def poly_tester(type, expected, poly)
60
+ json = poly.gsub(/new google.maps.LatLng\(([^)]+)\)/, '[ \1 ]').gsub(/"map":\s*map/, %{"map": "map"})
64
61
 
65
- assert_equal(
66
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(1.0, 1.0), new google.maps.LatLng(2.5, 2.5), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})",
67
- @polygon.to_g_polygon(
68
- :stroke_color => '#b00b1e',
69
- :stroke_weight => 5,
70
- :stroke_opacity => 0.5,
71
- :fill_color => '#b00b1e',
72
- :map => 'map'
73
- )
74
- )
62
+ assert(json =~ /^new google.maps.#{type}\((.+)\)$/)
63
+
64
+ assert_equal(expected, JSON.load($~[1]))
65
+ end
66
+
67
+ def test_to_g_polygon
68
+ poly_tester('Polygon', {
69
+ "paths" => [
70
+ [0.0, 0.0], [1.0, 1.0], [2.5, 2.5], [5.0, 5.0], [0.0, 0.0]
71
+ ]
72
+ }, @polygon.to_g_polygon)
73
+
74
+ poly_tester('Polygon', {
75
+ 'strokeColor' => '#b00b1e',
76
+ 'strokeWeight' => 5,
77
+ 'strokeOpacity' => 0.5,
78
+ 'fillColor' => '#b00b1e',
79
+ 'map' => 'map',
80
+ 'paths' => [
81
+ [ 0.0, 0.0 ],
82
+ [ 1.0, 1.0 ],
83
+ [ 2.5, 2.5 ],
84
+ [ 5.0, 5.0 ],
85
+ [ 0.0, 0.0 ]
86
+ ]
87
+ }, @polygon.to_g_polygon(
88
+ :stroke_color => '#b00b1e',
89
+ :stroke_weight => 5,
90
+ :stroke_opacity => 0.5,
91
+ :fill_color => '#b00b1e',
92
+ :map => 'map'
93
+ ))
75
94
  end
76
95
 
77
96
  def test_to_g_polygon_with_multi_polygon
@@ -82,6 +101,7 @@ class GoogleMapsApi3Tests < Test::Unit::TestCase
82
101
  ((20 20, 20 25, 25 25, 25 20, 20 20))
83
102
  )'
84
103
  )
104
+
85
105
  options = {
86
106
  :stroke_color => '#b00b1e',
87
107
  :stroke_weight => 5,
@@ -90,59 +110,83 @@ class GoogleMapsApi3Tests < Test::Unit::TestCase
90
110
  :map => 'map'
91
111
  }
92
112
 
93
- assert_equal(
94
- ["new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(5.0, 0.0), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})",
95
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [new google.maps.LatLng(10.0, 10.0), new google.maps.LatLng(15.0, 10.0), new google.maps.LatLng(15.0, 15.0), new google.maps.LatLng(10.0, 15.0), new google.maps.LatLng(10.0, 10.0)]})",
96
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [new google.maps.LatLng(20.0, 20.0), new google.maps.LatLng(25.0, 20.0), new google.maps.LatLng(25.0, 25.0), new google.maps.LatLng(20.0, 25.0), new google.maps.LatLng(20.0, 20.0)]})"],
97
- multi_polygon.to_g_polygon(
98
- :stroke_color => '#b00b1e',
99
- :stroke_weight => 5,
100
- :stroke_opacity => 0.5,
101
- :fill_color => '#b00b1e',
102
- :map => 'map'
103
- )
104
- )
105
-
106
- assert_equal(
107
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [[new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(5.0, 0.0), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 5.0), new google.maps.LatLng(0.0, 0.0)], [new google.maps.LatLng(10.0, 10.0), new google.maps.LatLng(15.0, 10.0), new google.maps.LatLng(15.0, 15.0), new google.maps.LatLng(10.0, 15.0), new google.maps.LatLng(10.0, 10.0)], [new google.maps.LatLng(20.0, 20.0), new google.maps.LatLng(25.0, 20.0), new google.maps.LatLng(25.0, 25.0), new google.maps.LatLng(20.0, 25.0), new google.maps.LatLng(20.0, 20.0)]]})",
108
- multi_polygon.to_g_polygon({
109
- :stroke_color => '#b00b1e',
110
- :stroke_weight => 5,
111
- :stroke_opacity => 0.5,
112
- :fill_color => '#b00b1e',
113
- :map => 'map'
114
- }, {
115
- :single => true
116
- })
117
- )
113
+ expected = [ {
114
+ "paths" => [[0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0], [0.0, 0.0]],
115
+ "strokeColor" => "#b00b1e",
116
+ "strokeOpacity" => 0.5,
117
+ "fillColor" => "#b00b1e",
118
+ "strokeWeight" => 5,
119
+ "map" => "map"
120
+ }, {
121
+ "paths" => [[10.0, 10.0], [15.0, 10.0], [15.0, 15.0], [10.0, 15.0], [10.0, 10.0]],
122
+ "strokeColor" => "#b00b1e",
123
+ "strokeOpacity" => 0.5,
124
+ "fillColor" => "#b00b1e",
125
+ "strokeWeight" => 5,
126
+ "map" => "map"
127
+ }, {
128
+ "paths" => [[20.0, 20.0], [25.0, 20.0], [25.0, 25.0], [20.0, 25.0], [20.0, 20.0]],
129
+ "strokeColor" => "#b00b1e",
130
+ "strokeOpacity" => 0.5,
131
+ "fillColor" => "#b00b1e",
132
+ "strokeWeight" => 5,
133
+ "map" => "map"
134
+ } ]
135
+
136
+ multi_polygon.to_g_polygon(options).each_with_index do |polygon, i|
137
+ poly_tester('Polygon', expected[i], polygon)
138
+ end
118
139
 
119
- assert_equal(
120
- "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"fillColor\": \"#b00b1e\", \"map\": map, \"paths\": [[new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(5.0, 0.0), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 5.0), new google.maps.LatLng(0.0, 0.0)], [new google.maps.LatLng(10.0, 10.0), new google.maps.LatLng(15.0, 10.0), new google.maps.LatLng(15.0, 15.0), new google.maps.LatLng(10.0, 15.0), new google.maps.LatLng(10.0, 10.0)], [new google.maps.LatLng(20.0, 20.0), new google.maps.LatLng(25.0, 20.0), new google.maps.LatLng(25.0, 25.0), new google.maps.LatLng(20.0, 25.0), new google.maps.LatLng(20.0, 20.0)]]})",
121
- multi_polygon.to_g_polygon_single(
122
- :stroke_color => '#b00b1e',
123
- :stroke_weight => 5,
124
- :stroke_opacity => 0.5,
125
- :fill_color => '#b00b1e',
126
- :map => 'map'
127
- )
128
- )
140
+ poly_tester("Polygon", {
141
+ "paths" => [
142
+ [[0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0], [0.0, 0.0]],
143
+ [[10.0, 10.0], [15.0, 10.0], [15.0, 15.0], [10.0, 15.0], [10.0, 10.0]],
144
+ [[20.0, 20.0], [25.0, 20.0], [25.0, 25.0], [20.0, 25.0], [20.0, 20.0]]
145
+ ],
146
+ "strokeColor" => "#b00b1e",
147
+ "strokeOpacity" => 0.5,
148
+ "fillColor" => "#b00b1e",
149
+ "strokeWeight" => 5,
150
+ "map" => "map"
151
+ }, multi_polygon.to_g_polygon(options, {
152
+ :single => true
153
+ }))
154
+
155
+ poly_tester("Polygon", {
156
+ "paths" => [
157
+ [[0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0], [0.0, 0.0]],
158
+ [[10.0, 10.0], [15.0, 10.0], [15.0, 15.0], [10.0, 15.0], [10.0, 10.0]],
159
+ [[20.0, 20.0], [25.0, 20.0], [25.0, 25.0], [20.0, 25.0], [20.0, 20.0]]
160
+ ],
161
+ "strokeColor" => "#b00b1e",
162
+ "strokeOpacity" => 0.5,
163
+ "fillColor" => "#b00b1e",
164
+ "strokeWeight" => 5,
165
+ "map" => "map"
166
+ }, multi_polygon.to_g_polygon_single(options))
129
167
  end
130
168
 
131
169
  def test_to_g_polyline
132
- assert_equal(
133
- "new google.maps.Polyline({\"path\": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(1.0, 1.0), new google.maps.LatLng(2.5, 2.5), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})",
134
- @polygon.to_g_polyline
135
- )
136
-
137
- assert_equal(
138
- "new google.maps.Polyline({\"strokeColor\": \"#b00b1e\", \"strokeWeight\": 5, \"strokeOpacity\": 0.5, \"map\": map, \"path\": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(1.0, 1.0), new google.maps.LatLng(2.5, 2.5), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})",
139
- @polygon.to_g_polyline(
140
- :stroke_color => '#b00b1e',
141
- :stroke_weight => 5,
142
- :stroke_opacity => 0.5,
143
- :map => 'map'
144
- )
145
- )
170
+ poly_tester("Polyline", {
171
+ "path" => [
172
+ [0.0, 0.0], [1.0, 1.0], [2.5, 2.5], [5.0, 5.0], [0.0, 0.0]
173
+ ]
174
+ }, @polygon.to_g_polyline)
175
+
176
+ poly_tester("Polyline", {
177
+ "strokeColor" => "#b00b1e",
178
+ "strokeWeight" => 5,
179
+ "strokeOpacity" => 0.5,
180
+ "map" => "map",
181
+ "path" => [
182
+ [0.0, 0.0], [1.0, 1.0], [2.5, 2.5], [5.0, 5.0], [0.0, 0.0]
183
+ ]
184
+ }, @polygon.to_g_polyline(
185
+ :stroke_color => '#b00b1e',
186
+ :stroke_weight => 5,
187
+ :stroke_opacity => 0.5,
188
+ :map => 'map'
189
+ ))
146
190
  end
147
191
 
148
192
  def test_to_g_marker
data/test/reader_tests.rb CHANGED
@@ -132,4 +132,13 @@ class GeosReaderTests < Test::Unit::TestCase
132
132
 
133
133
  assert_equal('POLYGON ((0 0, 10 10, 0 10, 0 0))', geom.to_wkt(:trim => true))
134
134
  end
135
+
136
+ def test_read_e_notation
137
+ assert_equal('POINT (60081.5 -0.000858307)', Geos.read('-8.58307e-04, 6.00815E+4').to_wkt(:trim => true))
138
+ assert_equal('POINT (60081.5 -0.000858307)', Geos.read('-8.58307e-04, 6.00815e4').to_wkt(:trim => true))
139
+ end
140
+
141
+ def test_no_leading_digits
142
+ assert_equal('POINT (0.01 0.02)', Geos.read('.02, .01').to_wkt(:trim => true))
143
+ end
135
144
  end
data/test/test_helper.rb CHANGED
@@ -3,7 +3,7 @@ require 'rubygems'
3
3
  require 'test/unit'
4
4
 
5
5
  if ENV['TEST_ACTIVERECORD']
6
- ACTIVERECORD_GEM_VERSION = ENV['ACTIVERECORD_GEM_VERSION'] || '~> 3.0.3'
6
+ ACTIVERECORD_GEM_VERSION = ENV['ACTIVERECORD_GEM_VERSION'] || '~> 3.2.0'
7
7
  gem 'activerecord', ACTIVERECORD_GEM_VERSION
8
8
 
9
9
  POSTGIS_PATHS = [
@@ -19,6 +19,10 @@ if ENV['TEST_ACTIVERECORD']
19
19
  require 'active_record'
20
20
  require 'active_record/fixtures'
21
21
  require 'logger'
22
+
23
+ if ActiveRecord::VERSION::STRING < '3.0'
24
+ require 'fake_arel'
25
+ end
22
26
  end
23
27
 
24
28
  require File.join(File.dirname(__FILE__), %w{ .. lib geos_extensions })
@@ -35,33 +39,47 @@ end
35
39
  if ENV['TEST_ACTIVERECORD']
36
40
  ActiveRecord::Base.logger = Logger.new("debug.log")
37
41
  ActiveRecord::Base.configurations = {
38
- 'arunit' => {
39
- :adapter => 'postgresql',
40
- :database => 'geos_extensions_unit_tests',
41
- :min_messages => 'warning',
42
- :schema_search_path => 'public'
43
- }
42
+ 'arunit' => {}
44
43
  }
45
44
 
45
+ %w{
46
+ database.yml
47
+ local_database.yml
48
+ }.each do |file|
49
+ file = File.join('test', file)
50
+
51
+ next unless File.exists?(file)
52
+
53
+ configuration = YAML.load(File.read(file))
54
+
55
+ if configuration['arunit']
56
+ ActiveRecord::Base.configurations['arunit'].merge!(configuration['arunit'])
57
+ end
58
+
59
+ if defined?(JRUBY_VERSION) && configuration['jdbc']
60
+ ActiveRecord::Base.configurations['arunit'].merge!(configuration['jdbc'])
61
+ end
62
+ end
63
+
46
64
  ActiveRecord::Base.establish_connection 'arunit'
47
65
  ARBC = ActiveRecord::Base.connection
48
66
 
49
- if postgresql_version = ARBC.query('SELECT version()').flatten.to_s
67
+ if postgresql_version = ARBC.select_rows('SELECT version()').first.first
50
68
  puts "PostgreSQL info from version(): #{postgresql_version}"
51
69
  end
52
70
 
53
71
  puts "Checking for PostGIS install"
54
72
  2.times do
55
73
  begin
56
- if postgis_version = ARBC.query('SELECT postgis_full_version()').flatten.to_s
74
+ if postgis_version = ARBC.select_rows('SELECT postgis_full_version()').first.first
57
75
  puts "PostGIS info from postgis_full_version(): #{postgis_version}"
58
76
  break
59
77
  end
60
78
  rescue ActiveRecord::StatementInvalid
61
79
  puts "Trying to install PostGIS. If this doesn't work, you'll have to do this manually!"
62
80
 
63
- plpgsql = ARBC.query(%{SELECT count(*) FROM pg_language WHERE lanname = 'plpgsql'}).to_s
64
- if plpgsql == '0'
81
+ plpgsql = ARBC.select_rows(%{SELECT count(*) FROM pg_language WHERE lanname = 'plpgsql'}).first.first.to_i
82
+ if plpgsql == 0
65
83
  ARBC.execute(%{CREATE LANGUAGE plpgsql})
66
84
  end
67
85
 
@@ -138,6 +156,24 @@ module TestHelper
138
156
 
139
157
  POLYGON_WITH_INTERIOR_RING = "POLYGON((0 0, 5 0, 5 5, 0 5, 0 0),(4 4, 4 1, 1 1, 1 4, 4 4))"
140
158
 
159
+ LINESTRING_WKT = "LINESTRING (0 0, 5 5, 5 10, 10 10)"
160
+
161
+ GEOMETRYCOLLECTION_WKT = 'GEOMETRYCOLLECTION (
162
+ MULTIPOLYGON (
163
+ ((0 0, 1 0, 1 1, 0 1, 0 0)),
164
+ (
165
+ (10 10, 10 14, 14 14, 14 10, 10 10),
166
+ (11 11, 11 12, 12 12, 12 11, 11 11)
167
+ )
168
+ ),
169
+ POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)),
170
+ POLYGON ((0 0, 5 0, 5 5, 0 5, 0 0), (4 4, 4 1, 1 1, 1 4, 4 4)),
171
+ MULTILINESTRING ((0 0, 2 3), (10 10, 3 4)),
172
+ LINESTRING (0 0, 2 3),
173
+ MULTIPOINT ((0 0), (2 3)),
174
+ POINT (9 0)
175
+ )'
176
+
141
177
  BOUNDS_G_LAT_LNG = "((0.1, 0.1), (5.2, 5.2))"
142
178
  BOUNDS_G_LAT_LNG_URL_VALUE = '0.1,0.1,5.2,5.2'
143
179
 
data/test/writer_tests.rb CHANGED
@@ -127,4 +127,215 @@ class GeosWriterTests < Test::Unit::TestCase
127
127
  end
128
128
  end
129
129
  end
130
+
131
+ if defined?(JSON)
132
+ def test_to_geojson_coord_seq
133
+ coord_seq = Geos.read(POLYGON_WKT).exterior_ring.coord_seq
134
+ json = coord_seq.to_geojson
135
+
136
+ assert_equal({
137
+ "type" => "LineString",
138
+ "coordinates" => [
139
+ [0.0, 0.0],
140
+ [1.0, 1.0],
141
+ [2.5, 2.5],
142
+ [5.0, 5.0],
143
+ [0.0, 0.0]
144
+ ]
145
+ }, JSON.load(json))
146
+ end
147
+
148
+ def test_to_geojson_polygon
149
+ polygon = Geos.read(POLYGON_WKT)
150
+ json = polygon.to_geojson
151
+
152
+ assert_equal({
153
+ "type" => "Polygon",
154
+ "coordinates" => [
155
+ [
156
+ [0.0, 0.0],
157
+ [1.0, 1.0],
158
+ [2.5, 2.5],
159
+ [5.0, 5.0],
160
+ [0.0, 0.0]
161
+ ]
162
+ ]
163
+ }, JSON.load(json))
164
+ end
165
+
166
+ def test_to_geojson_polygon_with_interior_ring
167
+ polygon = Geos.read(POLYGON_WITH_INTERIOR_RING)
168
+
169
+ assert_equal({
170
+ "type" => "Polygon",
171
+ "coordinates" => [
172
+ [
173
+ [0.0, 0.0],
174
+ [5.0, 0.0],
175
+ [5.0, 5.0],
176
+ [0.0, 5.0],
177
+ [0.0, 0.0]
178
+ ], [
179
+ [4.0, 4.0],
180
+ [4.0, 1.0],
181
+ [1.0, 1.0],
182
+ [1.0, 4.0],
183
+ [4.0, 4.0]
184
+ ]
185
+ ]
186
+ }, JSON.load(polygon.to_geojson))
187
+
188
+ assert_equal({
189
+ "type" => "Polygon",
190
+ "coordinates" => [
191
+ [
192
+ [0.0, 0.0],
193
+ [5.0, 0.0],
194
+ [5.0, 5.0],
195
+ [0.0, 5.0],
196
+ [0.0, 0.0]
197
+ ]
198
+ ]
199
+ }, JSON.load(polygon.to_geojson(:interior_rings => false)))
200
+ end
201
+
202
+ def test_to_geojson_point
203
+ point = Geos.read(POINT_WKT)
204
+
205
+ assert_equal({
206
+ "type" => "Point",
207
+ "coordinates" => [10.0, 10.01]
208
+ }, JSON.load(point.to_geojson))
209
+ end
210
+
211
+ def test_to_geojson_line_string
212
+ linestring = Geos.read(LINESTRING_WKT)
213
+
214
+ assert_equal({
215
+ "type" => "LineString",
216
+ "coordinates" => [
217
+ [0.0, 0.0],
218
+ [5.0, 5.0],
219
+ [5.0, 10.0],
220
+ [10.0, 10.0]
221
+ ]}, JSON.load(linestring.to_geojson))
222
+ end
223
+
224
+ def test_to_geojson_geometry_collection
225
+ collection = Geos.read(GEOMETRYCOLLECTION_WKT)
226
+
227
+ assert_equal({
228
+ "type" => "GeometryCollection",
229
+ "geometries" => [ {
230
+ "type" => "MultiPolygon",
231
+ "coordinates" => [
232
+ [
233
+ [
234
+ [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]
235
+ ]
236
+ ], [
237
+ [
238
+ [10.0, 10.0], [10.0, 14.0], [14.0, 14.0], [14.0, 10.0], [10.0, 10.0]
239
+ ], [
240
+ [11.0, 11.0], [11.0, 12.0], [12.0, 12.0], [12.0, 11.0], [11.0, 11.0]
241
+ ]
242
+ ]
243
+ ]
244
+ }, {
245
+ "type" => "Polygon",
246
+ "coordinates" => [
247
+ [
248
+ [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]
249
+ ]
250
+ ]
251
+ }, {
252
+ "type" => "Polygon",
253
+ "coordinates" => [
254
+ [
255
+ [0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0], [0.0, 0.0]
256
+ ], [
257
+ [4.0, 4.0], [4.0, 1.0], [1.0, 1.0], [1.0, 4.0], [4.0, 4.0]
258
+ ]
259
+ ]
260
+ }, {
261
+ "type" => "MultiLineString",
262
+ "coordinates" => [
263
+ [
264
+ [0.0, 0.0], [2.0, 3.0]
265
+ ], [
266
+ [10.0, 10.0], [3.0, 4.0]
267
+ ]
268
+ ]
269
+ }, {
270
+ "type" => "LineString",
271
+ "coordinates" => [
272
+ [0.0, 0.0], [2.0, 3.0]
273
+ ]
274
+ }, {
275
+ "type" => "MultiPoint",
276
+ "coordinates" => [
277
+ [0.0, 0.0], [2.0, 3.0]
278
+ ]
279
+ }, {
280
+ "type" => "Point",
281
+ "coordinates" => [9.0, 0.0]
282
+ }
283
+ ] }, JSON.load(collection.to_geojson))
284
+
285
+ assert_equal({
286
+ "type" => "GeometryCollection",
287
+ "geometries" => [ {
288
+ "type" => "MultiPolygon",
289
+ "coordinates" => [
290
+ [
291
+ [
292
+ [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]
293
+ ]
294
+ ], [
295
+ [
296
+ [10.0, 10.0], [10.0, 14.0], [14.0, 14.0], [14.0, 10.0], [10.0, 10.0]
297
+ ]
298
+ ]
299
+ ]
300
+ }, {
301
+ "type" => "Polygon",
302
+ "coordinates" => [
303
+ [
304
+ [0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]
305
+ ]
306
+ ]
307
+ }, {
308
+ "type" => "Polygon",
309
+ "coordinates" => [
310
+ [
311
+ [0.0, 0.0], [5.0, 0.0], [5.0, 5.0], [0.0, 5.0], [0.0, 0.0]
312
+ ]
313
+ ]
314
+ }, {
315
+ "type" => "MultiLineString",
316
+ "coordinates" => [
317
+ [
318
+ [0.0, 0.0], [2.0, 3.0]
319
+ ],
320
+ [
321
+ [10.0, 10.0], [3.0, 4.0]
322
+ ]
323
+ ]
324
+ }, {
325
+ "type" => "LineString",
326
+ "coordinates" => [
327
+ [0.0, 0.0], [2.0, 3.0]
328
+ ]
329
+ }, {
330
+ "type" => "MultiPoint",
331
+ "coordinates" => [
332
+ [0.0, 0.0], [2.0, 3.0]
333
+ ]
334
+ }, {
335
+ "type" => "Point",
336
+ "coordinates" => [9.0, 0.0]
337
+ } ]
338
+ }, JSON.load(collection.to_geojson(:interior_rings => false)))
339
+ end
340
+ end
130
341
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geos-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-02 00:00:00.000000000 Z
12
+ date: 2012-02-28 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Extensions for the GEOS library.
15
15
  email: code@zoocasa.com
@@ -39,6 +39,7 @@ files:
39
39
  - lib/geos_extensions.rb
40
40
  - lib/tasks/test.rake
41
41
  - test/adapter_tests.rb
42
+ - test/database.yml
42
43
  - test/fixtures/foos.yml
43
44
  - test/geometry_columns_tests.rb
44
45
  - test/geospatial_scopes_tests.rb
@@ -69,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
70
  version: '0'
70
71
  requirements: []
71
72
  rubyforge_project:
72
- rubygems_version: 1.8.11
73
+ rubygems_version: 1.8.16
73
74
  signing_key:
74
75
  specification_version: 3
75
76
  summary: Extensions for the GEOS library.