geos-extensions 0.0.7 → 0.1.0

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.
@@ -0,0 +1,169 @@
1
+
2
+ module Geos::GoogleMaps::Api3
3
+ module Geometry
4
+ UNESCAPED_MARKER_OPTIONS = %w{
5
+ icon
6
+ map
7
+ position
8
+ shadow
9
+ shape
10
+ }.freeze
11
+
12
+ # Returns a new LatLngBounds object with the proper GLatLngs in place
13
+ # for determining the geometry bounds.
14
+ def to_g_lat_lng_bounds_api3(options = {})
15
+ "new google.maps.LatLngBounds(#{self.lower_left.to_g_lat_lng_api3(options)}, #{self.upper_right.to_g_lat_lng_api3(options)})"
16
+ end
17
+
18
+ # Returns a String in Google Maps' LatLngBounds#toString() format.
19
+ def to_g_lat_lng_bounds_string_api3(precision = 10)
20
+ "((#{self.lower_left.to_g_url_value_api3(precision)}), (#{self.upper_right.to_g_url_value_api3(precision)}))"
21
+ end
22
+
23
+ # Returns a new Polyline.
24
+ def to_g_polyline_api3(polyline_options = {}, options = {})
25
+ self.coord_seq.to_g_polyline_api3(polyline_options, options)
26
+ end
27
+
28
+ # Returns a new Polygon.
29
+ def to_g_polygon_api3(polygon_options = {}, options = {})
30
+ self.coord_seq.to_g_polygon_api3(polygon_options, options)
31
+ end
32
+
33
+ # Returns a new Marker at the centroid of the geometry. The options
34
+ # Hash works the same as the Google Maps API MarkerOptions class does,
35
+ # but allows for underscored Ruby-like options which are then converted
36
+ # to the appropriate camel-cased Javascript options.
37
+ def to_g_marker_api3(marker_options = {}, options = {})
38
+ options = {
39
+ :escape => [],
40
+ :lat_lng_options => {}
41
+ }.merge(options)
42
+
43
+ opts = Geos::Helper.camelize_keys(marker_options)
44
+ opts[:position] = self.centroid.to_g_lat_lng(options[:lat_lng_options])
45
+ json = Geos::Helper.escape_json(opts, UNESCAPED_MARKER_OPTIONS - options[:escape])
46
+
47
+ "new google.maps.Marker(#{json})"
48
+ end
49
+ end
50
+
51
+ module CoordinateSequence
52
+ # Returns a Ruby Array of GLatLngs.
53
+ def to_g_lat_lng_api3(options = {})
54
+ self.to_a.collect do |p|
55
+ "new google.maps.LatLng(#{p[1]}, #{p[0]})"
56
+ end
57
+ end
58
+
59
+ # Returns a new GPolyline. Note that this GPolyline just uses whatever
60
+ # coordinates are found in the sequence in order, so it might not
61
+ # make much sense at all.
62
+ #
63
+ # The options Hash follows the Google Maps API arguments to the
64
+ # GPolyline constructor and include :color, :weight, :opacity and
65
+ # :options. 'null' is used in place of any unset options.
66
+ def to_g_polyline_api3(polyline_options = {}, options = {})
67
+ klass = if options[:short_class]
68
+ 'GPolyline'
69
+ else
70
+ 'google.maps.Polyline'
71
+ end
72
+
73
+ poly_opts = if polyline_options[:polyline_options]
74
+ Geos::Helper.camelize_keys(polyline_options[:polyline_options])
75
+ end
76
+
77
+ args = [
78
+ (polyline_options[:color] ? "'#{Geos::Helper.escape_javascript(polyline_options[:color])}'" : 'null'),
79
+ (polyline_options[:weight] || 'null'),
80
+ (polyline_options[:opacity] || 'null'),
81
+ (poly_opts ? poly_opts.to_json : 'null')
82
+ ].join(', ')
83
+
84
+ "new #{klass}([#{self.to_g_lat_lng(options).join(', ')}], #{args})"
85
+ end
86
+
87
+ # Returns a new GPolygon. Note that this GPolygon just uses whatever
88
+ # coordinates are found in the sequence in order, so it might not
89
+ # make much sense at all.
90
+ #
91
+ # The options Hash follows the Google Maps API arguments to the
92
+ # GPolygon constructor and include :stroke_color, :stroke_weight,
93
+ # :stroke_opacity, :fill_color, :fill_opacity and :options. 'null' is
94
+ # used in place of any unset options.
95
+ def to_g_polygon_api3(polygon_options = {}, options = {})
96
+ klass = if options[:short_class]
97
+ 'GPolygon'
98
+ else
99
+ 'google.maps.Polygon'
100
+ end
101
+
102
+ poly_opts = if polygon_options[:polygon_options]
103
+ Geos::Helper.camelize_keys(polygon_options[:polygon_options])
104
+ end
105
+
106
+ args = [
107
+ (polygon_options[:stroke_color] ? "'#{Geos::Helper.escape_javascript(polygon_options[:stroke_color])}'" : 'null'),
108
+ (polygon_options[:stroke_weight] || 'null'),
109
+ (polygon_options[:stroke_opacity] || 'null'),
110
+ (polygon_options[:fill_color] ? "'#{Geos::Helper.escape_javascript(polygon_options[:fill_color])}'" : 'null'),
111
+ (polygon_options[:fill_opacity] || 'null'),
112
+ (poly_opts ? poly_opts.to_json : 'null')
113
+ ].join(', ')
114
+ "new #{klass}([#{self.to_g_lat_lng_api3(options).join(', ')}], #{args})"
115
+ end
116
+ end
117
+
118
+ module Point
119
+ # Returns a new LatLng.
120
+ def to_g_lat_lng_api3(options = {})
121
+ no_wrap = if options[:no_wrap]
122
+ ', true'
123
+ end
124
+
125
+ "new google.maps.LatLng(#{self.lat}, #{self.lng}#{no_wrap})"
126
+ end
127
+
128
+ # Returns a new Point
129
+ def to_g_point_api3(options = {})
130
+ "new google.maps.Point(#{self.x}, #{self.y})"
131
+ end
132
+ end
133
+
134
+ module Polygon
135
+ # Returns a GPolyline of the exterior ring of the Polygon. This does
136
+ # not take into consideration any interior rings the Polygon may
137
+ # have.
138
+ def to_g_polyline_api3(polyline_options = {}, options = {})
139
+ self.exterior_ring.to_g_polyline_api3(polyline_options, options)
140
+ end
141
+
142
+ # Returns a GPolygon of the exterior ring of the Polygon. This does
143
+ # not take into consideration any interior rings the Polygon may
144
+ # have.
145
+ def to_g_polygon_api3(polygon_options = {}, options = {})
146
+ self.exterior_ring.to_g_polygon_api3(polygon_options, options)
147
+ end
148
+ end
149
+
150
+ module GeometryCollection
151
+ # Returns a Ruby Array of GPolylines for each geometry in the
152
+ # collection.
153
+ def to_g_polyline_api3(polyline_options = {}, options = {})
154
+ self.collect do |p|
155
+ p.to_g_polyline_api3(polyline_options, options)
156
+ end
157
+ end
158
+
159
+ # Returns a Ruby Array of GPolygons for each geometry in the
160
+ # collection.
161
+ def to_g_polygon_api3(polygon_options = {}, options = {})
162
+ self.collect do |p|
163
+ p.to_g_polygon_api3(polygon_options, options)
164
+ end
165
+ end
166
+ end
167
+ end
168
+
169
+ Geos::GoogleMaps.use_api(2)
@@ -0,0 +1,28 @@
1
+
2
+ module Geos
3
+ module GoogleMaps
4
+ class << self
5
+ def use_api(version)
6
+ version_const = Geos::GoogleMaps.const_get("Api#{version}")
7
+ version_const.constants.each do |c|
8
+ mod = version_const.const_get(c)
9
+ klass = Geos.const_get(c)
10
+ regex = %r{_api#{version}$}
11
+
12
+ if !klass.include?(mod)
13
+ klass.send(:include, mod)
14
+ end
15
+
16
+ mod.instance_methods.each do |method|
17
+ klass.send(:alias_method, method.to_s.sub(regex, ''), method)
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ autoload :PolylineEncoder, File.join(GEOS_EXTENSIONS_BASE, *%w{ geos google_maps polyline_encoder })
24
+ autoload :ApiIncluder, File.join(GEOS_EXTENSIONS_BASE, *%w{ geos google_maps api_includer })
25
+ autoload :Api2, File.join(GEOS_EXTENSIONS_BASE, *%w{ geos google_maps api_2 })
26
+ autoload :Api3, File.join(GEOS_EXTENSIONS_BASE, *%w{ geos google_maps api_3 })
27
+ end
28
+ end
@@ -0,0 +1,6 @@
1
+
2
+ module Geos
3
+ class RailsEngine < Rails::Engine
4
+ end
5
+ end
6
+
@@ -2,12 +2,12 @@
2
2
  require File.join(File.dirname(__FILE__), *%w{ geos_extensions })
3
3
 
4
4
  if defined?(ActiveRecord)
5
- require File.join(GEOS_EXTENSIONS_BASE, *%w{ active_record_extensions connection_adapters postgresql_adapter })
6
- require File.join(GEOS_EXTENSIONS_BASE, *%w{ active_record_extensions geometry_columns })
7
- require File.join(GEOS_EXTENSIONS_BASE, *%w{ active_record_extensions geospatial_scopes })
5
+ require File.join(Geos::GEOS_EXTENSIONS_BASE, *%w{ geos active_record_extensions })
8
6
  end
9
7
 
10
- if defined?(Rails)
11
- require File.join(File.dirname(__FILE__), %w{ .. rails railtie })
8
+ if defined?(Rails) && Rails::VERSION::MAJOR >= 3
9
+ require File.join(Geos::GEOS_EXTENSIONS_BASE, %w{ geos rails engine })
12
10
  end
13
11
 
12
+ Geos::GoogleMaps.use_api(2)
13
+
@@ -1,6 +1,4 @@
1
1
 
2
- GEOS_EXTENSIONS_BASE = File.join(File.dirname(__FILE__))
3
-
4
2
  begin
5
3
  if !ENV['USE_BINARY_GEOS']
6
4
  require 'ffi-geos'
@@ -12,9 +10,12 @@ require 'geos' unless defined?(Geos)
12
10
 
13
11
  # Some custom extensions to the SWIG-based Geos Ruby extension.
14
12
  module Geos
15
- autoload :Helper, File.join(GEOS_EXTENSIONS_BASE, 'geos_helper')
16
- autoload :ActiveRecord, File.join(GEOS_EXTENSIONS_BASE, 'active_record_extensions')
17
- autoload :GoogleMaps, File.join(GEOS_EXTENSIONS_BASE, 'google_maps')
13
+ GEOS_EXTENSIONS_BASE = File.join(File.dirname(__FILE__))
14
+ GEOS_EXTENSIONS_VERSION = File.read(File.join(GEOS_EXTENSIONS_BASE, %w{ .. VERSION })).strip rescue nil
15
+
16
+ autoload :Helper, File.join(GEOS_EXTENSIONS_BASE, *%w{ geos geos_helper })
17
+ autoload :ActiveRecord, File.join(GEOS_EXTENSIONS_BASE, *%w{ geos active_record_extensions })
18
+ autoload :GoogleMaps, File.join(GEOS_EXTENSIONS_BASE, *%w{ geos google_maps })
18
19
 
19
20
  REGEXP_WKT = /^(?:SRID=(-?[0-9]+);)?(\s*[PLMCG].+)/i
20
21
  REGEXP_WKB_HEX = /^[A-Fa-f0-9\s]+$/
@@ -294,52 +295,6 @@ module Geos
294
295
  alias :w :left
295
296
  alias :west :left
296
297
 
297
- # Returns a new GLatLngBounds object with the proper GLatLngs in place
298
- # for determining the geometry bounds.
299
- def to_g_lat_lng_bounds(options = {})
300
- klass = if options[:short_class]
301
- 'GLatLngBounds'
302
- else
303
- 'google.maps.LatLngBounds'
304
- end
305
-
306
- "new #{klass}(#{self.lower_left.to_g_lat_lng(options)}, #{self.upper_right.to_g_lat_lng(options)})"
307
- end
308
-
309
- # Returns a String in Google Maps' GLatLngBounds#toString() format.
310
- def to_g_lat_lng_bounds_string(precision = 10)
311
- "((#{self.lower_left.to_g_url_value(precision)}), (#{self.upper_right.to_g_url_value(precision)}))"
312
- end
313
-
314
- # Returns a new GPolyline.
315
- def to_g_polyline polyline_options = {}, options = {}
316
- self.coord_seq.to_g_polyline polyline_options, options
317
- end
318
-
319
- # Returns a new GPolygon.
320
- def to_g_polygon polygon_options = {}, options = {}
321
- self.coord_seq.to_g_polygon polygon_options, options
322
- end
323
-
324
- # Returns a new GMarker at the centroid of the geometry. The options
325
- # Hash works the same as the Google Maps API GMarkerOptions class does,
326
- # but allows for underscored Ruby-like options which are then converted
327
- # to the appropriate camel-cased Javascript options.
328
- def to_g_marker marker_options = {}, options = {}
329
- klass = if options[:short_class]
330
- 'GMarker'
331
- else
332
- 'google.maps.Marker'
333
- end
334
-
335
- opts = marker_options.inject({}) do |memo, (k, v)|
336
- memo[Geos::Helper.camelize(k.to_s)] = v
337
- memo
338
- end
339
-
340
- "new #{klass}(#{self.centroid.to_g_lat_lng(options)}, #{opts.to_json})"
341
- end
342
-
343
298
  # Spit out Google's JSON geocoder Point format. The extra 0 is added
344
299
  # on as Google's format seems to like including the Z coordinate.
345
300
  def to_g_json_point
@@ -385,83 +340,6 @@ module Geos
385
340
 
386
341
 
387
342
  class CoordinateSequence
388
- # Returns a Ruby Array of GLatLngs.
389
- def to_g_lat_lng(options = {})
390
- klass = if options[:short_class]
391
- 'GLatLng'
392
- else
393
- 'google.maps.LatLng'
394
- end
395
-
396
- self.to_a.collect do |p|
397
- "new #{klass}(#{p[1]}, #{p[0]})"
398
- end
399
- end
400
-
401
- # Returns a new GPolyline. Note that this GPolyline just uses whatever
402
- # coordinates are found in the sequence in order, so it might not
403
- # make much sense at all.
404
- #
405
- # The options Hash follows the Google Maps API arguments to the
406
- # GPolyline constructor and include :color, :weight, :opacity and
407
- # :options. 'null' is used in place of any unset options.
408
- def to_g_polyline polyline_options = {}, options = {}
409
- klass = if options[:short_class]
410
- 'GPolyline'
411
- else
412
- 'google.maps.Polyline'
413
- end
414
-
415
- poly_opts = if polyline_options[:polyline_options]
416
- polyline_options[:polyline_options].inject({}) do |memo, (k, v)|
417
- memo[Geos::Helper.camelize(k.to_s)] = v
418
- memo
419
- end
420
- end
421
-
422
- args = [
423
- (polyline_options[:color] ? "'#{Geos::Helper.escape_javascript(polyline_options[:color])}'" : 'null'),
424
- (polyline_options[:weight] || 'null'),
425
- (polyline_options[:opacity] || 'null'),
426
- (poly_opts ? poly_opts.to_json : 'null')
427
- ].join(', ')
428
-
429
- "new #{klass}([#{self.to_g_lat_lng(options).join(', ')}], #{args})"
430
- end
431
-
432
- # Returns a new GPolygon. Note that this GPolygon just uses whatever
433
- # coordinates are found in the sequence in order, so it might not
434
- # make much sense at all.
435
- #
436
- # The options Hash follows the Google Maps API arguments to the
437
- # GPolygon constructor and include :stroke_color, :stroke_weight,
438
- # :stroke_opacity, :fill_color, :fill_opacity and :options. 'null' is
439
- # used in place of any unset options.
440
- def to_g_polygon polygon_options = {}, options = {}
441
- klass = if options[:short_class]
442
- 'GPolygon'
443
- else
444
- 'google.maps.Polygon'
445
- end
446
-
447
- poly_opts = if polygon_options[:polygon_options]
448
- polygon_options[:polygon_options].inject({}) do |memo, (k, v)|
449
- memo[Geos::Helper.camelize(k.to_s)] = v
450
- memo
451
- end
452
- end
453
-
454
- args = [
455
- (polygon_options[:stroke_color] ? "'#{Geos::Helper.escape_javascript(polygon_options[:stroke_color])}'" : 'null'),
456
- (polygon_options[:stroke_weight] || 'null'),
457
- (polygon_options[:stroke_opacity] || 'null'),
458
- (polygon_options[:fill_color] ? "'#{Geos::Helper.escape_javascript(polygon_options[:fill_color])}'" : 'null'),
459
- (polygon_options[:fill_opacity] || 'null'),
460
- (poly_opts ? poly_opts.to_json : 'null')
461
- ].join(', ')
462
- "new #{klass}([#{self.to_g_lat_lng(options).join(', ')}], #{args})"
463
- end
464
-
465
343
  # Returns a Ruby Array of Arrays of coordinates within the
466
344
  # CoordinateSequence in the form [ x, y, z ].
467
345
  def to_a
@@ -539,28 +417,6 @@ module Geos
539
417
 
540
418
 
541
419
  class Point
542
- # Returns a new GLatLng.
543
- def to_g_lat_lng(options = {})
544
- klass = if options[:short_class]
545
- 'GLatLng'
546
- else
547
- 'google.maps.LatLng'
548
- end
549
-
550
- "new #{klass}(#{self.lat}, #{self.lng})"
551
- end
552
-
553
- # Returns a new GPoint
554
- def to_g_point(options = {})
555
- klass = if options[:short_class]
556
- 'GPoint'
557
- else
558
- 'google.maps.Point'
559
- end
560
-
561
- "new #{klass}(#{self.x}, #{self.y})"
562
- end
563
-
564
420
  # Returns the Y coordinate of the Point, which is actually the
565
421
  # latitude.
566
422
  def lat
@@ -651,21 +507,13 @@ module Geos
651
507
  end
652
508
 
653
509
 
654
- class Polygon
655
- # Returns a GPolyline of the exterior ring of the Polygon. This does
656
- # not take into consideration any interior rings the Polygon may
657
- # have.
658
- def to_g_polyline polyline_options = {}, options = {}
659
- self.exterior_ring.to_g_polyline polyline_options, options
660
- end
661
-
662
- # Returns a GPolygon of the exterior ring of the Polygon. This does
663
- # not take into consideration any interior rings the Polygon may
664
- # have.
665
- def to_g_polygon polygon_options = {}, options = {}
666
- self.exterior_ring.to_g_polygon polygon_options, options
510
+ class LineString
511
+ def to_jsonable(options = {})
512
+ self.coord_seq.to_jsonable(options)
667
513
  end
514
+ end
668
515
 
516
+ class Polygon
669
517
  # Build some XmlMarkup for XML. You can set various KML options like
670
518
  # tessellate, altitudeMode, etc. Use Rails/Ruby-style code and it
671
519
  # will be converted automatically, i.e. :altitudeMode, not
@@ -736,6 +584,7 @@ module Geos
736
584
  def to_jsonable options = {}
737
585
  options = {
738
586
  :encoded => true,
587
+ :level => 3,
739
588
  :interior_rings => false
740
589
  }.merge options
741
590
 
@@ -751,7 +600,8 @@ module Geos
751
600
  :type => 'polygon',
752
601
  :encoded => true,
753
602
  :polylines => [ Geos::GoogleMaps::PolylineEncoder.encode(
754
- self.exterior_ring.coord_seq.to_a
603
+ self.exterior_ring.coord_seq.to_a,
604
+ options[:level]
755
605
  ).merge(:bounds => {
756
606
  :sw => self.lower_left.to_a,
757
607
  :ne => self.upper_right.to_a
@@ -762,7 +612,10 @@ module Geos
762
612
 
763
613
  if options[:interior_rings] && self.num_interior_rings > 0
764
614
  (0..(self.num_interior_rings) - 1).to_a.each do |n|
765
- ret[:polylines] << Geos::GoogleMaps::PolylineEncoder.encode(self.interior_ring_n(n).coord_seq.to_a)
615
+ ret[:polylines] << Geos::GoogleMaps::PolylineEncoder.encode(
616
+ self.interior_ring_n(n).coord_seq.to_a,
617
+ options[:level]
618
+ )
766
619
  end
767
620
  end
768
621
  ret
@@ -815,22 +668,6 @@ module Geos
815
668
  self.get_geometry_n(self.num_geometries - 1) if self.num_geometries > 0
816
669
  end
817
670
 
818
- # Returns a Ruby Array of GPolylines for each geometry in the
819
- # collection.
820
- def to_g_polyline polyline_options = {}, options = {}
821
- self.collect do |p|
822
- p.to_g_polyline polyline_options, options
823
- end
824
- end
825
-
826
- # Returns a Ruby Array of GPolygons for each geometry in the
827
- # collection.
828
- def to_g_polygon polygon_options = {}, options = {}
829
- self.collect do |p|
830
- p.to_g_polygon polygon_options, options
831
- end
832
- end
833
-
834
671
  # Returns a Hash suitable for converting to JSON.
835
672
  def to_jsonable options = {}
836
673
  self.collect do |p|
File without changes
@@ -67,6 +67,15 @@ if ENV['TEST_ACTIVERECORD']
67
67
  ids_tester(:st_dwithin, [ 'POINT(5 5)', 10 ], [ 1, 2, 3 ])
68
68
  end
69
69
 
70
+ def test_allow_null
71
+ begin
72
+ foo = Foo.create(:name => 'four')
73
+ ids_tester(:st_contains, [ 'POINT(3 3)', { :allow_null => true } ], [ 3, foo.id ])
74
+ ensure
75
+ Foo.find_by_name('four').destroy
76
+ end
77
+ end
78
+
70
79
  def test_with_column
71
80
  assert_equal([1, 2, 3], Foo.st_disjoint('POINT(100 100)', :column => :the_other_geom).all.collect(&:id).sort)
72
81
  end
@@ -0,0 +1,185 @@
1
+
2
+ $: << File.dirname(__FILE__)
3
+ require 'test_helper'
4
+
5
+ begin
6
+ require 'json'
7
+ rescue LoadError
8
+ # do nothing
9
+ end
10
+
11
+ class GoogleMapsApi2Tests < Test::Unit::TestCase
12
+ include TestHelper
13
+
14
+ def setup
15
+ Geos::GoogleMaps.use_api(2)
16
+
17
+ @point = Geos.read(POINT_EWKB)
18
+ @polygon = Geos.read(POLYGON_EWKB)
19
+ end
20
+
21
+ def test_to_g_lat_lng
22
+ assert_equal("new google.maps.LatLng(10.01, 10.0)", @point.to_g_lat_lng)
23
+ assert_equal("new GLatLng(10.01, 10.0)", @point.to_g_lat_lng(:short_class => true))
24
+ end
25
+
26
+ def test_to_jsonable
27
+ assert_equal({
28
+ :type => "point",
29
+ :lat => 10.01,
30
+ :lng => 10.0
31
+ }, @point.to_jsonable)
32
+
33
+ assert_equal({
34
+ :type => "polygon",
35
+ :polylines => [{
36
+ :points => "??_ibE_ibE_~cH_~cH_hgN_hgN~po]~po]",
37
+ :bounds => {
38
+ :sw => [ 0.0, 0.0 ],
39
+ :ne => [ 5.0, 5.0 ]
40
+ },
41
+ :levels=>"BBBBB"
42
+ }],
43
+ :options => {},
44
+ :encoded => true
45
+ }, @polygon.to_jsonable)
46
+ end
47
+
48
+ if defined?(JSON)
49
+ def test_to_g_polygon
50
+ assert_equal(
51
+ "new google.maps.Polygon([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)], null, null, null, null, null, null)",
52
+ @polygon.to_g_polygon
53
+ )
54
+
55
+ assert_equal(
56
+ "new GPolygon([new GLatLng(0.0, 0.0), new GLatLng(1.0, 1.0), new GLatLng(2.5, 2.5), new GLatLng(5.0, 5.0), new GLatLng(0.0, 0.0)], null, null, null, null, null, null)",
57
+ @polygon.to_g_polygon({}, :short_class => true)
58
+ )
59
+
60
+ assert_equal(
61
+ "new google.maps.Polygon([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)], '#b00b1e', 5, 0.5, '#b00b1e', null, {\"mouseOutTolerence\":5})",
62
+ @polygon.to_g_polygon(
63
+ :stroke_color => '#b00b1e',
64
+ :stroke_weight => 5,
65
+ :stroke_opacity => 0.5,
66
+ :fill_color => '#b00b1e',
67
+ :polygon_options => {
68
+ :mouse_out_tolerence => 5
69
+ }
70
+ )
71
+ )
72
+ end
73
+
74
+ def test_to_g_polyline
75
+ assert_equal(
76
+ "new google.maps.Polyline([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)], null, null, null, null)",
77
+ @polygon.to_g_polyline
78
+ )
79
+
80
+ assert_equal(
81
+ "new GPolyline([new GLatLng(0.0, 0.0), new GLatLng(1.0, 1.0), new GLatLng(2.5, 2.5), new GLatLng(5.0, 5.0), new GLatLng(0.0, 0.0)], null, null, null, null)",
82
+ @polygon.to_g_polyline({}, :short_class => true)
83
+ )
84
+
85
+ assert_equal(
86
+ "new google.maps.Polyline([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)], '#b00b1e', 5, 0.5, {\"mouseOutTolerence\":5})",
87
+ @polygon.to_g_polyline(
88
+ :color => '#b00b1e',
89
+ :weight => 5,
90
+ :opacity => 0.5,
91
+ :polyline_options => {
92
+ :mouse_out_tolerence => 5
93
+ }
94
+ )
95
+ )
96
+ end
97
+
98
+ def test_to_g_marker_long
99
+ marker = @point.to_g_marker
100
+
101
+ lat, lng, json = if marker =~ /^new\s+
102
+ google\.maps\.Marker\(
103
+ new\s+google\.maps\.LatLng\(
104
+ (\d+\.\d+)\s*,\s*
105
+ (\d+\.\d+)
106
+ \),\s*
107
+ ((\{\}))
108
+ \)
109
+ /x
110
+ [ $1, $2, $3 ]
111
+ end
112
+
113
+ assert_in_delta(lng.to_f, 10.00, 0.000001)
114
+ assert_in_delta(lat.to_f, 10.01, 0.000001)
115
+ assert_equal(
116
+ {},
117
+ JSON.load(json)
118
+ )
119
+ end
120
+
121
+ def test_to_g_marker_short_class
122
+ marker = @point.to_g_marker({}, :short_class => true)
123
+
124
+ lat, lng, json = if marker =~ /^new\s+
125
+ GMarker\(
126
+ new\s+GLatLng\(
127
+ (\d+\.\d+)\s*,\s*
128
+ (\d+\.\d+)
129
+ \),\s*
130
+ (\{\})
131
+ \)
132
+ /x
133
+ [ $1, $2, $3 ]
134
+ end
135
+
136
+ assert_in_delta(lng.to_f, 10.00, 0.000001)
137
+ assert_in_delta(lat.to_f, 10.01, 0.000001)
138
+ assert_equal(
139
+ {},
140
+ JSON.load(json)
141
+ )
142
+ end
143
+
144
+
145
+ def test_to_g_marker_with_options
146
+ marker = @point.to_g_marker(
147
+ :bounce_gravity => 1,
148
+ :bouncy => true
149
+ )
150
+
151
+ lat, lng, json = if marker =~ /^new\s+
152
+ google\.maps\.Marker\(
153
+ new\s+google\.maps\.LatLng\(
154
+ (\d+\.\d+)\s*,\s*
155
+ (\d+\.\d+)
156
+ \),\s*
157
+ (\{[^}]+\})
158
+ \)
159
+ /x
160
+ [ $1, $2, $3 ]
161
+ end
162
+
163
+ assert_in_delta(lng.to_f, 10.00, 0.000001)
164
+ assert_in_delta(lat.to_f, 10.01, 0.000001)
165
+ assert_equal(
166
+ { "bounceGravity" => 1, "bouncy" => true },
167
+ JSON.load(json)
168
+ )
169
+ end
170
+
171
+ def test_to_g_json_point
172
+ assert_equal(
173
+ { :coordinates => [ 10.0, 10.01, 0 ] },
174
+ @point.to_g_json_point
175
+ )
176
+ end
177
+
178
+ def test_to_g_lat_lon_box
179
+ assert_equal(
180
+ { :east => 5.0, :west => 0.0, :north => 5.0, :south => 0.0},
181
+ @polygon.to_g_lat_lon_box
182
+ )
183
+ end
184
+ end
185
+ end