public
Description: Official Geokit Gem. Geokit gem provides geocoding and distance/heading calculations. Pair with the geokit-rails plugin for full-fledged location-based app functionality.
Homepage: http://geokit.rubyforge.org
Clone URL: git://github.com/andre/geokit-gem.git
Andre Lewis (author)
Mon Jun 15 19:11:01 -0700 2009
commit  94b62018a7f4f4fd25e6435b2bda19ed1c17d6e8
tree    539bb6ead755b31797b043620f3cd1319859c818
parent  1475432444b3378ed542319617020cd048f0aa52
geokit-gem / test / test_bounds.rb
100644 97 lines (78 sloc) 3.083 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'test/unit'
require 'lib/geokit'
 
class BoundsTest < Test::Unit::TestCase #:nodoc: all
  
  def setup
    # This is the area in Texas
    @sw = Geokit::LatLng.new(32.91663,-96.982841)
    @ne = Geokit::LatLng.new(32.96302,-96.919495)
    @bounds=Geokit::Bounds.new(@sw,@ne)
    @loc_a=Geokit::LatLng.new(32.918593,-96.958444) # inside bounds
    @loc_b=Geokit::LatLng.new(32.914144,-96.958444) # outside bouds
    
    # this is a cross-meridan area
    @cross_meridian=Geokit::Bounds.normalize([30,170],[40,-170])
    @inside_cm=Geokit::LatLng.new(35,175)
    @inside_cm_2=Geokit::LatLng.new(35,-175)
    @east_of_cm=Geokit::LatLng.new(35,-165)
    @west_of_cm=Geokit::LatLng.new(35,165)
 
  end
 
  def test_equality
    assert_equal Geokit::Bounds.new(@sw,@ne), Geokit::Bounds.new(@sw,@ne)
  end
  
  def test_normalize
    res=Geokit::Bounds.normalize(@sw,@ne)
    assert_equal res,Geokit::Bounds.new(@sw,@ne)
    res=Geokit::Bounds.normalize([@sw,@ne])
    assert_equal res,Geokit::Bounds.new(@sw,@ne)
    res=Geokit::Bounds.normalize([@sw.lat,@sw.lng],[@ne.lat,@ne.lng])
    assert_equal res,Geokit::Bounds.new(@sw,@ne)
    res=Geokit::Bounds.normalize([[@sw.lat,@sw.lng],[@ne.lat,@ne.lng]])
    assert_equal res,Geokit::Bounds.new(@sw,@ne)
  end
  
  def test_point_inside_bounds
    assert @bounds.contains?(@loc_a)
  end
 
  def test_point_outside_bounds
    assert !@bounds.contains?(@loc_b)
  end
 
  def test_point_inside_bounds_cross_meridian
    assert @cross_meridian.contains?(@inside_cm)
    assert @cross_meridian.contains?(@inside_cm_2)
  end
 
  def test_point_outside_bounds_cross_meridian
    assert !@cross_meridian.contains?(@east_of_cm)
    assert !@cross_meridian.contains?(@west_of_cm)
  end
  
  def test_center
    assert_in_delta 32.939828,@bounds.center.lat,0.00005
    assert_in_delta(-96.9511763,@bounds.center.lng,0.00005)
  end
 
  def test_center_cross_meridian
    assert_in_delta 35.41160, @cross_meridian.center.lat,0.00005
    assert_in_delta 179.38112, @cross_meridian.center.lng,0.00005
  end
  
  def test_creation_from_circle
    bounds=Geokit::Bounds.from_point_and_radius([32.939829, -96.951176],2.5)
    inside=Geokit::LatLng.new 32.9695270000,-96.9901590000
    outside=Geokit::LatLng.new 32.8951550000,-96.9584440000
    assert bounds.contains?(inside)
    assert !bounds.contains?(outside)
  end
  
  def test_bounds_to_span
    sw = Geokit::LatLng.new(32, -96)
    ne = Geokit::LatLng.new(40, -70)
    bounds = Geokit::Bounds.new(sw, ne)
    
    assert_equal Geokit::LatLng.new(8, 26), bounds.to_span
  end
  
  def test_bounds_to_span_with_bounds_crossing_prime_meridian
    sw = Geokit::LatLng.new(20, -70)
    ne = Geokit::LatLng.new(40, 100)
    bounds = Geokit::Bounds.new(sw, ne)
    
    assert_equal Geokit::LatLng.new(20, 170), bounds.to_span
  end
  
  def test_bounds_to_span_with_bounds_crossing_dateline
    sw = Geokit::LatLng.new(20, 100)
    ne = Geokit::LatLng.new(40, -70)
    bounds = Geokit::Bounds.new(sw, ne)
    
    assert_equal Geokit::LatLng.new(20, 190), bounds.to_span
  end
end