abuiles-geokit 1.6.1

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.
Files changed (45) hide show
  1. data/.bundle/config +2 -0
  2. data/.gitignore +4 -0
  3. data/Gemfile +3 -0
  4. data/Gemfile.lock +19 -0
  5. data/History.txt +77 -0
  6. data/Manifest.txt +21 -0
  7. data/README.markdown +273 -0
  8. data/Rakefile +13 -0
  9. data/geokit.gemspec +24 -0
  10. data/lib/geokit.rb +55 -0
  11. data/lib/geokit/bounds.rb +95 -0
  12. data/lib/geokit/geo_loc.rb +115 -0
  13. data/lib/geokit/geocoders.rb +68 -0
  14. data/lib/geokit/geocoders/ca_geocoder.rb +54 -0
  15. data/lib/geokit/geocoders/geo_plugin_geocoder.rb +30 -0
  16. data/lib/geokit/geocoders/geocode_error.rb +7 -0
  17. data/lib/geokit/geocoders/geocoder.rb +75 -0
  18. data/lib/geokit/geocoders/geonames_geocoder.rb +53 -0
  19. data/lib/geokit/geocoders/google_geocoder.rb +145 -0
  20. data/lib/geokit/geocoders/google_premier_geocoder.rb +147 -0
  21. data/lib/geokit/geocoders/ip_geocoder.rb +76 -0
  22. data/lib/geokit/geocoders/multi_geocoder.rb +60 -0
  23. data/lib/geokit/geocoders/us_geocoder.rb +50 -0
  24. data/lib/geokit/geocoders/yahoo_geocoder.rb +49 -0
  25. data/lib/geokit/inflector.rb +39 -0
  26. data/lib/geokit/lat_lng.rb +112 -0
  27. data/lib/geokit/mappable.rb +210 -0
  28. data/lib/geokit/too_many_queries_error.rb +4 -0
  29. data/lib/geokit/version.rb +3 -0
  30. data/test/test_base_geocoder.rb +58 -0
  31. data/test/test_bounds.rb +97 -0
  32. data/test/test_ca_geocoder.rb +39 -0
  33. data/test/test_geoloc.rb +72 -0
  34. data/test/test_geoplugin_geocoder.rb +58 -0
  35. data/test/test_google_geocoder.rb +225 -0
  36. data/test/test_google_premier_geocoder.rb +88 -0
  37. data/test/test_google_reverse_geocoder.rb +47 -0
  38. data/test/test_inflector.rb +24 -0
  39. data/test/test_ipgeocoder.rb +109 -0
  40. data/test/test_latlng.rb +209 -0
  41. data/test/test_multi_geocoder.rb +91 -0
  42. data/test/test_multi_ip_geocoder.rb +36 -0
  43. data/test/test_us_geocoder.rb +54 -0
  44. data/test/test_yahoo_geocoder.rb +103 -0
  45. metadata +141 -0
