1 $LOAD_PATH.unshift File.join('..', 'lib')
2 require 'geo_kit/mappable'
5 class LatLngTest < Test::Unit::TestCase #:nodoc: all
8 @loc_a = GeoKit::LatLng.new(32.918593,-96.958444)
9 @loc_e = GeoKit::LatLng.new(32.969527,-96.990159)
10 @point = GeoKit::LatLng.new(@loc_a.lat, @loc_a.lng)
13 def test_distance_between_same_using_defaults
14 assert_equal 0, GeoKit::LatLng.distance_between(@loc_a, @loc_a)
15 assert_equal 0, @loc_a.distance_to(@loc_a)
18 def test_distance_between_same_with_miles_and_flat
19 assert_equal 0, GeoKit::LatLng.distance_between(@loc_a, @loc_a, :units => :miles, :formula => :flat)
20 assert_equal 0, @loc_a.distance_to(@loc_a, :units => :miles, :formula => :flat)
23 def test_distance_between_same_with_kms_and_flat
24 assert_equal 0, GeoKit::LatLng.distance_between(@loc_a, @loc_a, :units => :kms, :formula => :flat)
25 assert_equal 0, @loc_a.distance_to(@loc_a, :units => :kms, :formula => :flat)
28 def test_distance_between_same_with_miles_and_sphere
29 assert_equal 0, GeoKit::LatLng.distance_between(@loc_a, @loc_a, :units => :miles, :formula => :sphere)
30 assert_equal 0, @loc_a.distance_to(@loc_a, :units => :miles, :formula => :sphere)
33 def test_distance_between_same_with_kms_and_sphere
34 assert_equal 0, GeoKit::LatLng.distance_between(@loc_a, @loc_a, :units => :kms, :formula => :sphere)
35 assert_equal 0, @loc_a.distance_to(@loc_a, :units => :kms, :formula => :sphere)
38 def test_distance_between_diff_using_defaults
39 assert_in_delta 3.97, GeoKit::LatLng.distance_between(@loc_a, @loc_e), 0.01
40 assert_in_delta 3.97, @loc_a.distance_to(@loc_e), 0.01
43 def test_distance_between_diff_with_miles_and_flat
44 assert_in_delta 3.97, GeoKit::LatLng.distance_between(@loc_a, @loc_e, :units => :miles, :formula => :flat), 0.2
45 assert_in_delta 3.97, @loc_a.distance_to(@loc_e, :units => :miles, :formula => :flat), 0.2
48 def test_distance_between_diff_with_kms_and_flat
49 assert_in_delta 6.39, GeoKit::LatLng.distance_between(@loc_a, @loc_e, :units => :kms, :formula => :flat), 0.4
50 assert_in_delta 6.39, @loc_a.distance_to(@loc_e, :units => :kms, :formula => :flat), 0.4
53 def test_distance_between_diff_with_miles_and_sphere
54 assert_in_delta 3.97, GeoKit::LatLng.distance_between(@loc_a, @loc_e, :units => :miles, :formula => :sphere), 0.01
55 assert_in_delta 3.97, @loc_a.distance_to(@loc_e, :units => :miles, :formula => :sphere), 0.01
58 def test_distance_between_diff_with_kms_and_sphere
59 assert_in_delta 6.39, GeoKit::LatLng.distance_between(@loc_a, @loc_e, :units => :kms, :formula => :sphere), 0.01
60 assert_in_delta 6.39, @loc_a.distance_to(@loc_e, :units => :kms, :formula => :sphere), 0.01
63 def test_manually_mixed_in
64 assert_equal 0, GeoKit::LatLng.distance_between(@point, @point)
65 assert_equal 0, @point.distance_to(@point)
66 assert_equal 0, @point.distance_to(@loc_a)
67 assert_in_delta 3.97, @point.distance_to(@loc_e, :units => :miles, :formula => :flat), 0.2
68 assert_in_delta 6.39, @point.distance_to(@loc_e, :units => :kms, :formula => :flat), 0.4
71 def test_heading_between
72 assert_in_delta 332, GeoKit::LatLng.heading_between(@loc_a,@loc_e), 0.5
76 assert_in_delta 332, @loc_a.heading_to(@loc_e), 0.5
79 def test_class_endpoint
80 endpoint=GeoKit::LatLng.endpoint(@loc_a, 332, 3.97)
81 assert_in_delta @loc_e.lat, endpoint.lat, 0.0005
82 assert_in_delta @loc_e.lng, endpoint.lng, 0.0005
85 def test_instance_endpoint
86 endpoint=@loc_a.endpoint(332, 3.97)
87 assert_in_delta @loc_e.lat, endpoint.lat, 0.0005
88 assert_in_delta @loc_e.lng, endpoint.lng, 0.0005
92 midpoint=@loc_a.midpoint_to(@loc_e)
93 assert_in_delta 32.944061, midpoint.lat, 0.0005
94 assert_in_delta -96.974296, midpoint.lng, 0.0005
100 res=GeoKit::LatLng.normalize(lat,lng)
101 assert_equal res,GeoKit::LatLng.new(lat,lng)
102 res=GeoKit::LatLng.normalize("#{lat}, #{lng}")
103 assert_equal res,GeoKit::LatLng.new(lat,lng)
104 res=GeoKit::LatLng.normalize("#{lat} #{lng}")
105 assert_equal res,GeoKit::LatLng.new(lat,lng)
106 res=GeoKit::LatLng.normalize("#{lat.to_i} #{lng.to_i}")
107 assert_equal res,GeoKit::LatLng.new(lat.to_i,lng.to_i)
108 res=GeoKit::LatLng.normalize([lat,lng])
109 assert_equal res,GeoKit::LatLng.new(lat,lng)