/test/test_latlng.rb

https://github.com/marionm/geokit-gem · Ruby · 215 lines · 181 code · 34 blank · 0 comment · 3 complexity · e73b4d76dfe111da9370d0a5b650fb2b MD5 · raw file

  1. require 'test/unit'
  2. require 'rubygems'
  3. require 'lib/geokit'
  4. require 'mocha'
  5. class LatLngTest < Test::Unit::TestCase #:nodoc: all
  6. def setup
  7. @loc_a = Geokit::LatLng.new(32.918593,-96.958444)
  8. @loc_e = Geokit::LatLng.new(32.969527,-96.990159)
  9. @point = Geokit::LatLng.new(@loc_a.lat, @loc_a.lng)
  10. end
  11. def valid_reverse_geocoding_result
  12. location = Geokit::GeoLoc.new({
  13. :city => "Essen",
  14. :country_code => "DE",
  15. :lat => 51.4578329,
  16. :lng => 7.0166848,
  17. :provider => "google",
  18. :state => "Nordrhein-Westfalen",
  19. :street_address => "Porscheplatz 1",
  20. :zip => "45127"
  21. })
  22. location.full_address = "Porscheplatz 1, 45127 Essen, Deutschland"
  23. location.precision = 'address'
  24. location.provider = 'google'
  25. location.success = true
  26. location
  27. end
  28. def test_distance_between_same_using_defaults
  29. assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a)
  30. assert_equal 0, @loc_a.distance_to(@loc_a)
  31. end
  32. def test_distance_between_same_with_miles_and_flat
  33. assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :miles, :formula => :flat)
  34. assert_equal 0, @loc_a.distance_to(@loc_a, :units => :miles, :formula => :flat)
  35. end
  36. def test_distance_between_same_with_kms_and_flat
  37. assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :kms, :formula => :flat)
  38. assert_equal 0, @loc_a.distance_to(@loc_a, :units => :kms, :formula => :flat)
  39. end
  40. def test_distance_between_same_with_nms_and_flat
  41. assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :nms, :formula => :flat)
  42. assert_equal 0, @loc_a.distance_to(@loc_a, :units => :nms, :formula => :flat)
  43. end
  44. def test_distance_between_same_with_miles_and_sphere
  45. assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :miles, :formula => :sphere)
  46. assert_equal 0, @loc_a.distance_to(@loc_a, :units => :miles, :formula => :sphere)
  47. end
  48. def test_distance_between_same_with_kms_and_sphere
  49. assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :kms, :formula => :sphere)
  50. assert_equal 0, @loc_a.distance_to(@loc_a, :units => :kms, :formula => :sphere)
  51. end
  52. def test_distance_between_same_with_nms_and_sphere
  53. assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :nms, :formula => :sphere)
  54. assert_equal 0, @loc_a.distance_to(@loc_a, :units => :nms, :formula => :sphere)
  55. end
  56. def test_distance_between_diff_using_defaults
  57. assert_in_delta 3.97, Geokit::LatLng.distance_between(@loc_a, @loc_e), 0.01
  58. assert_in_delta 3.97, @loc_a.distance_to(@loc_e), 0.01
  59. end
  60. def test_distance_between_diff_with_miles_and_flat
  61. assert_in_delta 3.97, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :miles, :formula => :flat), 0.2
  62. assert_in_delta 3.97, @loc_a.distance_to(@loc_e, :units => :miles, :formula => :flat), 0.2
  63. end
  64. def test_distance_between_diff_with_kms_and_flat
  65. assert_in_delta 6.39, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :kms, :formula => :flat), 0.4
  66. assert_in_delta 6.39, @loc_a.distance_to(@loc_e, :units => :kms, :formula => :flat), 0.4
  67. end
  68. def test_distance_between_diff_with_nms_and_flat
  69. assert_in_delta 3.334, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :nms, :formula => :flat), 0.4
  70. assert_in_delta 3.334, @loc_a.distance_to(@loc_e, :units => :nms, :formula => :flat), 0.4
  71. end
  72. def test_distance_between_diff_with_miles_and_sphere
  73. assert_in_delta 3.97, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :miles, :formula => :sphere), 0.01
  74. assert_in_delta 3.97, @loc_a.distance_to(@loc_e, :units => :miles, :formula => :sphere), 0.01
  75. end
  76. def test_distance_between_diff_with_kms_and_sphere
  77. assert_in_delta 6.39, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :kms, :formula => :sphere), 0.01
  78. assert_in_delta 6.39, @loc_a.distance_to(@loc_e, :units => :kms, :formula => :sphere), 0.01
  79. end
  80. def test_distance_between_diff_with_nms_and_sphere
  81. assert_in_delta 3.454, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :nms, :formula => :sphere), 0.01
  82. assert_in_delta 3.454, @loc_a.distance_to(@loc_e, :units => :nms, :formula => :sphere), 0.01
  83. end
  84. def test_manually_mixed_in
  85. assert_equal 0, Geokit::LatLng.distance_between(@point, @point)
  86. assert_equal 0, @point.distance_to(@point)
  87. assert_equal 0, @point.distance_to(@loc_a)
  88. assert_in_delta 3.97, @point.distance_to(@loc_e, :units => :miles, :formula => :flat), 0.2
  89. assert_in_delta 6.39, @point.distance_to(@loc_e, :units => :kms, :formula => :flat), 0.4
  90. assert_in_delta 3.334, @point.distance_to(@loc_e, :units => :nms, :formula => :flat), 0.4
  91. end
  92. def test_heading_between
  93. assert_in_delta 332, Geokit::LatLng.heading_between(@loc_a,@loc_e), 0.5
  94. end
  95. def test_headings_between
  96. headings = Geokit::LatLng.headings_between(@loc_a, [@loc_e])
  97. assert_in_delta 332, headings[@loc_e], 0.5
  98. end
  99. def test_heading_to
  100. assert_in_delta 332, @loc_a.heading_to(@loc_e), 0.5
  101. end
  102. def test_class_endpoint
  103. endpoint=Geokit::LatLng.endpoint(@loc_a, 332, 3.97)
  104. assert_in_delta @loc_e.lat, endpoint.lat, 0.0005
  105. assert_in_delta @loc_e.lng, endpoint.lng, 0.0005
  106. end
  107. def test_instance_endpoint
  108. endpoint=@loc_a.endpoint(332, 3.97)
  109. assert_in_delta @loc_e.lat, endpoint.lat, 0.0005
  110. assert_in_delta @loc_e.lng, endpoint.lng, 0.0005
  111. end
  112. def test_midpoint
  113. midpoint=@loc_a.midpoint_to(@loc_e)
  114. assert_in_delta 32.944061, midpoint.lat, 0.0005
  115. assert_in_delta(-96.974296, midpoint.lng, 0.0005)
  116. end
  117. def test_normalize
  118. lat=37.7690
  119. lng=-122.443
  120. res=Geokit::LatLng.normalize(lat,lng)
  121. assert_equal res,Geokit::LatLng.new(lat,lng)
  122. res=Geokit::LatLng.normalize("#{lat}, #{lng}")
  123. assert_equal res,Geokit::LatLng.new(lat,lng)
  124. res=Geokit::LatLng.normalize("#{lat} #{lng}")
  125. assert_equal res,Geokit::LatLng.new(lat,lng)
  126. res=Geokit::LatLng.normalize("#{lat.to_i} #{lng.to_i}")
  127. assert_equal res,Geokit::LatLng.new(lat.to_i,lng.to_i)
  128. res=Geokit::LatLng.normalize([lat,lng])
  129. assert_equal res,Geokit::LatLng.new(lat,lng)
  130. end
  131. def test_hash
  132. lat=37.7690
  133. lng=-122.443
  134. first = Geokit::LatLng.new(lat,lng)
  135. second = Geokit::LatLng.new(lat,lng)
  136. assert_equal first.hash, second.hash
  137. end
  138. def test_eql?
  139. lat=37.7690
  140. lng=-122.443
  141. first = Geokit::LatLng.new(lat,lng)
  142. second = Geokit::LatLng.new(lat,lng)
  143. assert first.eql?(second)
  144. assert second.eql?(first)
  145. end
  146. def test_reverse_geocode
  147. point = Geokit::LatLng.new(51.4578329, 7.0166848)
  148. Geokit::Geocoders::MultiGeocoder.expects(:reverse_geocode).with(point).returns(valid_reverse_geocoding_result)
  149. res = point.reverse_geocode
  150. assert_equal "Nordrhein-Westfalen", res.state
  151. assert_equal "Essen", res.city
  152. assert_equal "45127", res.zip
  153. assert_equal "51.4578329,7.0166848", res.ll # slightly dif from yahoo
  154. assert res.is_us? == false
  155. assert_equal "Porscheplatz 1, 45127 Essen, Deutschland", res.full_address #slightly different from yahoo
  156. end
  157. def test_reverse_geocoding_using_specific_geocoder
  158. point = Geokit::LatLng.new(51.4578329, 7.0166848)
  159. Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with(point).returns(valid_reverse_geocoding_result)
  160. res = point.reverse_geocode(:using => Geokit::Geocoders::GoogleGeocoder)
  161. assert_equal "Nordrhein-Westfalen", res.state
  162. assert_equal "Essen", res.city
  163. assert_equal "45127", res.zip
  164. assert_equal "51.4578329,7.0166848", res.ll # slightly dif from yahoo
  165. assert res.is_us? == false
  166. assert_equal "Porscheplatz 1, 45127 Essen, Deutschland", res.full_address #slightly different from yahoo
  167. assert_equal "google", res.provider
  168. end
  169. def test_reverse_geocoding_using_specific_geocoder_short_syntax
  170. point = Geokit::LatLng.new(51.4578329, 7.0166848)
  171. Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with(point).returns(valid_reverse_geocoding_result)
  172. res = point.reverse_geocode(:using => :google)
  173. assert_equal "Nordrhein-Westfalen", res.state
  174. assert_equal "Essen", res.city
  175. assert_equal "45127", res.zip
  176. assert_equal "51.4578329,7.0166848", res.ll # slightly dif from yahoo
  177. assert res.is_us? == false
  178. assert_equal "Porscheplatz 1, 45127 Essen, Deutschland", res.full_address #slightly different from yahoo
  179. assert_equal "google", res.provider
  180. end
  181. end