@@ -0,0 +1,88 @@
1
+ Geokit::Geocoders::google = '0xdeadbeef'
2
+ Geokit::Geocoders::google_client = 'gme-acme'
3
+ Geokit::Geocoders::google_channel = 'marketing'
4
+
5
+ class GooglePremierGeocoderTest < BaseGeocoderTest #:nodoc: all
6
+
7
+ GOOGLE_PREMIER_FULL=<<-EOF
8
+ {
9
+ "status": "OK",
10
+ "results": [ {
11
+ "types": [ "street_address" ],
12
+ "formatted_address": "65 Upper Queen St, Auckland 1010, New Zealand",
13
+ "address_components": [ {
14
+ "long_name": "65",
15
+ "short_name": "65",
16
+ "types": [ "street_number" ]
17
+ }, {
18
+ "long_name": "Upper Queen St",
19
+ "short_name": "Upper Queen St",
20
+ "types": [ "route" ]
21
+ }, {
22
+ "long_name": "Auckland",
23
+ "short_name": "Auckland",
24
+ "types": [ "locality", "political" ]
25
+ }, {
26
+ "long_name": "Auckland",
27
+ "short_name": "Auckland",
28
+ "types": [ "administrative_area_level_3", "political" ]
29
+ }, {
30
+ "long_name": "Auckland",
31
+ "short_name": "Auckland",
32
+ "types": [ "administrative_area_level_1", "political" ]
33
+ }, {
34
+ "long_name": "New Zealand",
35
+ "short_name": "NZ",
36
+ "types": [ "country", "political" ]
37
+ }, {
38
+ "long_name": "1010",
39
+ "short_name": "1010",
40
+ "types": [ "postal_code" ]
41
+ } ],
42
+ "geometry": {
43
+ "location": {
44
+ "lat": -36.8624475,
45
+ "lng": 174.7589730
46
+ },
47
+ "location_type": "RANGE_INTERPOLATED",
48
+ "viewport": {
49
+ "southwest": {
50
+ "lat": -36.8656001,
51
+ "lng": 174.7558353
52
+ },
53
+ "northeast": {
54
+ "lat": -36.8593048,
55
+ "lng": 174.7621306
56
+ }
57
+ },
58
+ "bounds": {
59
+ "southwest": {
60
+ "lat": -36.8624574,
61
+ "lng": 174.7589730
62
+ },
63
+ "northeast": {
64
+ "lat": -36.8624475,
65
+ "lng": 174.7589929
66
+ }
67
+ }
68
+ }
69
+ } ]
70
+ }
71
+ EOF
72
+
73
+ def setup
74
+ @address = "65 Upper Queen Street, Auckland"
75
+ end
76
+
77
+ def test_google_full_address
78
+ response = MockSuccess.new
79
+ response.expects(:body).returns(GOOGLE_PREMIER_FULL)
80
+ url = "http://maps.googleapis.com/maps/api/geocode/json?address=65+Upper+Queen+Street%2C+Auckland&channel=marketing&client=gme-acme&sensor=false&signature=AO3Ppgjdr7SoQBUcwSdvXFjwN5M="
81
+ Geokit::Geocoders::GooglePremierGeocoder.expects(:call_geocoder_service).with(url).returns(response)
82
+ res = Geokit::Geocoders::GooglePremierGeocoder.geocode(@address)
83
+
84
+ assert_equal "Auckland", res.city
85
+ assert_equal 174.7589730, res.lng
86
+ end
87
+
88
+ end
@@ -0,0 +1,47 @@
1
+ Geokit::Geocoders::google = 'Google'
2
+
3
+ class GoogleReverseGeocoderTest < BaseGeocoderTest #:nodoc: all
4
+
5
+ GOOGLE_REVERSE_FULL=<<-EOF.strip
6
+ <?xml version="1.0" encoding="UTF-8" ?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>51.457833,7.016685</name><Status><code>200</code><request>geocode</request></Status><Placemark id="p1"><address>Porscheplatz 1, 45127 Essen, Deutschland</address><AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName><DependentLocality><DependentLocalityName>Stadtkern</DependentLocalityName><Thoroughfare><ThoroughfareName>Porscheplatz 1</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>45127</PostalCodeNumber></PostalCode></DependentLocality></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.4609805" south="51.4546853" east="7.0198324" west="7.0135372" /></ExtendedData><Point><coordinates>7.0166848,51.4578329,0</coordinates></Point></Placemark><Placemark id="p2"><address>Stadtkern, Essen, Deutschland</address><AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName><DependentLocality><DependentLocalityName>Stadtkern</DependentLocalityName></DependentLocality></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.4630710" south="51.4506320" east="7.0193200" west="7.0026170" /></ExtendedData><Point><coordinates>7.0124328,51.4568201,0</coordinates></Point></Placemark><Placemark id="p3"><address>45127 Essen, Deutschland</address><AddressDetails Accuracy="5" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName><PostalCode><PostalCodeNumber>45127</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.4637808" south="51.4503125" east="7.0231080" west="6.9965454" /></ExtendedData><Point><coordinates>7.0104543,51.4556194,0</coordinates></Point></Placemark><Placemark id="p4"><address>Essen, Deutschland</address><AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName><Locality><LocalityName>Essen</LocalityName></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.5342070" south="51.3475730" east="7.1376530" west="6.8943470" /></ExtendedData><Point><coordinates>7.0147614,51.4580686,0</coordinates></Point></Placemark><Placemark id="p5"><address>Essen, Deutschland</address><AddressDetails Accuracy="3" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Essen</SubAdministrativeAreaName></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.5342070" south="51.3475730" east="7.1376530" west="6.8943470" /></ExtendedData><Point><coordinates>7.0461136,51.4508381,0</coordinates></Point></Placemark><Placemark id="p6"><address>Nordrhein-Westfalen, Deutschland</address><AddressDetails Accuracy="2" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName><AdministrativeArea><AdministrativeAreaName>Nordrhein-Westfalen</AdministrativeAreaName></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="52.5314170" south="50.3225720" east="9.4615950" west="5.8663566" /></ExtendedData><Point><coordinates>7.6615938,51.4332367,0</coordinates></Point></Placemark><Placemark id="p7"><address>Deutschland</address><AddressDetails Accuracy="1" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>DE</CountryNameCode><CountryName>Deutschland</CountryName></Country></AddressDetails><ExtendedData><LatLonBox north="55.0568230" south="47.2701270" east="15.0418536" west="5.8663566" /></ExtendedData><Point><coordinates>10.4515260,51.1656910,0</coordinates></Point></Placemark></Response></kml>
7
+ EOF
8
+
9
+
10
+ def test_google_full_address
11
+ response = MockSuccess.new
12
+ response.expects(:body).returns(GOOGLE_REVERSE_FULL)
13
+
14
+
15
+ # http://maps.google.com/maps/geo?output=xml&oe=utf-8&ll=51.4578329,7.0166848&key=asdad
16
+
17
+ # #<Geokit::GeoLoc:0x10ec7ec
18
+ # @city="Essen",
19
+ # @country_code="DE",
20
+ # @full_address="Porscheplatz 1, 45127 Essen, Germany",
21
+ # @lat=51.4578329,
22
+ # @lng=7.0166848,
23
+ # @precision="address",
24
+ # @provider="google",
25
+ # @state="Nordrhein-Westfalen",
26
+ # @street_address="Porscheplatz 1",
27
+ # @success=true,
28
+ # @zip="45127">
29
+ #
30
+
31
+
32
+ @latlng = "51.4578329,7.0166848"
33
+
34
+ url = "http://maps.google.com/maps/geo?key=Google&ll=#{Geokit::Inflector.url_escape(@latlng)}&oe=utf-8&output=xml"
35
+ Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
36
+ res=Geokit::Geocoders::GoogleGeocoder.reverse_geocode(@latlng)
37
+ assert_equal "Nordrhein-Westfalen", res.state
38
+ assert_equal "Essen", res.city
39
+ assert_equal "45127", res.zip
40
+ assert_equal "51.4578329,7.0166848", res.ll # slightly dif from yahoo
41
+ assert res.is_us? == false
42
+ assert_equal "Porscheplatz 1, 45127 Essen, Deutschland", res.full_address #slightly different from yahoo
43
+ assert_equal "google", res.provider
44
+ end
45
+
46
+
47
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test/unit'
4
+ require 'geokit'
5
+
6
+ class InflectorTest < Test::Unit::TestCase #:nodoc: all
7
+
8
+ def test_titleize
9
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('Sugar Grove')
10
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('Sugar grove')
11
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('sugar Grove')
12
+ assert_equal 'Sugar Grove', Geokit::Inflector.titleize('sugar grove')
13
+ end
14
+
15
+ def test_titleize_with_unicode
16
+ assert_equal 'Borås', Geokit::Inflector.titleize('Borås')
17
+ assert_equal 'Borås', Geokit::Inflector.titleize('borås')
18
+ assert_equal 'Borås (Abc)', Geokit::Inflector.titleize('Borås (Abc)')
19
+ assert_equal 'Borås (Abc)', Geokit::Inflector.titleize('Borås (abc)')
20
+ assert_equal 'Borås (Abc)', Geokit::Inflector.titleize('borås (Abc)')
21
+ assert_equal 'Borås (Abc)', Geokit::Inflector.titleize('borås (abc)')
22
+ end
23
+
24
+ end
@@ -0,0 +1,109 @@
1
+ # encoding: utf-8
2
+
3
+ class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
4
+
5
+ IP_FAILURE=<<-EOF
6
+ Country: SWITZERLAND (CH)
7
+ City: (Unknown City)
8
+ Latitude:
9
+ Longitude:
10
+ EOF
11
+
12
+ IP_SUCCESS=<<-EOF
13
+ Country: UNITED STATES (US)
14
+ City: Sugar Grove, IL
15
+ Latitude: 41.7696
16
+ Longitude: -88.4588
17
+ EOF
18
+
19
+ IP_UNICODED=<<-EOF
20
+ Country: SWEDEN (SE)
21
+ City: Borås
22
+ Latitude: 57.7167
23
+ Longitude: 12.9167
24
+ EOF
25
+
26
+ PRIVATE_IPS_TO_TEST = [
27
+ '10.10.10.10',
28
+ '172.16.1.3',
29
+ '172.22.3.42',
30
+ '172.30.254.164',
31
+ '192.168.1.1',
32
+ '0.0.0.0',
33
+ '127.0.0.1',
34
+ '240.3.4.5',
35
+ '225.1.6.55'
36
+ ].freeze
37
+
38
+ def setup
39
+ super
40
+ @success.provider = "hostip"
41
+ end
42
+
43
+ def test_successful_lookup
44
+ success = MockSuccess.new
45
+ success.expects(:body).returns(IP_SUCCESS)
46
+ url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
47
+ Geokit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
48
+ location = Geokit::Geocoders::IpGeocoder.geocode('12.215.42.19')
49
+ assert_not_nil location
50
+ assert_equal 41.7696, location.lat
51
+ assert_equal(-88.4588, location.lng)
52
+ assert_equal "Sugar Grove", location.city
53
+ assert_equal "IL", location.state
54
+ assert_equal "US", location.country_code
55
+ assert_equal "hostip", location.provider
56
+ assert location.success?
57
+ end
58
+
59
+ def test_unicoded_lookup
60
+ success = MockSuccess.new
61
+ success.expects(:body).returns(IP_UNICODED)
62
+ url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
63
+ Geokit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
64
+ location = Geokit::Geocoders::IpGeocoder.geocode('12.215.42.19')
65
+ assert_not_nil location
66
+ assert_equal 57.7167, location.lat
67
+ assert_equal 12.9167, location.lng
68
+ assert_equal "Bor\303\245s", location.city
69
+ assert_nil location.state
70
+ assert_equal "SE", location.country_code
71
+ assert_equal "hostip", location.provider
72
+ assert location.success?
73
+ end
74
+
75
+ def test_failed_lookup
76
+ failure = MockSuccess.new
77
+ failure.expects(:body).returns(IP_FAILURE)
78
+ url = 'http://api.hostip.info/get_html.php?ip=128.178.0.0&position=true'
79
+ Geokit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
80
+ location = Geokit::Geocoders::IpGeocoder.geocode("128.178.0.0")
81
+ assert_not_nil location
82
+ assert !location.success?
83
+ end
84
+
85
+ def test_private_ips
86
+ Geokit::Geocoders::IpGeocoder.expects(:call_geocoder_service).never
87
+ PRIVATE_IPS_TO_TEST.each do |ip|
88
+ location = Geokit::Geocoders::IpGeocoder.geocode(ip)
89
+ assert_not_nil location
90
+ assert !location.success?
91
+ end
92
+ end
93
+
94
+ def test_invalid_ip
95
+ Geokit::Geocoders::IpGeocoder.expects(:call_geocoder_service).never
96
+ location = Geokit::Geocoders::IpGeocoder.geocode("blah")
97
+ assert_not_nil location
98
+ assert !location.success?
99
+ end
100
+
101
+ def test_service_unavailable
102
+ failure = MockFailure.new
103
+ url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
104
+ Geokit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
105
+ location = Geokit::Geocoders::IpGeocoder.geocode("12.215.42.19")
106
+ assert_not_nil location
107
+ assert !location.success?
108
+ end
109
+ end
@@ -0,0 +1,209 @@
1
+ require 'test/unit'
2
+ require 'geokit'
3
+ require 'mocha'
4
+
5
+ class LatLngTest < Test::Unit::TestCase #:nodoc: all
6
+
7
+ def setup
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)
11
+ end
12
+
13
+ def valid_reverse_geocoding_result
14
+ location = Geokit::GeoLoc.new({
15
+ :city => "Essen",
16
+ :country_code => "DE",
17
+ :lat => 51.4578329,
18
+ :lng => 7.0166848,
19
+ :provider => "google",
20
+ :state => "Nordrhein-Westfalen",
21
+ :street_address => "Porscheplatz 1",
22
+ :zip => "45127"
23
+ })
24
+
25
+ location.full_address = "Porscheplatz 1, 45127 Essen, Deutschland"
26
+ location.precision = 'address'
27
+ location.provider = 'google'
28
+ location.success = true
29
+ location
30
+ end
31
+
32
+ def test_distance_between_same_using_defaults
33
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a)
34
+ assert_equal 0, @loc_a.distance_to(@loc_a)
35
+ end
36
+
37
+ def test_distance_between_same_with_miles_and_flat
38
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :miles, :formula => :flat)
39
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :miles, :formula => :flat)
40
+ end
41
+
42
+ def test_distance_between_same_with_kms_and_flat
43
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :kms, :formula => :flat)
44
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :kms, :formula => :flat)
45
+ end
46
+
47
+ def test_distance_between_same_with_nms_and_flat
48
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :nms, :formula => :flat)
49
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :nms, :formula => :flat)
50
+ end
51
+
52
+ def test_distance_between_same_with_miles_and_sphere
53
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :miles, :formula => :sphere)
54
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :miles, :formula => :sphere)
55
+ end
56
+
57
+ def test_distance_between_same_with_kms_and_sphere
58
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :kms, :formula => :sphere)
59
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :kms, :formula => :sphere)
60
+ end
61
+
62
+ def test_distance_between_same_with_nms_and_sphere
63
+ assert_equal 0, Geokit::LatLng.distance_between(@loc_a, @loc_a, :units => :nms, :formula => :sphere)
64
+ assert_equal 0, @loc_a.distance_to(@loc_a, :units => :nms, :formula => :sphere)
65
+ end
66
+
67
+ def test_distance_between_diff_using_defaults
68
+ assert_in_delta 3.97, Geokit::LatLng.distance_between(@loc_a, @loc_e), 0.01
69
+ assert_in_delta 3.97, @loc_a.distance_to(@loc_e), 0.01
70
+ end
71
+
72
+ def test_distance_between_diff_with_miles_and_flat
73
+ assert_in_delta 3.97, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :miles, :formula => :flat), 0.2
74
+ assert_in_delta 3.97, @loc_a.distance_to(@loc_e, :units => :miles, :formula => :flat), 0.2
75
+ end
76
+
77
+ def test_distance_between_diff_with_kms_and_flat
78
+ assert_in_delta 6.39, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :kms, :formula => :flat), 0.4
79
+ assert_in_delta 6.39, @loc_a.distance_to(@loc_e, :units => :kms, :formula => :flat), 0.4
80
+ end
81
+
82
+ def test_distance_between_diff_with_nms_and_flat
83
+ assert_in_delta 3.334, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :nms, :formula => :flat), 0.4
84
+ assert_in_delta 3.334, @loc_a.distance_to(@loc_e, :units => :nms, :formula => :flat), 0.4
85
+ end
86
+
87
+ def test_distance_between_diff_with_miles_and_sphere
88
+ assert_in_delta 3.97, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :miles, :formula => :sphere), 0.01
89
+ assert_in_delta 3.97, @loc_a.distance_to(@loc_e, :units => :miles, :formula => :sphere), 0.01
90
+ end
91
+
92
+ def test_distance_between_diff_with_kms_and_sphere
93
+ assert_in_delta 6.39, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :kms, :formula => :sphere), 0.01
94
+ assert_in_delta 6.39, @loc_a.distance_to(@loc_e, :units => :kms, :formula => :sphere), 0.01
95
+ end
96
+
97
+ def test_distance_between_diff_with_nms_and_sphere
98
+ assert_in_delta 3.454, Geokit::LatLng.distance_between(@loc_a, @loc_e, :units => :nms, :formula => :sphere), 0.01
99
+ assert_in_delta 3.454, @loc_a.distance_to(@loc_e, :units => :nms, :formula => :sphere), 0.01
100
+ end
101
+
102
+ def test_manually_mixed_in
103
+ assert_equal 0, Geokit::LatLng.distance_between(@point, @point)
104
+ assert_equal 0, @point.distance_to(@point)
105
+ assert_equal 0, @point.distance_to(@loc_a)
106
+ assert_in_delta 3.97, @point.distance_to(@loc_e, :units => :miles, :formula => :flat), 0.2
107
+ assert_in_delta 6.39, @point.distance_to(@loc_e, :units => :kms, :formula => :flat), 0.4
108
+ assert_in_delta 3.334, @point.distance_to(@loc_e, :units => :nms, :formula => :flat), 0.4
109
+ end
110
+
111
+ def test_heading_between
112
+ assert_in_delta 332, Geokit::LatLng.heading_between(@loc_a,@loc_e), 0.5
113
+ end
114
+
115
+ def test_heading_to
116
+ assert_in_delta 332, @loc_a.heading_to(@loc_e), 0.5
117
+ end
118
+
119
+ def test_class_endpoint
120
+ endpoint=Geokit::LatLng.endpoint(@loc_a, 332, 3.97)
121
+ assert_in_delta @loc_e.lat, endpoint.lat, 0.0005
122
+ assert_in_delta @loc_e.lng, endpoint.lng, 0.0005
123
+ end
124
+
125
+ def test_instance_endpoint
126
+ endpoint=@loc_a.endpoint(332, 3.97)
127
+ assert_in_delta @loc_e.lat, endpoint.lat, 0.0005
128
+ assert_in_delta @loc_e.lng, endpoint.lng, 0.0005
129
+ end
130
+
131
+ def test_midpoint
132
+ midpoint=@loc_a.midpoint_to(@loc_e)
133
+ assert_in_delta 32.944061, midpoint.lat, 0.0005
134
+ assert_in_delta(-96.974296, midpoint.lng, 0.0005)
135
+ end
136
+
137
+ def test_normalize
138
+ lat=37.7690
139
+ lng=-122.443
140
+ res=Geokit::LatLng.normalize(lat,lng)
141
+ assert_equal res,Geokit::LatLng.new(lat,lng)
142
+ res=Geokit::LatLng.normalize("#{lat}, #{lng}")
143
+ assert_equal res,Geokit::LatLng.new(lat,lng)
144
+ res=Geokit::LatLng.normalize("#{lat} #{lng}")
145
+ assert_equal res,Geokit::LatLng.new(lat,lng)
146
+ res=Geokit::LatLng.normalize("#{lat.to_i} #{lng.to_i}")
147
+ assert_equal res,Geokit::LatLng.new(lat.to_i,lng.to_i)
148
+ res=Geokit::LatLng.normalize([lat,lng])
149
+ assert_equal res,Geokit::LatLng.new(lat,lng)
150
+ end
151
+
152
+ def test_hash
153
+ lat=37.7690
154
+ lng=-122.443
155
+ first = Geokit::LatLng.new(lat,lng)
156
+ second = Geokit::LatLng.new(lat,lng)
157
+ assert_equal first.hash, second.hash
158
+ end
159
+
160
+ def test_eql?
161
+ lat=37.7690
162
+ lng=-122.443
163
+ first = Geokit::LatLng.new(lat,lng)
164
+ second = Geokit::LatLng.new(lat,lng)
165
+ assert first.eql?(second)
166
+ assert second.eql?(first)
167
+ end
168
+
169
+ def test_reverse_geocode
170
+ point = Geokit::LatLng.new(51.4578329, 7.0166848)
171
+ Geokit::Geocoders::MultiGeocoder.expects(:reverse_geocode).with(point).returns(valid_reverse_geocoding_result)
172
+ res = point.reverse_geocode
173
+
174
+ assert_equal "Nordrhein-Westfalen", res.state
175
+ assert_equal "Essen", res.city
176
+ assert_equal "45127", res.zip
177
+ assert_equal "51.4578329,7.0166848", res.ll # slightly dif from yahoo
178
+ assert res.is_us? == false
179
+ assert_equal "Porscheplatz 1, 45127 Essen, Deutschland", res.full_address #slightly different from yahoo
180
+ end
181
+
182
+ def test_reverse_geocoding_using_specific_geocoder
183
+ point = Geokit::LatLng.new(51.4578329, 7.0166848)
184
+ Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with(point).returns(valid_reverse_geocoding_result)
185
+ res = point.reverse_geocode(:using => Geokit::Geocoders::GoogleGeocoder)
186
+
187
+ assert_equal "Nordrhein-Westfalen", res.state
188
+ assert_equal "Essen", res.city
189
+ assert_equal "45127", res.zip
190
+ assert_equal "51.4578329,7.0166848", res.ll # slightly dif from yahoo
191
+ assert res.is_us? == false
192
+ assert_equal "Porscheplatz 1, 45127 Essen, Deutschland", res.full_address #slightly different from yahoo
193
+ assert_equal "google", res.provider
194
+ end
195
+
196
+ def test_reverse_geocoding_using_specific_geocoder_short_syntax
197
+ point = Geokit::LatLng.new(51.4578329, 7.0166848)
198
+ Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with(point).returns(valid_reverse_geocoding_result)
199
+ res = point.reverse_geocode(:using => :google)
200
+
201
+ assert_equal "Nordrhein-Westfalen", res.state
202
+ assert_equal "Essen", res.city
203
+ assert_equal "45127", res.zip
204
+ assert_equal "51.4578329,7.0166848", res.ll # slightly dif from yahoo
205
+ assert res.is_us? == false
206
+ assert_equal "Porscheplatz 1, 45127 Essen, Deutschland", res.full_address #slightly different from yahoo
207
+ assert_equal "google", res.provider
208
+ end
209
+ end